/* Rename SSA copies.
- Copyright (C) 2004 Free Software Foundation, Inc.
+ Copyright (C) 2004-2013 Free Software Foundation, Inc.
Contributed by Andrew MacLeod <amacleod@redhat.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING. If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA. */
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
+#include "gimple.h"
#include "flags.h"
#include "basic-block.h"
#include "function.h"
-#include "diagnostic.h"
+#include "tree-pretty-print.h"
#include "bitmap.h"
#include "tree-flow.h"
-#include "tree-gimple.h"
+#include "gimple.h"
#include "tree-inline.h"
-#include "timevar.h"
-#include "tree-alias-common.h"
#include "hashtab.h"
-#include "tree-dump.h"
#include "tree-ssa-live.h"
#include "tree-pass.h"
+#include "langhooks.h"
-extern void rename_ssa_copies (void);
+static struct
+{
+ /* Number of copies coalesced. */
+ int coalesced;
+} stats;
/* The following routines implement the SSA copy renaming phase.
Each copy is examined to determine if it is possible to rename the base
variable of one of the operands to the same variable as the other operand.
- ie.
+ i.e.
T.3_5 = <blah>
a_1 = T.3_5
- If this copy couldn't be copy propagated, it could possibly remain in the
- program throughout the optimization phases. After SSA->normal, it would
+ If this copy couldn't be copy propagated, it could possibly remain in the
+ program throughout the optimization phases. After SSA->normal, it would
become:
T.3 = <blah>
a = T.3
-
- Since T.3_5 is distinct from all other SSA versions of T.3, there is no
- fundamental reason why the base variable needs to be T.3, subject to
- certain restrictions. This optimization attempts to determine if we can
+
+ Since T.3_5 is distinct from all other SSA versions of T.3, there is no
+ fundamental reason why the base variable needs to be T.3, subject to
+ certain restrictions. This optimization attempts to determine if we can
change the base variable on copies like this, and result in code such as:
a_5 = <blah>
a_1 = a_5
- This gives the SSA->normal pass a shot at coalescing a_1 and a_5. If it is
+ This gives the SSA->normal pass a shot at coalescing a_1 and a_5. If it is
possible, the copy goes away completely. If it isn't possible, a new temp
will be created for a_5, and you will end up with the exact same code:
a_1 = <blah>
<blah2> = a_1
- get turned into
-
+ get turned into
+
T.3_5 = <blah>
a_1 = T.3_5
<blah2> = a_1
<blah2> = a_1
which copy propagation would then turn into:
-
+
a_5 = <blah>
<blah2> = a_5
{
int p1, p2, p3;
tree root1, root2;
- var_ann_t ann1, ann2, ann3;
- bool gimp1, gimp2;
+ tree rep1, rep2;
+ bool ign1, ign2, abnorm;
-#ifdef ENABLE_CHECKING
- if (TREE_CODE (var1) != SSA_NAME || TREE_CODE (var2) != SSA_NAME)
- abort ();
-#endif
+ gcc_assert (TREE_CODE (var1) == SSA_NAME);
+ gcc_assert (TREE_CODE (var2) == SSA_NAME);
- register_ssa_partition (map, var1, false);
- register_ssa_partition (map, var2, true);
+ register_ssa_partition (map, var1);
+ register_ssa_partition (map, var2);
p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
fprintf (debug, "(P%d)", p2);
}
-#ifdef ENABLE_CHECKING
- if (p1 == NO_PARTITION || p2 == NO_PARTITION)
- abort ();
-#endif
+ gcc_assert (p1 != NO_PARTITION);
+ gcc_assert (p2 != NO_PARTITION);
- root1 = SSA_NAME_VAR (partition_to_var (map, p1));
- root2 = SSA_NAME_VAR (partition_to_var (map, p2));
-
- if (DECL_HARD_REGISTER (root1) || DECL_HARD_REGISTER (root2))
+ if (p1 == p2)
{
if (debug)
- {
- if (DECL_HARD_REGISTER (root1))
- print_generic_expr (debug, var1, TDF_SLIM);
- else
- print_generic_expr (debug, var2, TDF_SLIM);
- fprintf (debug, " is a hardware register. No Coalescing.\n");
- }
+ fprintf (debug, " : Already coalesced.\n");
return;
}
- ann1 = var_ann (root1);
- ann2 = var_ann (root2);
-
- if (p1 == p2)
+ rep1 = partition_to_var (map, p1);
+ rep2 = partition_to_var (map, p2);
+ root1 = SSA_NAME_VAR (rep1);
+ root2 = SSA_NAME_VAR (rep2);
+ if (!root1 && !root2)
+ return;
+
+ /* Don't coalesce if one of the variables occurs in an abnormal PHI. */
+ abnorm = (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep1)
+ || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep2));
+ if (abnorm)
{
if (debug)
- fprintf (debug, " : Already coalesced.\n");
+ fprintf (debug, " : Abnormal PHI barrier. No coalesce.\n");
return;
}
return;
}
- /* Never attempt to coalesce 2 difference parameters. */
- if (TREE_CODE (root1) == PARM_DECL && TREE_CODE (root2) == PARM_DECL)
+ /* Never attempt to coalesce 2 different parameters. */
+ if ((root1 && TREE_CODE (root1) == PARM_DECL)
+ && (root2 && TREE_CODE (root2) == PARM_DECL))
{
if (debug)
fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n");
return;
}
- if ((TREE_CODE (root1) == RESULT_DECL) != (TREE_CODE (root2) == RESULT_DECL))
+ if ((root1 && TREE_CODE (root1) == RESULT_DECL)
+ != (root2 && TREE_CODE (root2) == RESULT_DECL))
{
if (debug)
fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n");
return;
}
- gimp1 = is_gimple_tmp_var (root1);
- gimp2 = is_gimple_tmp_var (root2);
+ ign1 = !root1 || (TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1));
+ ign2 = !root2 || (TREE_CODE (root2) == VAR_DECL && DECL_IGNORED_P (root2));
- /* Never attempt to coalesce 2 user variables unless one is an inline
- variable. */
- if (!gimp1 && !gimp2)
+ /* Refrain from coalescing user variables, if requested. */
+ if (!ign1 && !ign2)
{
- if (DECL_FROM_INLINE (root2))
- gimp2 = true;
+ if (flag_ssa_coalesce_vars && DECL_FROM_INLINE (root2))
+ ign2 = true;
+ else if (flag_ssa_coalesce_vars && DECL_FROM_INLINE (root1))
+ ign1 = true;
+ else if (flag_ssa_coalesce_vars != 2)
+ {
+ if (debug)
+ fprintf (debug, " : 2 different USER vars. No coalesce.\n");
+ return;
+ }
else
- if (DECL_FROM_INLINE (root1))
- gimp1 = true;
- else
- {
- if (debug)
- fprintf (debug, " : 2 different USER vars. No coalesce.\n");
- return;
- }
+ ign2 = true;
}
-
- /* Don't coalesce if there are two different memory tags. */
- if (ann1->type_mem_tag && ann2->type_mem_tag
- && ann1->type_mem_tag != ann2->type_mem_tag)
- {
- if (debug)
- fprintf (debug, " : 2 memory tags. No coalesce.\n");
- return;
- }
-
- /* If both values have default defs, we can't coalesce. If only one has a
+ /* If both values have default defs, we can't coalesce. If only one has a
tag, make sure that variable is the new root partition. */
- if (default_def (root1))
+ if (root1 && ssa_default_def (cfun, root1))
{
- if (default_def (root2))
+ if (root2 && ssa_default_def (cfun, root2))
{
if (debug)
fprintf (debug, " : 2 default defs. No coalesce.\n");
}
else
{
- gimp2 = true;
- gimp1 = false;
+ ign2 = true;
+ ign1 = false;
}
}
- else
- if (default_def (root2))
- {
- gimp1 = true;
- gimp2 = false;
- }
+ else if (root2 && ssa_default_def (cfun, root2))
+ {
+ ign1 = true;
+ ign2 = false;
+ }
+
+ /* Do not coalesce if we cannot assign a symbol to the partition. */
+ if (!(!ign2 && root2)
+ && !(!ign1 && root1))
+ {
+ if (debug)
+ fprintf (debug, " : Choosen variable has no root. No coalesce.\n");
+ return;
+ }
+
+ /* Don't coalesce if the new chosen root variable would be read-only.
+ If both ign1 && ign2, then the root var of the larger partition
+ wins, so reject in that case if any of the root vars is TREE_READONLY.
+ Otherwise reject only if the root var, on which replace_ssa_name_symbol
+ will be called below, is readonly. */
+ if (((root1 && TREE_READONLY (root1)) && ign2)
+ || ((root2 && TREE_READONLY (root2)) && ign1))
+ {
+ if (debug)
+ fprintf (debug, " : Readonly variable. No coalesce.\n");
+ return;
+ }
+
+ /* Don't coalesce if the two variables aren't type compatible . */
+ if (!types_compatible_p (TREE_TYPE (var1), TREE_TYPE (var2))
+ /* There is a disconnect between the middle-end type-system and
+ VRP, avoid coalescing enum types with different bounds. */
+ || ((TREE_CODE (TREE_TYPE (var1)) == ENUMERAL_TYPE
+ || TREE_CODE (TREE_TYPE (var2)) == ENUMERAL_TYPE)
+ && TREE_TYPE (var1) != TREE_TYPE (var2)))
+ {
+ if (debug)
+ fprintf (debug, " : Incompatible types. No coalesce.\n");
+ return;
+ }
/* Merge the two partitions. */
p3 = partition_union (map->var_partition, p1, p2);
- /* Set the root variable of the partition to the better choice, if there is
+ /* Set the root variable of the partition to the better choice, if there is
one. */
- if (!gimp2)
- SSA_NAME_VAR (partition_to_var (map, p3)) = root2;
- else
- if (!gimp1)
- SSA_NAME_VAR (partition_to_var (map, p3)) = root1;
-
- /* Update the various flag widgitry of the current base representative. */
- ann3 = var_ann (SSA_NAME_VAR (partition_to_var (map, p3)));
- if (ann1->type_mem_tag)
- ann3->type_mem_tag = ann1->type_mem_tag;
+ if (!ign2 && root2)
+ replace_ssa_name_symbol (partition_to_var (map, p3), root2);
+ else if (!ign1 && root1)
+ replace_ssa_name_symbol (partition_to_var (map, p3), root1);
else
- ann3->type_mem_tag = ann2->type_mem_tag;
+ gcc_unreachable ();
if (debug)
{
fprintf (debug, " --> P%d ", p3);
- print_generic_expr (debug, SSA_NAME_VAR (partition_to_var (map, p3)),
+ print_generic_expr (debug, SSA_NAME_VAR (partition_to_var (map, p3)),
TDF_SLIM);
fprintf (debug, "\n");
}
/* This function will make a pass through the IL, and attempt to coalesce any
SSA versions which occur in PHI's or copies. Coalescing is accomplished by
- changing the underlying root variable of all coalesced version. This will
- then cause the SSA->normal pass to attempt to coalesce them all to the same
+ changing the underlying root variable of all coalesced version. This will
+ then cause the SSA->normal pass to attempt to coalesce them all to the same
variable. */
-void
+static unsigned int
rename_ssa_copies (void)
{
var_map map;
basic_block bb;
- block_stmt_iterator bsi;
- tree phi, stmt, var, part_var;
+ gimple_stmt_iterator gsi;
+ tree var, part_var;
+ gimple stmt, phi;
unsigned x;
FILE *debug;
+ memset (&stats, 0, sizeof (stats));
+
if (dump_file && (dump_flags & TDF_DETAILS))
debug = dump_file;
else
debug = NULL;
- map = init_var_map (num_ssa_names + 1);
+ map = init_var_map (num_ssa_names);
FOR_EACH_BB (bb)
{
/* Scan for real copies. */
- for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+ for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
- stmt = bsi_stmt (bsi);
- if (TREE_CODE (stmt) == MODIFY_EXPR)
+ stmt = gsi_stmt (gsi);
+ if (gimple_assign_ssa_name_copy_p (stmt))
{
- tree lhs = TREE_OPERAND (stmt, 0);
- tree rhs = TREE_OPERAND (stmt, 1);
+ tree lhs = gimple_assign_lhs (stmt);
+ tree rhs = gimple_assign_rhs1 (stmt);
- if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
- copy_rename_partition_coalesce (map, lhs, rhs, debug);
+ copy_rename_partition_coalesce (map, lhs, rhs, debug);
}
}
}
FOR_EACH_BB (bb)
{
/* Treat PHI nodes as copies between the result and each argument. */
- for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+ for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
- int i;
- tree res = PHI_RESULT (phi);
+ size_t i;
+ tree res;
+
+ phi = gsi_stmt (gsi);
+ res = gimple_phi_result (phi);
/* Do not process virtual SSA_NAMES. */
- if (!is_gimple_reg (SSA_NAME_VAR (res)))
+ if (virtual_operand_p (res))
continue;
- for (i = 0; i < PHI_NUM_ARGS (phi); i++)
- {
- tree arg = PHI_ARG_DEF (phi, i);
- if (TREE_CODE (arg) == SSA_NAME)
- copy_rename_partition_coalesce (map, res, arg, debug);
- }
+ /* Make sure to only use the same partition for an argument
+ as the result but never the other way around. */
+ if (SSA_NAME_VAR (res)
+ && !DECL_IGNORED_P (SSA_NAME_VAR (res)))
+ for (i = 0; i < gimple_phi_num_args (phi); i++)
+ {
+ tree arg = PHI_ARG_DEF (phi, i);
+ if (TREE_CODE (arg) == SSA_NAME)
+ copy_rename_partition_coalesce (map, res, arg,
+ debug);
+ }
+ /* Else if all arguments are in the same partition try to merge
+ it with the result. */
+ else
+ {
+ int all_p_same = -1;
+ int p = -1;
+ for (i = 0; i < gimple_phi_num_args (phi); i++)
+ {
+ tree arg = PHI_ARG_DEF (phi, i);
+ if (TREE_CODE (arg) != SSA_NAME)
+ {
+ all_p_same = 0;
+ break;
+ }
+ else if (all_p_same == -1)
+ {
+ p = partition_find (map->var_partition,
+ SSA_NAME_VERSION (arg));
+ all_p_same = 1;
+ }
+ else if (all_p_same == 1
+ && p != partition_find (map->var_partition,
+ SSA_NAME_VERSION (arg)))
+ {
+ all_p_same = 0;
+ break;
+ }
+ }
+ if (all_p_same == 1)
+ copy_rename_partition_coalesce (map, res,
+ PHI_ARG_DEF (phi, 0),
+ debug);
+ }
}
}
/* Now one more pass to make all elements of a partition share the same
root variable. */
-
- for (x = 1; x <= num_ssa_names; x++)
+
+ for (x = 1; x < num_ssa_names; x++)
{
part_var = partition_to_var (map, x);
if (!part_var)
continue;
- var = map->partition_to_var[x];
+ var = ssa_name (x);
+ if (SSA_NAME_VAR (var) == SSA_NAME_VAR (part_var))
+ continue;
if (debug)
{
- if (SSA_NAME_VAR (var) != SSA_NAME_VAR (part_var))
- {
- fprintf (debug, "Coalesced ");
- print_generic_expr (debug, var, TDF_SLIM);
- fprintf (debug, " to ");
- print_generic_expr (debug, part_var, TDF_SLIM);
- fprintf (debug, "\n");
- }
+ fprintf (debug, "Coalesced ");
+ print_generic_expr (debug, var, TDF_SLIM);
+ fprintf (debug, " to ");
+ print_generic_expr (debug, part_var, TDF_SLIM);
+ fprintf (debug, "\n");
}
- SSA_NAME_VAR (var) = SSA_NAME_VAR (part_var);
+ stats.coalesced++;
+ replace_ssa_name_symbol (var, SSA_NAME_VAR (part_var));
}
+ statistics_counter_event (cfun, "copies coalesced",
+ stats.coalesced);
delete_var_map (map);
+ return 0;
}
/* Return true if copy rename is to be performed. */
return flag_tree_copyrename != 0;
}
-struct tree_opt_pass pass_rename_ssa_copies =
-{
+struct gimple_opt_pass pass_rename_ssa_copies =
+{
+ {
+ GIMPLE_PASS,
"copyrename", /* name */
+ OPTGROUP_NONE, /* optinfo_flags */
gate_copyrename, /* gate */
rename_ssa_copies, /* execute */
NULL, /* sub */
PROP_cfg | PROP_ssa, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
- 0, /* todo_flags_start */
- TODO_dump_func | TODO_verify_ssa /* todo_flags_finish */
-};
-
+ 0, /* todo_flags_start */
+ TODO_verify_ssa /* todo_flags_finish */
+ }
+};