IPA-SRA already contains a check to figure out that an otherwise dead
parameter is actually required because of non-call exceptions, but it
is not present at the equivalent spot where SRA figures out whether
the return statement is used for anything useful. This patch adds
that condition there.
Unfortunately, even though this patch should be good enough for any
normal (I'd even say reasonable) use of the compiler, it hints that
when the user manually switches all sorts of DCE, IPA-SRA would
probably leave behind problematic statements manipulating what
originally were return values, just like it does for parameters (PR
93385). Fixing this properly might unfortunately be a separate issue
from the mentioned bug because the LHS of a call is changed during
call redirection and the caller often is not a clone. But I'll see
what I can do.
Meanwhile, the patch below has been bootstrapped and tested on x86_64.
gcc/ChangeLog:
2021-01-18 Martin Jambor <mjambor@suse.cz>
PR ipa/98690
* ipa-sra.c (ssa_name_only_returned_p): New parameter fun. Check
whether non-call exceptions allow removal of a statement.
(isra_analyze_call): Pass the appropriate function to
ssa_name_only_returned_p.
gcc/testsuite/ChangeLog:
2021-01-18 Martin Jambor <mjambor@suse.cz>
PR ipa/98690
* g++.dg/ipa/pr98690.C: New test.
-/* Return true if SSA_NAME NAME is only used in return statements, or if
- results of any operations it is involved in are only used in return
- statements. ANALYZED is a bitmap that tracks which SSA names we have
- already started investigating. */
+/* Return true if SSA_NAME NAME of function described by FUN is only used in
+ return statements, or if results of any operations it is involved in are
+ only used in return statements. ANALYZED is a bitmap that tracks which SSA
+ names we have already started investigating. */
-ssa_name_only_returned_p (tree name, bitmap analyzed)
+ssa_name_only_returned_p (function *fun, tree name, bitmap analyzed)
{
bool res = true;
imm_use_iterator imm_iter;
{
bool res = true;
imm_use_iterator imm_iter;
- else if ((is_gimple_assign (stmt) && !gimple_has_volatile_ops (stmt))
- || gimple_code (stmt) == GIMPLE_PHI)
+ else if (!stmt_unremovable_because_of_non_call_eh_p (fun, stmt)
+ && ((is_gimple_assign (stmt) && !gimple_has_volatile_ops (stmt))
+ || gimple_code (stmt) == GIMPLE_PHI))
{
/* TODO: And perhaps for const function calls too? */
tree lhs;
{
/* TODO: And perhaps for const function calls too? */
tree lhs;
}
gcc_assert (!gimple_vdef (stmt));
if (bitmap_set_bit (analyzed, SSA_NAME_VERSION (lhs))
}
gcc_assert (!gimple_vdef (stmt));
if (bitmap_set_bit (analyzed, SSA_NAME_VERSION (lhs))
- && !ssa_name_only_returned_p (lhs, analyzed))
+ && !ssa_name_only_returned_p (fun, lhs, analyzed))
if (TREE_CODE (lhs) == SSA_NAME)
{
bitmap analyzed = BITMAP_ALLOC (NULL);
if (TREE_CODE (lhs) == SSA_NAME)
{
bitmap analyzed = BITMAP_ALLOC (NULL);
- if (ssa_name_only_returned_p (lhs, analyzed))
+ if (ssa_name_only_returned_p (DECL_STRUCT_FUNCTION (cs->caller->decl),
+ lhs, analyzed))
csum->m_return_returned = true;
BITMAP_FREE (analyzed);
}
csum->m_return_returned = true;
BITMAP_FREE (analyzed);
}
--- /dev/null
+/* { dg-do run } */
+/* { dg-options "-O2 -fnon-call-exceptions" } */
+
+int g;
+volatile int v;
+
+static int * __attribute__((noinline))
+almost_useless_return (void)
+{
+ v = 1;
+ return &g;
+}
+
+static void __attribute__((noinline))
+foo (void)
+{
+ int *p = almost_useless_return ();
+ int i = *p;
+ v = 2;
+}
+
+int
+main (int argc, char *argv[])
+{
+ foo ();
+ return 0;
+}