Bug 59970 - Bogus Wuninitialized warnings at low optimization levels
Summary: Bogus Wuninitialized warnings at low optimization levels
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: unknown
: P3 minor
Target Milestone: 7.0
Assignee: Thomas Schwinge
URL:
Keywords: diagnostic
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2014-01-28 21:06 UTC by Thomas Schwinge
Modified: 2021-03-26 22:40 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build: x86_64-linux-gnu
Known to work:
Known to fail: 4.8.4, 4.9.4, 5.5.0, 6.4.0
Last reconfirmed:


Attachments
cp-demangle.c (419 bytes, text/plain)
2014-01-28 21:06 UTC, Thomas Schwinge
Details
Preprocessed source for boost::lexical_cast case (192.56 KB, application/octet-stream)
2015-04-22 13:39 UTC, Mike Crowe
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Schwinge 2014-01-28 21:06:31 UTC
Created attachment 31970 [details]
cp-demangle.c

The attached code is greatly reduced from libiberty/cp-demangle.c.  As discussed in <http://news.gmane.org/find-root.php?message_id=%3C87bnyw41hf.fsf%40kepler.schwinge.homeip.net%3E>, with trunk r207200, it exhibits the following behavior:

    $ for t in '' -DTOGGLE1; do for a in '' -DABORT; do for o in -O0 -Og -O1 -O2 -O3 -Ofast; do (set -x && gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c $t $a $o); done; done; done
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -O0
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -Og
    ../../cp-demangle.c: In function 'd_demangle_callback':
    ../../cp-demangle.c:40:3: warning: 'dc' may be used uninitialized in this function [-Wmaybe-uninitialized]
       return dc;
       ^
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -O1
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -O2
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -O3
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -Ofast
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DABORT -O0
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DABORT -Og
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DABORT -O1
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DABORT -O2
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DABORT -O3
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DABORT -Ofast
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -O0
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -Og
    ../../cp-demangle.c: In function 'd_demangle_callback':
    ../../cp-demangle.c:40:3: warning: 'dc' may be used uninitialized in this function [-Wmaybe-uninitialized]
       return dc;
       ^
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -O1
    ../../cp-demangle.c: In function 'd_demangle_callback':
    ../../cp-demangle.c:40:3: warning: 'dc' may be used uninitialized in this function [-Wmaybe-uninitialized]
       return dc;
       ^
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -O2
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -O3
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -Ofast
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -DABORT -O0
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -DABORT -Og
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -DABORT -O1
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -DABORT -O2
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -DABORT -O3
    + gcc/xgcc -Bgcc/ -Wall -Wextra -c ../../cp-demangle.c -DTOGGLE1 -DABORT -Ofast

In certain cases, GCC fails to track that all the possible values for enum type indeed have been covered, and so dc must have been initialized.

It does seem appropriate to me for GCC not to do such tracking at low optimization levels, but then the uninitialized warning should also be disabled (which I assume is happening for -O0?) to avoid false positive warnings.

The abort to libiberty/cp-demangle.c has been added in r207200.  The libgomp/env.c change r203514 looks like another instance of this issue, <http://news.gmane.org/find-root.php?message_id=%3C20131014082733.GF30970%40tucnak.zalov.cz%3E>.

I'm assigning this to me, tentatively, low priority, so if someone has any clues, don't hesititate to take it over.
Comment 1 Andrew Pinski 2014-01-28 21:22:06 UTC
I don't think this is a GCC bug except for the unitialized predicates is not working.  The reason why it fails for -Og is because jump threading is disabled as you want to be able to step through the code.  -O1 must not decided jump threading is profitable either.

For the unitialized predicates issue, it might be because switch statements are not handled.
Comment 2 Matt Godbolt 2014-04-25 01:36:25 UTC
FWIW I stumbled over what I believe is the same issue inside the boost v1.55.0 lexical_cast implementation.  That also only shows up as a "possible uninitialized" at -Og optimization only.  I can provide preprocessed source if necessary (though, being boost, it's 80k lines, ugh) but if you have gcc4.9.0 and boost 1.55 available:

$ cat test.cc
#include <boost/lexical_cast.hpp>

int test() { return boost::lexical_cast<int>("1"); }

$ g++ -Wall -Wextra -Og -c test.cc
test.cc: In function ‘int test()’:
test.cc:4:40: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     return boost::lexical_cast<int>("1");


The relevant lines in boost itself appear to be http://svn.boost.org/svn/boost/tags/release/Boost_1_55_0/boost/lexical_cast.hpp lines 2365-2377 where the "result" is optionally initialized.

I'll also submit this to the boost developers in case they have some ideas.
Comment 3 Mike Crowe 2015-04-22 13:39:05 UTC
Created attachment 35384 [details]
Preprocessed source for boost::lexical_cast case

I can reproduce the same problem with Debian Jessie's gcc 4.9.2 and a self-compiled gcc 4.9.1 when compiling:

 #include <boost/lexical_cast.hpp>

 int wibble()
 {
     return boost::lexical_cast<int>("1");
 }

Preprocessed source attached (compressed to avoid file size limit.)

 $ g++ -Wall -Werror -Og -save-temps test.cpp
 test.cpp: In function ‘int wibble()’:
 test.cpp:5:40: error: ‘result’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
      return boost::lexical_cast<int>("1");
                                        ^
 cc1plus: all warnings being treated as errors

Oddly the warning is not reported when the function is named "main"!
Comment 4 Mike Crowe 2015-04-22 16:56:34 UTC
The test case in comment #3 shows the same problem in gcc-5-branch r222333.
Comment 5 Manuel López-Ibáñez 2016-09-03 10:02:23 UTC
(In reply to Thomas Schwinge from comment #0)
> It does seem appropriate to me for GCC not to do such tracking at low
> optimization levels, but then the uninitialized warning should also be
> disabled (which I assume is happening for -O0?) to avoid false positive
> warnings.

It doesn't work like that. The uninit pass sees the code as transformed by other optimization passes, thus what the uninit pass sees depends not on a particular level but on the passes enabled, their order and their effect on the given code. Higher levels do not always mean better uninit warnings, either. Nonetheless, it would be useful to identify which passes do more good than harm for uninit (probably VRP) and enable those at lower levels somehow or even in some limited form in the FEs.
Comment 6 Eyal Rozenberg 2017-01-26 12:00:47 UTC
Chiming in after having noticed this issue with GCC 5.4.0 20160609 on Linux MInt 18.1 with Boost 1.58.0 (using lexical_cast). Quite annoying...
Comment 7 Martin Sebor 2021-03-26 22:27:28 UTC
The test case in comment #0 is not diagnosed anymore.  Bisection points to r240221 as the fix.  Ditto for attachment 35384 [details] from comment #3.  Let me add the one from attachment 31970 [details] to the test suite and resolve this as fixed.
Comment 8 GCC Commits 2021-03-26 22:40:50 UTC
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:980b12cc81979e52f491bf0dfe961d30c07fe864

commit r11-7869-g980b12cc81979e52f491bf0dfe961d30c07fe864
Author: Martin Sebor <msebor@redhat.com>
Date:   Fri Mar 26 16:37:34 2021 -0600

    PR tree-optimization/59970 - Bogus -Wmaybe-uninitialized at low optimization levels
    
            PR tree-optimization/59970
            * gcc.dg/uninit-pr59970.c: New test.