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] Canonicalize &a to &a[0] during fold


Hi!

This patch canoncializes array-to-pointer decay to
ADDR_EXPR(ARRAY_REF(a,0)) rather than ADDR_EXPR(a).  This will allow
simplifications to other foldings that try to fold address arithmetic
on array element pointers.  It also will simplify follow-up patches
to do more of those.

It is a general question if we want to do foldings for canonicalization
purposes only - and I suggest writing down the canonicalizations
somewhere.  Another question is wether fold operations should rely on
canonicalizations done elsewhere or just should try to match every
possible way of expressing an operation in trees?

If there is consensus, I'll try to prepare documentations of canonical
forms fold will create together with proper testcases for the
canonicalization if they do not already exist.

This patch was bootstrapped on x86_64-unknown-linux-gnu with all
languages except ada and treelang.  An earlier version of the patch
was tested with only fortran regressions, this version tested ok
on fortran, the rest of the testing is still in progress.

Ok for mainline if testing succeeds?

Any comments on the canonicalizations in fold?

Thanks,
Richard.
2005-04-24  Richard Guenther  <rguenth@gcc.gnu.org>

	* fold-const.c (fold_unary): Fold decayed array a to &a[0].

	* gcc.dg/tree-ssa/canonicalize-array-decay.c: New testcase.


Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.568
diff -c -3 -p -r1.568 fold-const.c
*** fold-const.c	23 Apr 2005 02:01:49 -0000	1.568
--- fold-const.c	24 Apr 2005 17:02:53 -0000
*************** fold_unary (enum tree_code code, tree ty
*** 6964,6969 ****
--- 6964,6984 ----
        tem = fold_convert_const (code, type, arg0);
        return tem ? tem : NULL_TREE;
  
+     case ADDR_EXPR:
+       /* Fold decayed array a to &a[0].  */
+       if (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
+ 	  && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (op0)))
+         {
+ 	  tree adomain = TYPE_DOMAIN (TREE_TYPE (op0));
+ 	  tree firsti = integer_zero_node;
+ 	  tree aref;
+ 	  if (adomain && TYPE_MIN_VALUE (adomain))
+ 	    firsti = TYPE_MIN_VALUE (adomain);
+ 	  aref = build4 (ARRAY_REF, TREE_TYPE (type),
+ 			 op0, firsti, NULL_TREE, NULL_TREE);
+ 	  return build1 (ADDR_EXPR, type, aref);
+ 	}
+       
      case VIEW_CONVERT_EXPR:
        if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
  	return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));



/* { dg-do compile } */
/* { dg-options "-O -fdump-tree-optimized" } */

int a[2];
int (*b)[2];
int *c;

void foo(void)
{
	b = &a;
	c = a;
}

/* { dg-final { scan-tree-dump "&a;" "optimized" } } */
/* { dg-final { scan-tree-dump "&a.0.;" "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */

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