This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[tree-ssa] Really remove unused variables


Hello,

there is a small thinko in my current code to remove unused variables --
their detection is based on the variable annotations that no longer
exist when the variables are expanded.  This patch fixes it.

Zdenek

	* tree-flow.h (remove_useless_vars): Declare.
	* tree-ssa.c (rewrite_out_of_ssa): Call it.
	* gimple-low.c (expand_var_p, remove_useless_vars): New.
	(expand_used_vars): Expand all variables in the
	cfun->unexpanded_var_list.

Index: tree-flow.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-flow.h,v
retrieving revision 1.1.4.142
diff -c -3 -p -r1.1.4.142 tree-flow.h
*** tree-flow.h	8 Nov 2003 09:49:19 -0000	1.1.4.142
--- tree-flow.h	8 Nov 2003 20:34:59 -0000
*************** extern void mark_new_vars_to_rename (tre
*** 507,512 ****
--- 508,514 ----
  /* In gimple-low.c  */
  void lower_function_body (tree *);
  void expand_used_vars (void);
+ void remove_useless_vars (void);
  void record_vars (tree);
  
  /* In tree-ssa.c  */
Index: tree-ssa.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-ssa.c,v
retrieving revision 1.1.4.146
diff -c -3 -p -r1.1.4.146 tree-ssa.c
*** tree-ssa.c	8 Nov 2003 18:17:28 -0000	1.1.4.146
--- tree-ssa.c	8 Nov 2003 20:34:59 -0000
*************** rewrite_out_of_ssa (tree fndecl, enum tr
*** 1888,1894 ****
  
    /* Do some cleanups which reduce the amount of data the
       tree->rtl expanders deal with.  */
    remove_useless_stmts_and_vars (&DECL_SAVED_TREE (fndecl), true);
  
    /* Debugging dumps.  */
    if (dump_file)
--- 1888,1897 ----
  
    /* Do some cleanups which reduce the amount of data the
       tree->rtl expanders deal with.  */
    remove_useless_stmts_and_vars (&DECL_SAVED_TREE (fndecl), true);
+ 
+   /* Remove unnecesary variables.  */
+   remove_useless_vars ();
  
    /* Debugging dumps.  */
    if (dump_file)
Index: gimple-low.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/gimple-low.c,v
retrieving revision 1.1.4.7
diff -c -3 -p -r1.1.4.7 gimple-low.c
*** gimple-low.c	8 Nov 2003 09:49:19 -0000	1.1.4.7
--- gimple-low.c	8 Nov 2003 20:41:55 -0000
*************** static void lower_stmt_body (tree *, str
*** 51,56 ****
--- 51,57 ----
  static void lower_stmt (tree_stmt_iterator *, struct lower_data *);
  static void lower_bind_expr (tree_stmt_iterator *, struct lower_data *);
  static void lower_cond_expr (tree_stmt_iterator *, struct lower_data *);
+ static bool expand_var_p (tree);
  
  /* Lowers the BODY.  */
  void
*************** lower_cond_expr (tree_stmt_iterator *tsi
*** 257,294 ****
      tsi_link_after (tsi, end_label, TSI_CONTINUE_LINKING);
  }
  
! /* Expand those variables in the unexpanded_var_list that are used.  */
  
! void
! expand_used_vars (void)
  {
!   tree var, cell;
  
!   cfun->unexpanded_var_list = nreverse (cfun->unexpanded_var_list);
  
!   for (cell = cfun->unexpanded_var_list; cell; cell = TREE_CHAIN (cell))
!     {
!       var = TREE_VALUE (cell);
  
!       if (TREE_CODE (var) == VAR_DECL)
! 	{
! 	  struct var_ann_d *ann = var_ann (var);
  
! 	  /* Remove all unused, unaliased temporaries.  Also remove
! 	     unused, unaliased local variables during highly
! 	     optimizing compilations.  */
! 	  ann = var_ann (var);
! 	  if (ann
! 	      && ! ann->may_aliases
! 	      && ! ann->used
! 	      && ! ann->has_hidden_use
! 	      && ! TREE_ADDRESSABLE (var)
! 	      && (DECL_ARTIFICIAL (var) || optimize >= 2))
! 	    continue;
  	}
  
!       expand_var (var);
      }
  
    cfun->unexpanded_var_list = NULL_TREE;
  }
--- 258,321 ----
      tsi_link_after (tsi, end_label, TSI_CONTINUE_LINKING);
  }
  
! /* Check whether to expand a variable VAR.  */
  
! static bool
! expand_var_p (tree var)
  {
!   struct var_ann_d *ann;
  
!   if (TREE_CODE (var) != VAR_DECL)
!     return true;
  
!   ann = var_ann (var);
  
!   /* Remove all unused, unaliased temporaries.  Also remove unused, unaliased
!      local variables during highly optimizing compilations.  */
!   ann = var_ann (var);
!   if (ann
!       && ! ann->may_aliases
!       && ! ann->used
!       && ! ann->has_hidden_use
!       && ! TREE_ADDRESSABLE (var)
!       && (DECL_ARTIFICIAL (var) || optimize >= 2))
!     return false;
! 
!   return true;
! }
! 
! /* Throw away variables that are unused.  */
! 
! void
! remove_useless_vars (void)
! {
!   tree var, *cell;
  
!   for (cell = &cfun->unexpanded_var_list; *cell; )
!     {
!       var = TREE_VALUE (*cell);
! 
!       if (!expand_var_p (var))
!       	{
! 	  *cell = TREE_CHAIN (*cell);
! 	  continue;
  	}
  
!       cell = &TREE_CHAIN (*cell);
      }
+ }
+ 
+ /* Expand variables in the unexpanded_var_list.  */
+ 
+ void
+ expand_used_vars (void)
+ {
+   tree cell;
+ 
+   cfun->unexpanded_var_list = nreverse (cfun->unexpanded_var_list);
+ 
+   for (cell = cfun->unexpanded_var_list; cell; cell = TREE_CHAIN (cell))
+     expand_var (TREE_VALUE (cell));
  
    cfun->unexpanded_var_list = NULL_TREE;
  }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]