OSDN Git Service

2009-09-14 Sebastian Pop <sebastian.pop@amd.com>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-copyrename.c
index db85d9c..ed221c2 100644 (file)
@@ -1,12 +1,12 @@
 /* Rename SSA copies.
 /* Rename SSA copies.
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2006, 2007, 2008 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
    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,
 any later version.
 
 GCC is distributed in the hope that it will be useful,
@@ -15,33 +15,30 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 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 "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 "bitmap.h"
 #include "tree-flow.h"
 #include "flags.h"
 #include "basic-block.h"
 #include "function.h"
 #include "diagnostic.h"
 #include "bitmap.h"
 #include "tree-flow.h"
-#include "tree-gimple.h"
+#include "gimple.h"
 #include "tree-inline.h"
 #include "timevar.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"
 
 #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);
-
 /* The following routines implement the SSA copy renaming phase.
 
    This optimization looks for copies between 2 SSA_NAMES, either through a
 /* The following routines implement the SSA copy renaming phase.
 
    This optimization looks for copies between 2 SSA_NAMES, either through a
@@ -49,7 +46,7 @@ extern void rename_ssa_copies (void);
 
    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.
 
    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
 
    T.3_5 = <blah>
    a_1 = T.3_5
 
@@ -112,21 +109,19 @@ extern void rename_ssa_copies (void);
 /* Coalesce the partitions in MAP representing VAR1 and VAR2 if it is valid.
    Choose a representative for the partition, and send debug info to DEBUG.  */
 
 /* Coalesce the partitions in MAP representing VAR1 and VAR2 if it is valid.
    Choose a representative for the partition, and send debug info to DEBUG.  */
 
-static void
+static bool
 copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
 {
   int p1, p2, p3;
   tree root1, root2;
 copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
 {
   int p1, p2, p3;
   tree root1, root2;
-  var_ann_t ann1, ann2, ann3;
-  bool ign1, ign2;
+  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));
 
   p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
   p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
@@ -140,35 +135,29 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
       fprintf (debug, "(P%d)", p2);
     }
 
       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));
+  rep1 = partition_to_var (map, p1);
+  rep2 = partition_to_var (map, p2);
+  root1 = SSA_NAME_VAR (rep1);
+  root2 = SSA_NAME_VAR (rep2);
 
 
-  if (DECL_HARD_REGISTER (root1) || DECL_HARD_REGISTER (root2))
+  if (p1 == p2)
     {
       if (debug)
     {
       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");
-       }
-      return;
+       fprintf (debug, " : Already coalesced.\n");
+      return false;
     }
 
     }
 
-  ann1 = var_ann (root1);
-  ann2 = var_ann (root2);
-
-  if (p1 == p2)
+  /* 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)
     {
       if (debug)
-       fprintf (debug, " : Already coalesced.\n");
-      return;
+       fprintf (debug, " : Abnormal PHI barrier.  No coalesce.\n");
+      return false;
     }
 
   /* Partitions already have the same root, simply merge them.  */
     }
 
   /* Partitions already have the same root, simply merge them.  */
@@ -177,7 +166,7 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
       p1 = partition_union (map->var_partition, p1, p2);
       if (debug)
        fprintf (debug, " : Same root, coalesced --> P%d.\n", p1);
       p1 = partition_union (map->var_partition, p1, p2);
       if (debug)
        fprintf (debug, " : Same root, coalesced --> P%d.\n", p1);
-      return;
+      return false;
     }
 
   /* Never attempt to coalesce 2 difference parameters.  */
     }
 
   /* Never attempt to coalesce 2 difference parameters.  */
@@ -185,14 +174,14 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
     {
       if (debug)
         fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n");
     {
       if (debug)
         fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n");
-      return;
+      return false;
     }
 
   if ((TREE_CODE (root1) == RESULT_DECL) != (TREE_CODE (root2) == RESULT_DECL))
     {
       if (debug)
         fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n");
     }
 
   if ((TREE_CODE (root1) == RESULT_DECL) != (TREE_CODE (root2) == RESULT_DECL))
     {
       if (debug)
         fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n");
-      return;
+      return false;
     }
 
   ign1 = TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1);
     }
 
   ign1 = TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1);
@@ -210,28 +199,19 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
        {
          if (debug)
            fprintf (debug, " : 2 different USER vars. No coalesce.\n");
        {
          if (debug)
            fprintf (debug, " : 2 different USER vars. No coalesce.\n");
-         return;
+         return false;
        }
     }
 
        }
     }
 
-  /* 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 
      tag, make sure that variable is the new root partition.  */
   /* 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 (gimple_default_def (cfun, root1))
     {
     {
-      if (default_def (root2))
+      if (gimple_default_def (cfun, root2))
        {
          if (debug)
            fprintf (debug, " : 2 default defs. No coalesce.\n");
        {
          if (debug)
            fprintf (debug, " : 2 default defs. No coalesce.\n");
-         return;
+         return false;
        }
       else
         {
        }
       else
         {
@@ -239,18 +219,18 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
          ign1 = false;
        }
     }
          ign1 = false;
        }
     }
-  else if (default_def (root2))
+  else if (gimple_default_def (cfun, root2))
     {
       ign1 = true;
       ign2 = false;
     }
 
   /* Don't coalesce if the two variables aren't type compatible.  */
     {
       ign1 = true;
       ign2 = false;
     }
 
   /* Don't coalesce if the two variables aren't type compatible.  */
-  if (!lang_hooks.types_compatible_p (TREE_TYPE (root1), TREE_TYPE (root2)))
+  if (!types_compatible_p (TREE_TYPE (root1), TREE_TYPE (root2)))
     {
       if (debug)
        fprintf (debug, " : Incompatible types.  No coalesce.\n");
     {
       if (debug)
        fprintf (debug, " : Incompatible types.  No coalesce.\n");
-      return;
+      return false;
     }
 
   /* Merge the two partitions.  */
     }
 
   /* Merge the two partitions.  */
@@ -263,13 +243,6 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
   else if (!ign1)
     replace_ssa_name_symbol (partition_to_var (map, p3), root1);
 
   else if (!ign1)
     replace_ssa_name_symbol (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;
-  else
-    ann3->type_mem_tag = ann2->type_mem_tag;
-
   if (debug)
     {
       fprintf (debug, " --> P%d ", p3);
   if (debug)
     {
       fprintf (debug, " --> P%d ", p3);
@@ -277,6 +250,7 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
                          TDF_SLIM);
       fprintf (debug, "\n");
     }
                          TDF_SLIM);
       fprintf (debug, "\n");
     }
+  return true;
 }
 
 
 }
 
 
@@ -286,36 +260,37 @@ copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
    then cause the SSA->normal pass to attempt to coalesce them all to the same 
    variable.  */
 
    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;
 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;
   unsigned x;
   FILE *debug;
+  bool updated = false;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     debug = dump_file;
   else
     debug = NULL;
 
 
   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_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);
+             updated |= copy_rename_partition_coalesce (map, lhs, rhs, debug);
            }
        }
     }
            }
        }
     }
@@ -323,20 +298,23 @@ rename_ssa_copies (void)
   FOR_EACH_BB (bb)
     {
       /* Treat PHI nodes as copies between the result and each argument.  */
   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)))
            continue;
 
 
          /* Do not process virtual SSA_NAMES.  */
          if (!is_gimple_reg (SSA_NAME_VAR (res)))
            continue;
 
-          for (i = 0; i < PHI_NUM_ARGS (phi); i++)
+          for (i = 0; i < gimple_phi_num_args (phi); i++)
             {
             {
-              tree arg = PHI_ARG_DEF (phi, i);
+              tree arg = gimple_phi_arg (phi, i)->def;
               if (TREE_CODE (arg) == SSA_NAME)
               if (TREE_CODE (arg) == SSA_NAME)
-               copy_rename_partition_coalesce (map, res, arg, debug);
+               updated |= copy_rename_partition_coalesce (map, res, arg, debug);
             }
         }
     }
             }
         }
     }
@@ -347,12 +325,12 @@ rename_ssa_copies (void)
   /* Now one more pass to make all elements of a partition share the same
      root variable.  */
   
   /* 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;
     {
       part_var = partition_to_var (map, x);
       if (!part_var)
         continue;
-      var = map->partition_to_var[x];
+      var = ssa_name (x);
       if (debug)
         {
          if (SSA_NAME_VAR (var) != SSA_NAME_VAR (part_var))
       if (debug)
         {
          if (SSA_NAME_VAR (var) != SSA_NAME_VAR (part_var))
@@ -368,6 +346,7 @@ rename_ssa_copies (void)
     }
 
   delete_var_map (map);
     }
 
   delete_var_map (map);
+  return updated ? TODO_remove_unused_locals : 0;
 }
 
 /* Return true if copy rename is to be performed.  */
 }
 
 /* Return true if copy rename is to be performed.  */
@@ -378,8 +357,10 @@ gate_copyrename (void)
   return flag_tree_copyrename != 0;
 }
 
   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 */
   gate_copyrename,                     /* gate */
   rename_ssa_copies,                   /* execute */
   "copyrename",                                /* name */
   gate_copyrename,                     /* gate */
   rename_ssa_copies,                   /* execute */
@@ -387,11 +368,10 @@ struct tree_opt_pass pass_rename_ssa_copies =
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
   TV_TREE_COPY_RENAME,                 /* tv_id */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
   TV_TREE_COPY_RENAME,                 /* tv_id */
-  PROP_cfg | PROP_ssa | PROP_alias,    /* properties_required */
+  PROP_cfg | PROP_ssa,                 /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */ 
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */ 
-  TODO_dump_func | TODO_verify_ssa,     /* todo_flags_finish */
-  0                                    /* letter */
+  TODO_dump_func | TODO_verify_ssa      /* todo_flags_finish */
+ }
 }; 
 }; 
-