aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2024-03-14 17:48:30 +0100
committerJakub Jelinek <jakub@redhat.com>2024-06-20 15:07:38 +0200
commit8a917760aa47f7fb307a4abf6e17dd5b52a5b25f (patch)
treecdee063a52f8164ca51627c98c79623e2d1635cd
parent0570303f818ed12ff28de0d258ebe4f6803ef7e0 (diff)
icf: Reset SSA_NAME_{PTR,RANGE}_INFO in successfully merged functions [PR113907]
AFAIK we have no code in LTO streaming to stream out or in SSA_NAME_{RANGE,PTR}_INFO, so LTO effectively throws it all away and let vrp1 and alias analysis after IPA recompute that. There is just one spot, for IPA VRP and IPA bit CCP we save/restore ranges and set SSA_NAME_{PTR,RANGE}_INFO e.g. on parameters depending on what we saved and propagated, but that is after streaming in bodies for the post IPA optimizations. Now, without LTO SSA_NAME_{RANGE,PTR}_INFO is already computed from earlier in many cases (er.g. evrp and early alias analysis but other spots too), but IPA ICF is ignoring the ranges and points-to details when comparing the bodies. I think ignoring that is just fine, that is effectively what we do for LTO where we throw that information away before the analysis, and not ignoring it could lead to fewer ICF merging possibilities. So, the following patch instead verifies that for LTO SSA_NAME_{PTR,RANGE}_INFO just isn't there on SSA_NAMEs in functions into which other functions have been ICFed, and for non-LTO throws that information away (which matches the LTO behavior). Another possibility would be to remember the SSA_NAME <-> SSA_NAME mapping vector (just one of the 2) on successful sem_function::equals on the sem_function which is not the chosen leader (e.g. how SSA_NAMEs in the leader map to SSA_NAMEs in the other function) and use that vector to union the ranges in sem_function::merge. I can implement that for comparison, but wanted to post this first if there is an agreement on doing that or if Honza thinks we should take SSA_NAME_{RANGE,PTR}_INFO into account. I think we can compare SSA_NAME_RANGE_INFO, but have no idea how to try to compare points to info. And I think it will result in less effective ICF for non-LTO vs. LTO unnecessarily. 2024-03-12 Jakub Jelinek <jakub@redhat.com> PR middle-end/113907 * ipa-icf.c (sem_item_optimizer::merge_classes): Reset SSA_NAME_RANGE_INFO and SSA_NAME_PTR_INFO on successfully ICF merged functions. * gcc.dg/pr113907-1.c: New test. (cherry picked from commit 7580e39452b65ab5fb5a06f3f1ad7d59720269b5)
-rw-r--r--gcc/ipa-icf.c32
-rw-r--r--gcc/testsuite/gcc.dg/pr113907-1.c49
2 files changed, 80 insertions, 1 deletions
diff --git a/gcc/ipa-icf.c b/gcc/ipa-icf.c
index 44407e1b5cd..f054a9c7842 100644
--- a/gcc/ipa-icf.c
+++ b/gcc/ipa-icf.c
@@ -3389,6 +3389,7 @@ sem_item_optimizer::merge_classes (unsigned int prev_class_count,
continue;
sem_item *source = c->members[0];
+ bool this_merged_p = false;
if (DECL_NAME (source->decl)
&& MAIN_NAME_P (DECL_NAME (source->decl)))
@@ -3435,7 +3436,7 @@ sem_item_optimizer::merge_classes (unsigned int prev_class_count,
if (dbg_cnt (merged_ipa_icf))
{
bool merged = source->merge (alias);
- merged_p |= merged;
+ this_merged_p |= merged;
if (merged && alias->type == VAR)
{
@@ -3444,6 +3445,35 @@ sem_item_optimizer::merge_classes (unsigned int prev_class_count,
}
}
}
+
+ merged_p |= this_merged_p;
+ if (this_merged_p
+ && source->type == FUNC
+ && (!flag_wpa || flag_checking))
+ {
+ unsigned i;
+ tree name;
+ FOR_EACH_SSA_NAME (i, name, DECL_STRUCT_FUNCTION (source->decl))
+ {
+ /* We need to either merge or reset SSA_NAME_*_INFO.
+ For merging we don't preserve the mapping between
+ original and alias SSA_NAMEs from successful equals
+ calls. */
+ if (POINTER_TYPE_P (TREE_TYPE (name)))
+ {
+ if (SSA_NAME_PTR_INFO (name))
+ {
+ gcc_checking_assert (!flag_wpa);
+ SSA_NAME_PTR_INFO (name) = NULL;
+ }
+ }
+ else if (SSA_NAME_RANGE_INFO (name))
+ {
+ gcc_checking_assert (!flag_wpa);
+ SSA_NAME_RANGE_INFO (name) = NULL;
+ }
+ }
+ }
}
if (!m_merged_variables.is_empty ())
diff --git a/gcc/testsuite/gcc.dg/pr113907-1.c b/gcc/testsuite/gcc.dg/pr113907-1.c
new file mode 100644
index 00000000000..04c4fb8c128
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr113907-1.c
@@ -0,0 +1,49 @@
+/* PR middle-end/113907 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+/* { dg-additional-options "-minline-all-stringops" { target i?86-*-* x86_64-*-* } } */
+
+static inline int
+foo (int len, void *indata, void *outdata)
+{
+ if (len < 0 || (len & 7) != 0)
+ return 0;
+ if (len != 0 && indata != outdata)
+ __builtin_memcpy (outdata, indata, len);
+ return len;
+}
+
+static inline int
+bar (int len, void *indata, void *outdata)
+{
+ if (len < 0 || (len & 1) != 0)
+ return 0;
+ if (len != 0 && indata != outdata)
+ __builtin_memcpy (outdata, indata, len);
+ return len;
+}
+
+int (*volatile p1) (int, void *, void *) = foo;
+int (*volatile p2) (int, void *, void *) = bar;
+
+__attribute__((noipa)) int
+baz (int len, void *indata, void *outdata)
+{
+ if ((len & 6) != 0)
+ bar (len, indata, outdata);
+ else
+ foo (len, indata, outdata);
+}
+
+struct S { char buf[64]; } s __attribute__((aligned (8)));
+
+int
+main ()
+{
+ for (int i = 0; i < 64; ++i)
+ s.buf[i] = ' ' + i;
+ p2 (2, s.buf, s.buf + 33);
+ for (int i = 0; i < 64; ++i)
+ if (s.buf[i] != ' ' + ((i >= 33 && i < 35) ? i - 33 : i))
+ __builtin_abort ();
+}