I would have sworn that there was a bug open about this, but I can't find it, and this bug has been annoying me for years. Compiling this code with g++ -O2 -Wall: #ifndef __cplusplus #include <stdbool.h> #endif extern int intval(); extern bool cond(); int main() { bool valid = false; int value; if (cond()) { valid = true; value = intval(); } while (!valid || intval() < value) ; return 0; } says: trivial_uninit_warning.cc: In function ‘int main()’: trivial_uninit_warning.cc:18:17: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized] while (!valid || intval() < value) Oddly, compiling exactly the same code in C (instead of C++) does not warn. This seems quite sensitive to exactly what the code is doing. IIRC the issue is that the "load" of value is moved ahead of the check of valid by the optimizer (which is entirely valid), but then that load appears to access an uninitialized value and warns.
This might be a duplicate of PR56574
Could you remove the stdbool.h header (just use char or int, it should not change the bug)? Also could you compile with -fdump-tree-all-all-lineno and paste/attach the dump file that looks similar to test.c.XXXt.uninitX? It should show clearly whether it is a duplicate or a different issue.
Created attachment 33484 [details] Headerless reproducer (c++ only)
Created attachment 33485 [details] Output from g++ -O2 -Wall -fdump-tree-all-all-lineno pr59500.cc
(In reply to Andy Lutomirski from comment #1) > This might be a duplicate of PR56574 I think not. In this case the problem is that # value = PHI<value(D),intval()> if (!valid || intval() < value) is converted to # value = PHI<value(D),intval() > if(!valid) else if (intval() < value) and I think the uninit pass is not smart enough to realize that the use is guarded by valid != 0 but the default definition implies valid == 0. Perhaps it is also a missed-optimization, since "if(cond())" could jump directly to "if (intval() < value)".
This was fixed a few years ago by: Author: amker <amker@138bc75d-0d04-0410-961f-82ee72b054a4> Date: Wed Feb 26 01:49:35 2014 +0000 PR target/60280 * tree-cfgcleanup.c (tree_forwarder_block_p): Protect loop preheaders and latches only if requested. Fix latch if it is removed. * tree-ssa-dom.c (tree_ssa_dominator_optimize): Set LOOPS_HAVE_PREHEADERS. PR target/60280 * gnat.dg/renaming5.adb: Change to two expected gotos. * gcc.dg/tree-ssa/pr21559.c: Change back to three expected jump threads. * gcc.dg/tree-prof/update-loopch.c: Check two "Invalid sum" messages for removed basic block. * gcc.dg/tree-ssa/ivopt_1.c: Fix unreliable scanning string. * gcc.dg/tree-ssa/ivopt_2.c: Ditto. * gcc.dg/tree-ssa/ivopt_3.c: Ditto. * gcc.dg/tree-ssa/ivopt_4.c: Ditto.
Per c#6.