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] Make some tree-walking functions tail recursive


Hello,

I keep running out of stack when compiling some huge functions (like
20001226-1.c).  This patch fixes it by making walk_tree and
record_in_finally_tree to use tail recursion.  Bootstrapped and
regtested on i686.

Zdenek

	* tree-inline.c (walk_tree): Tail recursion optimized for COMPOUND_EXPRs.
	* tree-eh.c (tree-eh.c): Ditto.

Index: tree-inline.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-inline.c,v
retrieving revision 1.26.2.57
diff -c -3 -p -r1.26.2.57 tree-inline.c
*** tree-inline.c	30 Oct 2003 02:49:41 -0000	1.26.2.57
--- tree-inline.c	1 Nov 2003 19:31:35 -0000
*************** walk_tree (tree *tp, walk_tree_fn func, 
*** 1887,1894 ****
  	--len;
        /* Go through the subtrees.  We need to do this in forward order so
           that the scope of a FOR_EXPR is handled properly.  */
!       for (i = 0; i < len; ++i)
  	WALK_SUBTREE (TREE_OPERAND (*tp, i));
  
        if (code == BIND_EXPR)
  	{
--- 1887,1904 ----
  	--len;
        /* Go through the subtrees.  We need to do this in forward order so
           that the scope of a FOR_EXPR is handled properly.  */
!       for (i = 0; i < len - 1; ++i)
  	WALK_SUBTREE (TREE_OPERAND (*tp, i));
+ 
+       if (len)
+ 	{
+ 	  /* The common case is that we may tail recurse here.  */
+ 	  if (code != BIND_EXPR
+ 	      && !TREE_CHAIN (*tp))
+ 	    WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
+ 	  else
+ 	    WALK_SUBTREE (TREE_OPERAND (*tp, len - 1));
+ 	}
  
        if (code == BIND_EXPR)
  	{
Index: tree-eh.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-eh.c,v
retrieving revision 1.1.2.10
diff -c -3 -p -r1.1.2.10 tree-eh.c
*** tree-eh.c	30 Oct 2003 02:49:41 -0000	1.1.2.10
--- tree-eh.c	1 Nov 2003 19:31:35 -0000
*************** record_in_finally_tree (tree child, tree
*** 146,151 ****
--- 146,153 ----
  static void
  collect_finally_tree (tree t, tree region)
  {
+ tailrecurse:
+ 
    switch (TREE_CODE (t))
      {
      case LABEL_EXPR:
*************** collect_finally_tree (tree t, tree regio
*** 168,175 ****
      case COMPOUND_EXPR:
      case TRY_CATCH_EXPR:
        collect_finally_tree (TREE_OPERAND (t, 0), region);
!       collect_finally_tree (TREE_OPERAND (t, 1), region);
!       break;
      case CATCH_EXPR:
        collect_finally_tree (CATCH_BODY (t), region);
        break;
--- 170,177 ----
      case COMPOUND_EXPR:
      case TRY_CATCH_EXPR:
        collect_finally_tree (TREE_OPERAND (t, 0), region);
!       t = TREE_OPERAND (t, 1);
!       goto tailrecurse;
      case CATCH_EXPR:
        collect_finally_tree (CATCH_BODY (t), region);
        break;


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