With pass_lower_switch moved after the late jump threading, at -O1 the
default branch of a nested switch can no longer be folded away because
no optimization pass runs after the lowering. The resulting dead edge
carries the uninitialized value into the return PHI and the uninit
predicate analysis, which reasons about control dependence only, cannot
prove it unreachable, so -Wmaybe-uninitialized flags a false positive
(gcc.dg/uninit-pr44547.c).
Enable a ranger during the late uninit pass and, before the predicate
analysis gives up on an unguarded use, prove each maybe-undefined PHI
operand's incoming edge infeasible from the value range of the
condition controlling that edge. If every such edge is infeasible the
undefined value cannot reach the use.
gcc/
* gimple-predicate-analysis.cc (prune_phi_opnds_by_ranges): New.
(uninit_analysis::is_use_guarded): Call it.
* tree-ssa-uninit.cc (execute_late_warn_uninitialized): Enable a
ranger around the analysis and disable it before returning.
---
gcc/gimple-predicate-analysis.cc | 67 ++++++++++++++++++++++++++++++++
gcc/tree-ssa-uninit.cc | 7 ++++
2 files changed, 74 insertions(+)
diff --git a/gcc/gimple-predicate-analysis.cc b/gcc/gimple-predicate-analysis.cc
index 6ecb1bc0387..03845693ce1 100644
--- a/gcc/gimple-predicate-analysis.cc
+++ b/gcc/gimple-predicate-analysis.cc
@@ -40,6 +40,7 @@
#include "builtins.h"
#include "calls.h"
#include "value-query.h"
+#include "gimple-range.h"
#include "cfganal.h"
#include "tree-eh.h"
#include "gimple-fold.h"
@@ -2242,6 +2243,66 @@ uninit_analysis::init_from_phi_def (gphi *phi)
VISITED_PHIS is a pointer set of phis being visited. */
+/* Return true if every operand of PHI marked in OPNDS arrives on an edge
+ that is provably unreachable according to the value range of the
+ condition controlling that edge. This prunes dead default branches of
+ a switch that has been lowered late (after the last jump-threading pass)
+ and therefore could not be eliminated by optimization, avoiding false
+ positives from -Wmaybe-uninitialized. */
+
+static bool
+prune_phi_opnds_by_ranges (gphi *phi, unsigned opnds)
+{
+ if (!opnds)
+ return false;
+
+ range_query *query = get_range_query (cfun);
+ unsigned n = gimple_phi_num_args (phi);
+ bool any = false;
+
+ for (unsigned i = 0; i < n; i++)
+ {
+ if (!MASK_TEST_BIT (opnds, i))
+ continue;
+ any = true;
+
+ edge e = gimple_phi_arg_edge (phi, i);
+
+ /* Walk up through forwarder blocks to the conditional that guards
+ the incoming edge. */
+ while (single_pred_p (e->src) && single_succ_p (e->src))
+ e = single_pred_edge (e->src);
+
+ if (EDGE_COUNT (e->src->succs) != 2)
+ return false;
+
+ gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (e->src));
+ if (!cond)
+ return false;
+
+ /* Canonicalize to the SSA_NAME compared against a constant. */
+ tree lhs = gimple_cond_lhs (cond);
+ tree rhs = gimple_cond_rhs (cond);
+ tree var = NULL_TREE;
+ if (TREE_CODE (lhs) == SSA_NAME && is_gimple_constant (rhs))
+ var = lhs;
+ else if (TREE_CODE (rhs) == SSA_NAME && is_gimple_constant (lhs))
+ var = rhs;
+ if (!var)
+ return false;
+
+ /* The range of VAR on edge E is empty when the branch to E is
+ infeasible, in which case this operand cannot reach PHI. */
+ int_range_max r;
+ if (!query->range_on_edge (r, e, var))
+ return false;
+ if (!r.undefined_p ())
+ return false;
+ }
+
+ return any;
+}
+
bool
uninit_analysis::is_use_guarded (gimple *use_stmt, basic_block use_bb,
gphi *phi, unsigned opnds,
@@ -2250,6 +2311,12 @@ uninit_analysis::is_use_guarded (gimple *use_stmt,
basic_block use_bb,
if (visited->add (phi))
return false;
+ /* If every maybe-undefined operand of PHI arrives on a provably
+ infeasible edge, the undefined value cannot reach any use, no matter
+ what predicate guards the use. */
+ if (prune_phi_opnds_by_ranges (phi, opnds))
+ return true;
+
/* The basic block where the PHI is defined. */
basic_block def_bb = gimple_bb (phi);
diff --git a/gcc/tree-ssa-uninit.cc b/gcc/tree-ssa-uninit.cc
index 7d242ab7530..454f27f000d 100644
--- a/gcc/tree-ssa-uninit.cc
+++ b/gcc/tree-ssa-uninit.cc
@@ -1469,6 +1469,12 @@ execute_late_warn_uninitialized (function *fun)
unreachable blocks. */
set_all_edges_as_executable (fun);
mark_dfs_back_edges (fun);
+
+ /* Enable a ranger so the uninit analysis can prove some incoming PHI
+ edges infeasible from value ranges (e.g. the default branch of a
+ switch lowered late, which no pass could fold away at -O1). */
+ enable_ranger (fun);
+
int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
int n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
@@ -1511,6 +1517,7 @@ execute_late_warn_uninitialized (function *fun)
free (rpo);
free (bb_to_rpo);
+ disable_ranger (fun);
delete possibly_undefined_names;
possibly_undefined_names = NULL;
delete defined_args;
--
2.34.1