> >Would you be OK with a slight variation of your earlier idea, i.e.
> >calling fold_stmt with a specific valueizer from fold_marked_statements
> >instead of the implicit no_follow_ssa_edges in the inliner? Something
> >like:
> >
> >tree
> >follow_anonymous_single_use_edges (tree val)
> >{
> >
> > if (TREE_CODE (val) == SSA_NAME
> >
> > && (!SSA_NAME_VAR (val) || DECL_IGNORED_P (SSA_NAME_VAR
> > (var)))
> > && has_single_use (val))
> >
> > return val
> >
> > return NULL_TREE;
> >
> >}
>
> Yes, that works for me as well.
But not for GCC. :-) The propagation per se works but, since the statement is
not folded in the end, no replacement is made at all...
So we're back to square one and anonymous constant propagation seems to be the
only way out at -O0. The attached patch implements it in a less hackish way
(and enables it unconditionally when optimizing as suggested by Jan) by doing
it just before invoking fold_stmt on the marked statements so this should make
folding more effective in the process.
Tested (compiler only) on x86_64-suse-linux, OK for the mainline?
2015-05-11 Eric Botcazou <ebotca...@adacore.com>
* tree-inline.c: Include tree-ssa-propagate.h.
(replace_constant_uses_in): New function.
(fold_marked_statements): Call it before folding the statement.
* gimple-expr.h (is_gimple_constant): Reorder.
* tree-ssa-propagate.c (before_dom_children): Use inline accessor.
--
Eric Botcazou
Index: tree-ssa-propagate.c
===================================================================
--- tree-ssa-propagate.c (revision 222673)
+++ tree-ssa-propagate.c (working copy)
@@ -1246,9 +1246,7 @@ substitute_and_fold_dom_walker::before_d
&& gimple_call_noreturn_p (stmt))
stmts_to_fixup.safe_push (stmt);
- if (is_gimple_assign (stmt)
- && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
- == GIMPLE_SINGLE_RHS))
+ if (gimple_assign_single_p (stmt))
{
tree rhs = gimple_assign_rhs1 (stmt);
Index: tree-inline.c
===================================================================
--- tree-inline.c (revision 222673)
+++ tree-inline.c (working copy)
@@ -82,6 +82,7 @@ along with GCC; see the file COPYING3.
#include "expr.h"
#include "tree-dfa.h"
#include "tree-ssa.h"
+#include "tree-ssa-propagate.h"
#include "tree-pretty-print.h"
#include "except.h"
#include "debug.h"
@@ -4801,6 +4802,54 @@ gimple_expand_calls_inline (basic_block
return inlined;
}
+/* Replace USE references in statement STMT with constant values. Return
+ true if at least one reference was replaced.
+
+ We do it at -O0 for anonymous SSA names or SSA names of anonymous decls,
+ this makes it possible to generate reasonable code for operators that are
+ implemented as functions and inlined on constants. */
+
+static bool
+replace_constant_uses_in (gimple stmt)
+{
+ bool replaced = false;
+ use_operand_p use;
+ ssa_op_iter iter;
+
+ FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
+ {
+ tree tuse = USE_FROM_PTR (use);
+
+ if (TREE_CODE (tuse) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (tuse))
+ continue;
+
+ if (!optimize
+ && SSA_NAME_VAR (tuse)
+ && !DECL_IGNORED_P (SSA_NAME_VAR (tuse)))
+ continue;
+
+ if (!gimple_assign_single_p (SSA_NAME_DEF_STMT (tuse)))
+ continue;
+
+ tree rhs = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (tuse));
+ if (!is_gimple_constant (rhs))
+ continue;
+
+ propagate_value (use, rhs);
+
+ replaced = true;
+ }
+
+ if (replaced && gimple_assign_single_p (stmt))
+ {
+ tree rhs = gimple_assign_rhs1 (stmt);
+
+ if (TREE_CODE (rhs) == ADDR_EXPR)
+ recompute_tree_invariant_for_addr_expr (rhs);
+ }
+
+ return replaced;
+}
/* Walk all basic blocks created after FIRST and try to fold every statement
in the STATEMENTS pointer set. */
@@ -4819,7 +4868,10 @@ fold_marked_statements (int first, hash_
if (statements->contains (gsi_stmt (gsi)))
{
gimple old_stmt = gsi_stmt (gsi);
- tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
+ tree old_decl
+ = is_gimple_call (old_stmt)
+ ? gimple_call_fndecl (old_stmt) : NULL_TREE;
+ bool replaced = replace_constant_uses_in (old_stmt);
if (old_decl && DECL_BUILT_IN (old_decl))
{
@@ -4827,7 +4879,7 @@ fold_marked_statements (int first, hash_
we need to look at all of them. */
gimple_stmt_iterator i2 = gsi;
gsi_prev (&i2);
- if (fold_stmt (&gsi))
+ if (fold_stmt (&gsi) || replaced)
{
gimple new_stmt;
/* If a builtin at the end of a bb folded into nothing,
@@ -4871,7 +4923,7 @@ fold_marked_statements (int first, hash_
}
}
}
- else if (fold_stmt (&gsi))
+ else if (fold_stmt (&gsi) || replaced)
{
/* Re-read the statement from GSI as fold_stmt() may
have changed it. */
Index: gimple-expr.h
===================================================================
--- gimple-expr.h (revision 222673)
+++ gimple-expr.h (working copy)
@@ -136,9 +136,9 @@ is_gimple_constant (const_tree t)
case INTEGER_CST:
case REAL_CST:
case FIXED_CST:
- case STRING_CST:
case COMPLEX_CST:
case VECTOR_CST:
+ case STRING_CST:
return true;
default: