-/* Replaces immediate uses of VAR by REPL. */
-
-static void
-replace_immediate_uses (tree var, tree repl)
-{
- int i, j, n;
- dataflow_t df;
- tree stmt;
- bool mark_new_vars;
- ssa_op_iter iter;
- use_operand_p use_p;
-
- df = get_immediate_uses (SSA_NAME_DEF_STMT (var));
- n = num_immediate_uses (df);
-
- for (i = 0; i < n; i++)
- {
- stmt = immediate_use (df, i);
-
- if (TREE_CODE (stmt) == PHI_NODE)
- {
- for (j = 0; j < PHI_NUM_ARGS (stmt); j++)
- if (PHI_ARG_DEF (stmt, j) == var)
- {
- SET_PHI_ARG_DEF (stmt, j, repl);
- if (TREE_CODE (repl) == SSA_NAME
- && PHI_ARG_EDGE (stmt, j)->flags & EDGE_ABNORMAL)
- SSA_NAME_OCCURS_IN_ABNORMAL_PHI (repl) = 1;
- }
-
- continue;
- }
-
- get_stmt_operands (stmt);
- mark_new_vars = false;
- if (is_gimple_reg (SSA_NAME_VAR (var)))
- {
- if (TREE_CODE (stmt) == MODIFY_EXPR)
- {
- propagate_into_addr (stmt, var, &TREE_OPERAND (stmt, 0), repl);
- propagate_into_addr (stmt, var, &TREE_OPERAND (stmt, 1), repl);
- }
-
- FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
- if (USE_FROM_PTR (use_p) == var)
- {
- propagate_value (use_p, repl);
- mark_new_vars = POINTER_TYPE_P (TREE_TYPE (repl));
- }
- }
- else
- {
- FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_VIRTUAL_USES)
- if (USE_FROM_PTR (use_p) == var)
- propagate_value (use_p, repl);
- }
-
- /* FIXME. If REPL is a constant, we need to fold STMT.
- However, fold_stmt wants a pointer to the statement, because
- it may happen that it needs to replace the whole statement
- with a new expression. Since the current def-use machinery
- does not return pointers to statements, we call fold_stmt
- with the address of a local temporary, if that call changes
- the temporary then we fall on our swords.
-
- Note that all this will become unnecessary soon. This
- pass is being replaced with a proper copy propagation pass
- for 4.1 (dnovillo, 2004-09-17). */
- if (TREE_CODE (repl) != SSA_NAME)
- {
- tree tmp = stmt;
- fold_stmt (&tmp);
- if (tmp != stmt)
- abort ();
- }
-
- /* If REPL is a pointer, it may have different memory tags associated
- with it. For instance, VAR may have had a name tag while REPL
- only had a type tag. In these cases, the virtual operands (if
- any) in the statement will refer to different symbols which need
- to be renamed. */
- if (mark_new_vars)
- mark_new_vars_to_rename (stmt, vars_to_rename);
- else
- modify_stmt (stmt);
- }
-}
-
-/* Gets the value VAR is equivalent to according to EQ_TO. */
-
-static tree
-get_eq_name (tree *eq_to, tree var)
-{
- unsigned ver;
- tree val = var;
-
- while (TREE_CODE (val) == SSA_NAME)
- {
- ver = SSA_NAME_VERSION (val);
- if (!eq_to[ver])
- break;
-
- val = eq_to[ver];
- }
-
- while (TREE_CODE (var) == SSA_NAME)
- {
- ver = SSA_NAME_VERSION (var);
- if (!eq_to[ver])
- break;
-
- var = eq_to[ver];
- eq_to[ver] = val;
- }
-
- return val;
-}
-
-/* Checks whether phi node PHI is redundant and if it is, records the ssa name
- its result is redundant to to EQ_TO array. */
-
-static void
-check_phi_redundancy (tree phi, tree *eq_to)
-{
- tree val = NULL_TREE, def, res = PHI_RESULT (phi), stmt;
- unsigned i, ver = SSA_NAME_VERSION (res), n;
- dataflow_t df;
-
- /* It is unlikely that such large phi node would be redundant. */
- if (PHI_NUM_ARGS (phi) > 16)
- return;
-
- for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
- {
- def = PHI_ARG_DEF (phi, i);
-
- if (TREE_CODE (def) == SSA_NAME)
- {
- def = get_eq_name (eq_to, def);
- if (def == res)
- continue;
- }
-
- if (val
- && !operand_equal_p (val, def, 0))
- return;
-
- val = def;
- }
-
- /* At least one of the arguments should not be equal to the result, or
- something strange is happening. */
- gcc_assert (val);
-
- if (get_eq_name (eq_to, res) == val)
- return;
-
- if (!may_propagate_copy (res, val))
- return;
-
- eq_to[ver] = val;
-
- df = get_immediate_uses (SSA_NAME_DEF_STMT (res));
- n = num_immediate_uses (df);
-
- for (i = 0; i < n; i++)
- {
- stmt = immediate_use (df, i);
-
- if (TREE_CODE (stmt) == PHI_NODE)
- check_phi_redundancy (stmt, eq_to);
- }
-}
-
-/* Removes redundant phi nodes.
-
- A redundant PHI node is a PHI node where all of its PHI arguments
- are the same value, excluding any PHI arguments which are the same
- as the PHI result.
-
- A redundant PHI node is effectively a copy, so we forward copy propagate
- which removes all uses of the destination of the PHI node then
- finally we delete the redundant PHI node.
-
- Note that if we can not copy propagate the PHI node, then the PHI
- will not be removed. Thus we do not have to worry about dependencies
- between PHIs and the problems serializing PHIs into copies creates.
-
- The most important effect of this pass is to remove degenerate PHI
- nodes created by removing unreachable code. */
-
-void
-kill_redundant_phi_nodes (void)
-{
- tree *eq_to;
- unsigned i, old_num_ssa_names;
- basic_block bb;
- tree phi, var, repl, stmt;
-
- /* The EQ_TO[VER] holds the value by that the ssa name VER should be
- replaced. If EQ_TO[VER] is ssa name and it is decided to replace it by
- other value, it may be necessary to follow the chain till the final value.
- We perform path shortening (replacing the entries of the EQ_TO array with
- heads of these chains) whenever we access the field to prevent quadratic
- complexity (probably would not occur in practice anyway, but let us play
- it safe). */
- eq_to = xcalloc (num_ssa_names, sizeof (tree));
-
- /* We have had cases where computing immediate uses takes a
- significant amount of compile time. If we run into such
- problems here, we may want to only compute immediate uses for
- a subset of all the SSA_NAMEs instead of computing it for
- all of the SSA_NAMEs. */
- compute_immediate_uses (TDFA_USE_OPS | TDFA_USE_VOPS, NULL);
- old_num_ssa_names = num_ssa_names;
-
- FOR_EACH_BB (bb)
- {
- for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
- {
- var = PHI_RESULT (phi);
- check_phi_redundancy (phi, eq_to);
- }
- }
-
- /* Now propagate the values. */
- for (i = 0; i < old_num_ssa_names; i++)
- {
- if (!ssa_name (i))
- continue;
-
- repl = get_eq_name (eq_to, ssa_name (i));
- if (repl != ssa_name (i))
- replace_immediate_uses (ssa_name (i), repl);
- }
-
- /* And remove the dead phis. */
- for (i = 0; i < old_num_ssa_names; i++)
- {
- if (!ssa_name (i))
- continue;
-
- repl = get_eq_name (eq_to, ssa_name (i));
- if (repl != ssa_name (i))
- {
- stmt = SSA_NAME_DEF_STMT (ssa_name (i));
- remove_phi_node (stmt, NULL_TREE, bb_for_stmt (stmt));
- }
- }
-
- free_df ();
- free (eq_to);
-}
-
-struct tree_opt_pass pass_redundant_phi =
-{
- "redphi", /* name */
- NULL, /* gate */
- kill_redundant_phi_nodes, /* execute */
- NULL, /* sub */
- NULL, /* next */
- 0, /* static_pass_number */
- 0, /* tv_id */
- PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
- 0, /* properties_provided */
- 0, /* properties_destroyed */
- 0, /* todo_flags_start */
- TODO_dump_func | TODO_rename_vars
- | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
- 0 /* letter */
-};