public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Julian Seward <sewardj42@gmail.com>
Cc: Mark Wielaard <mark@klomp.org>,
	David Malcolm <dmalcolm@redhat.com>,
	 GCC Development <gcc@gcc.gnu.org>
Subject: Re: Uninit warnings due to optimizing short-circuit conditionals
Date: Tue, 15 Feb 2022 14:28:04 +0100	[thread overview]
Message-ID: <CAFiYyc3qoAb+Je57xJCAPwPstFm9b8RrM-aQ+3f8ONWDEGjDhQ@mail.gmail.com> (raw)
In-Reply-To: <CAPQtpYuKWMgYJua7dLrCsph-wMLgLZDYjuoLuD-fVZA5r2Hm=Q@mail.gmail.com>

On Tue, Feb 15, 2022 at 2:00 PM Julian Seward <sewardj42@gmail.com> wrote:
>
> Sorry for the delayed response.  I've been paging this all back in.
>
> I first saw this problem when memcheck-ing Firefox as compiled by Clang, some
> years back.  Not long after GCC was also at it.  The transformation in
> question is (at the C level):
>
> A && B  ==>  B && A   if it can be proved that A
>                       is always false whenever B is undefined
>                       and (I assume) that B is provably exception-free
>
> where && means the standard lazy left-first C logical-AND.  I believe this
> might have become more prevalent due to ever-more aggressive inlining (at
> least for Firefox), which presented the compilers with greater opportunities
> to make the required proofs.

Note GCC doesn't try to prove this, instead it reasons that when
B is undefined it takes an indeterminate value and if A is _not_ always
false then the program would have invoked undefined behavior, so we
can disregard this possibility and assume B is not undefined.  So
either B is not undefined and everything is OK, or B is undefined but
then A must be always false.

Note that when A is always false we may have transformed a valid
program (does not access B) into a program invoking undefined behavior
(in C language terms).  We don't treat undefined uses as "very" undefined
behavior but I do remember we've shot ourselves in the foot with this
transform - in this case we'd have to make the use of B determinate
somehow, something we cannot yet do.  So we'd want a transform
like

 A && B ==> OK(B) && A

where 'OK' sanitizes B in case it is undefined.  The error we can run into
is that two of the uninit Bs can be equated to two different values,
breaking the B == B invariant (technically also OK, but not if it was us
that introduced the undefinedness in the first place).

Richard.

  reply	other threads:[~2022-02-15 13:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-14 15:57 David Malcolm
2022-02-14 16:26 ` Jeff Law
2022-02-14 17:10   ` David Malcolm
2022-02-14 16:57 ` Mark Wielaard
2022-02-14 17:20   ` David Malcolm
2022-02-14 17:37     ` Mark Wielaard
2022-02-15  7:25       ` Richard Biener
2022-02-15 12:29         ` Mark Wielaard
2022-02-15 13:00           ` Julian Seward
2022-02-15 13:28             ` Richard Biener [this message]
2022-02-15 21:40               ` David Malcolm

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=CAFiYyc3qoAb+Je57xJCAPwPstFm9b8RrM-aQ+3f8ONWDEGjDhQ@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=mark@klomp.org \
    --cc=sewardj42@gmail.com \
    /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: link
Be 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).