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]

[PATCH] Fix PR21636, another missed CCP opportunity


This patch teaches CCP to propagate invariant addresses through address
operations derived from them like for

struct f
{
  int i;
};
int g()throw()
{
  f a;
  a.i = 1;
  f *a1 = &a;
  int *i = &a1->i;
  return *i;  /* This should be turned into a.i */
}

where we previously couldn't see tat &a1->i is constant.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.

Richard.

2008-04-30  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/21636
	* tree-ssa-ccp.c (ccp_fold): Handle &p->x with p being a
	constant address.
	(evaluate_stmt): Print the likely value.
	(ccp_visit_stmt): Avoid excessive vertical spacing.

	* gcc.dg/tree-ssa/ssa-ccp-19.c: New testcase.

Index: testsuite/gcc.dg/tree-ssa/ssa-ccp-19.c
===================================================================
*** testsuite/gcc.dg/tree-ssa/ssa-ccp-19.c	(revision 0)
--- testsuite/gcc.dg/tree-ssa/ssa-ccp-19.c	(revision 0)
***************
*** 0 ****
--- 1,16 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O -fdump-tree-ccp1" } */
+ 
+ struct f { int i; };
+ int g()
+ {
+   struct f a, *a1;
+   int *i;
+   a.i = 1;
+   a1 = &a;
+   i = &a1->i;
+   return *i;  /* This should be turned into a.i */
+ }
+ 
+ /* { dg-final { scan-tree-dump "= a.i;" "ccp1" } } */
+ /* { dg-final { cleanup-tree-dump "ccp1" } } */
Index: tree-ssa-ccp.c
===================================================================
*** tree-ssa-ccp.c	(revision 134817)
--- tree-ssa-ccp.c	(working copy)
*************** ccp_fold (tree stmt)
*** 975,980 ****
--- 975,1012 ----
    else if (kind == tcc_reference)
      return fold_const_aggregate_ref (rhs);
  
+   /* Handle propagating invariant addresses into address operations.
+      The folding we do here matches that in tree-ssa-forwprop.c.  */
+   else if (code == ADDR_EXPR)
+     {
+       tree *base;
+       base = &TREE_OPERAND (rhs, 0);
+       while (handled_component_p (*base))
+ 	base = &TREE_OPERAND (*base, 0);
+       if (TREE_CODE (*base) == INDIRECT_REF
+ 	  && TREE_CODE (TREE_OPERAND (*base, 0)) == SSA_NAME)
+ 	{
+ 	  prop_value_t *val = get_value (TREE_OPERAND (*base, 0));
+ 	  if (val->lattice_val == CONSTANT
+ 	      && TREE_CODE (val->value) == ADDR_EXPR
+ 	      && useless_type_conversion_p (TREE_TYPE (TREE_OPERAND (*base, 0)),
+ 					    TREE_TYPE (val->value))
+ 	      && useless_type_conversion_p (TREE_TYPE (*base),
+ 					    TREE_TYPE (TREE_OPERAND (val->value, 0))))
+ 	    {
+ 	      /* We need to return a new tree, not modify the IL or share
+ 		 parts of it.  So play some tricks to avoid manually
+ 		 building it.  */
+ 	      tree ret, save = *base;
+ 	      *base = TREE_OPERAND (val->value, 0);
+ 	      ret = unshare_expr (rhs);
+ 	      recompute_tree_invariant_for_addr_expr (ret);
+ 	      *base = save;
+ 	      return ret;
+ 	    }
+ 	}
+     }
+ 
    /* We may be able to fold away calls to builtin functions if their
       arguments are constants.  */
    else if (code == CALL_EXPR
*************** evaluate_stmt (tree stmt)
*** 1210,1215 ****
--- 1242,1266 ----
  
    fold_undefer_overflow_warnings (is_constant, stmt, 0);
  
+   if (dump_file && (dump_flags & TDF_DETAILS))
+     {
+       fprintf (dump_file, "which is likely ");
+       switch (likelyvalue)
+ 	{
+ 	case CONSTANT:
+ 	  fprintf (dump_file, "CONSTANT");
+ 	  break;
+ 	case UNDEFINED:
+ 	  fprintf (dump_file, "UNDEFINED");
+ 	  break;
+ 	case VARYING:
+ 	  fprintf (dump_file, "VARYING");
+ 	  break;
+ 	default:;
+ 	}
+       fprintf (dump_file, "\n");
+     }
+ 
    if (is_constant)
      {
        /* The statement produced a constant value.  */
*************** ccp_visit_stmt (tree stmt, edge *taken_e
*** 1378,1384 ****
      {
        fprintf (dump_file, "\nVisiting statement:\n");
        print_generic_stmt (dump_file, stmt, dump_flags);
-       fprintf (dump_file, "\n");
      }
  
    if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
--- 1429,1434 ----


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