aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>2025-09-05 16:50:58 -0700
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>2025-09-06 09:19:00 -0700
commit8bd31f9248185824ac015a510f954fb13056230b (patch)
treec184bd1e04243f1c60594b7a61b7cd39d1677f25 /gcc
parent477868c59ed6d8192d16661c17b94201153cb88c (diff)
downloadgcc-8bd31f9248185824ac015a510f954fb13056230b.zip
gcc-8bd31f9248185824ac015a510f954fb13056230b.tar.gz
gcc-8bd31f9248185824ac015a510f954fb13056230b.tar.bz2
phiopt: Improve locations for factor out conditional operation [PR108466]
This improves the locations for the phi args and the newly created statement. Since this is a factorization/commonizing in one case the location for the new statement will not always be set correctly either way. The new locations on the new phi will either be the old location of the argument to the phi or the location of the defining statement (if it exists). The new statement will be either the location of the phi or the location of the defining statements if the location of the phi is unknown. This fixes the location of an uninitialized variable warning too. Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/108466 gcc/ChangeLog: * tree-ssa-phiopt.cc (factor_out_conditional_operation): Give better locations to the new phi args and the new statement. gcc/testsuite/ChangeLog: * gcc.dg/uninit-pr108466-1.c: New test. Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/testsuite/gcc.dg/uninit-pr108466-1.c23
-rw-r--r--gcc/tree-ssa-phiopt.cc33
2 files changed, 53 insertions, 3 deletions
diff --git a/gcc/testsuite/gcc.dg/uninit-pr108466-1.c b/gcc/testsuite/gcc.dg/uninit-pr108466-1.c
new file mode 100644
index 0000000..817a364
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/uninit-pr108466-1.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wmaybe-uninitialized" } */
+
+/* PR tree-optimization/108466 */
+/* The location of the uninitialized warning was missing due
+ to phi factoring. */
+
+typedef unsigned long long T;
+
+T f(void);
+unsigned char g(void)
+{
+ T a;
+
+ if (f())
+ a = f();
+ if (f())
+ return 0;
+ else
+ return a; /* { dg-warning "may be used" } */
+}
+
+
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 73341e7..e1c9e12 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -323,6 +323,13 @@ factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
tree arg0 = gimple_phi_arg_def (phi, e0->dest_idx);
tree arg1 = gimple_phi_arg_def (phi, e1->dest_idx);
+ location_t narg0_loc = gimple_location (phi);
+ location_t narg1_loc = gimple_location (phi);
+ if (gimple_phi_arg_location (phi, e0->dest_idx) != UNKNOWN_LOCATION)
+ narg0_loc = gimple_phi_arg_location (phi, e0->dest_idx);
+ if (gimple_phi_arg_location (phi, e1->dest_idx) != UNKNOWN_LOCATION)
+ narg1_loc = gimple_phi_arg_location (phi, e1->dest_idx);
+
gcc_assert (arg0 != NULL_TREE && arg1 != NULL_TREE);
/* Arugments that are the same don't have anything to be
@@ -365,6 +372,8 @@ factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
return false;
if (!is_factor_profitable (arg0_def_stmt, merge, new_arg0))
return false;
+ if (gimple_has_location (arg0_def_stmt))
+ narg0_loc = gimple_location (arg0_def_stmt);
if (TREE_CODE (arg1) == SSA_NAME)
{
@@ -385,9 +394,21 @@ factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
return false;
new_arg1 = arg1_op.ops[0];
-
if (!is_factor_profitable (arg1_def_stmt, merge, new_arg1))
return false;
+ if (gimple_has_location (arg1_def_stmt))
+ narg1_loc = gimple_location (arg1_def_stmt);
+
+ /* Chose the location for the new statement if the phi location is unknown. */
+ if (locus == UNKNOWN_LOCATION)
+ {
+ if (narg0_loc == UNKNOWN_LOCATION
+ && narg1_loc != UNKNOWN_LOCATION)
+ locus = narg1_loc;
+ else if (narg0_loc != UNKNOWN_LOCATION
+ && narg1_loc == UNKNOWN_LOCATION)
+ locus = narg0_loc;
+ }
}
else
{
@@ -472,6 +493,10 @@ factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
/* Drop the overlow that fold_convert might add. */
if (TREE_OVERFLOW (new_arg1))
new_arg1 = drop_tree_overflow (new_arg1);
+
+ /* The locus of the new statement is arg0 defining statement. */
+ if (gimple_has_location (arg0_def_stmt))
+ locus = gimple_location (arg0_def_stmt);
}
/* If types of new_arg0 and new_arg1 are different bailout. */
@@ -497,6 +522,8 @@ factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
return false;
}
+ if (locus != UNKNOWN_LOCATION)
+ annotate_all_with_location (seq, locus);
gsi = gsi_after_labels (gimple_bb (phi));
gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
@@ -525,8 +552,8 @@ factor_out_conditional_operation (edge e0, edge e1, basic_block merge,
release_defs (arg1_def_stmt);
}
- add_phi_arg (newphi, new_arg0, e0, locus);
- add_phi_arg (newphi, new_arg1, e1, locus);
+ add_phi_arg (newphi, new_arg0, e0, narg0_loc);
+ add_phi_arg (newphi, new_arg1, e1, narg1_loc);
/* Remove the original PHI stmt. */
gsi = gsi_for_stmt (phi);