public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-gcc at vinc17 dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/107839] spurious "may be used uninitialized" warning while all uses are under "if (c)"
Date: Thu, 24 Nov 2022 17:09:53 +0000	[thread overview]
Message-ID: <bug-107839-4-ld3nSP6NVV@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107839-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107839

--- Comment #3 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
(In reply to Richard Biener from comment #2)
> it's loop invariant motion that hoists the v + v compute out of the loop
> and thus outside of its controlling condition.  You can see it's careful
> to not introduce undefined overflow that is possibly conditionally
> executed only but it fails to consider the case of 'v' being conditionally
> uninitialized.
> 
> It's very difficult to do the right thing here - it might be tempting to
> hoist the compute as
> 
>   if (c)
>     tem = v+v;
>   while (1)
>     if (c)
>       f(tem);

Couldn't the -Wmaybe-uninitialized warning be disabled on hoisted code, so that
the controlling condition wouldn't be needed?

To make sure not to disable potential warnings, the information that v was used
for tem should be kept together with tem in the loop. Something like
((void)v,tem), though GCC doesn't currently warn on that if v is uninitialized
(but that's another issue that should be solved).

However...

> Maybe the simplest thing would be to never hoist v + v, or only
> hoist it when the controlling branch is not loop invariant.
> 
> The original testcase is probably more "sensible", does it still have
> a loop invariant controlling condition and a loop invariant computation
> under that control?

In my tmd/binary32/hrcases.c file, there doesn't seem to be a loop invariant,
so I'm wondering what is the real cause. The code looks like the following:

static inline double cldiff (clock_t t1, clock_t t0)
{
  return (double) (t1 - t0) / CLOCKS_PER_SEC;
}

and in a function hrsearch() where its mprog argument (named c above) is an
integer that enables progress output when it is nonzero:

  if (mprog)
    {
      mctr = 0;
      nctr = 0;
      t0 = ti = clock ();
    }

  do
    {
[...]
      if (mprog && ++mctr == mprog)
        {
          mctr = 0;
          tj = clock ();
          mpfr_fprintf (stderr, "[exponent %ld: %8.2fs %8.2fs  %5lu / %lu]\n",
                        e, cldiff (tj, ti), cldiff (tj, t0), ++nctr, nprog);
          ti = tj;
        }
[...]
    }
  while (mpfr_get_exp (x) < e + 2);

The warning I get is

In function ‘cldiff’,
    inlined from ‘hrsearch’ at hrcases.c:298:11,
    inlined from ‘main’ at hrcases.c:520:9:
hrcases.c:46:23: warning: ‘t0’ may be used uninitialized
[-Wmaybe-uninitialized]
   46 |   return (double) (t1 - t0) / CLOCKS_PER_SEC;
      |                   ~~~~^~~~~
hrcases.c: In function ‘main’:
hrcases.c:128:11: note: ‘t0’ was declared here
  128 |   clock_t t0, ti, tj;
      |           ^~

So the operation on t0 is tj - t0, and as tj is set just before, I don't see
how it can be used in a loop invariant.

This can be simplified as follows:

int f (int);
void g (int mprog)
{
  int t0, ti, tj;

  if (mprog)
    t0 = ti = f(0);

  do
    if (mprog)
      {
        tj = f(0);
        f(tj - ti);
        f(tj - t0);
        ti = tj;
      }
  while (f(0));
}

and I get

tst.c: In function ‘g’:
tst.c:13:9: warning: ‘t0’ may be used uninitialized [-Wmaybe-uninitialized]
   13 |         f(tj - ti);
      |         ^~~~~~~~~~
tst.c:4:7: note: ‘t0’ was declared here
    4 |   int t0, ti, tj;
      |       ^~

BTW, the warning is incorrect: I can't see t0 in "f(tj - ti);".

  parent reply	other threads:[~2022-11-24 17:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-23 16:38 [Bug tree-optimization/107839] New: " vincent-gcc at vinc17 dot net
2022-11-23 21:00 ` [Bug tree-optimization/107839] " rguenth at gcc dot gnu.org
2022-11-24 10:12 ` rguenth at gcc dot gnu.org
2022-11-24 17:09 ` vincent-gcc at vinc17 dot net [this message]
2022-12-05  9:32 ` cvs-commit at gcc dot gnu.org
2022-12-05  9:47 ` rguenth at gcc dot gnu.org
2022-12-12 11:20 ` cvs-commit at gcc dot gnu.org
2024-01-20 17:22 ` pinskia at gcc dot gnu.org

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-107839-4-ld3nSP6NVV@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: 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).