This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[ast-optimizer-branch]: Simplify STMT_EXPR's
- From: Daniel Berlin <dberlin at dberlin dot org>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 2 May 2002 10:34:44 -0400 (EDT)
- Subject: [ast-optimizer-branch]: Simplify STMT_EXPR's
Transforms stmt expressions like
a
({<....>})
b
into
a
{<....>}
b
Trying to transform those that return values doesn't always work.
We generate what looks like correct code, i.e.:
a
b = ({<.....> <value>})
becomes
a
{<....> <new temporary> = <value>}
b = <new temporary>
but during rtl expansion, it aborts failing the following test in
varasm.c:
/* Check that we are not being given an automatic variable. */
/* A weak alias has TREE_PUBLIC set but not the other bits. */
if (TREE_CODE (decl) == PARM_DECL
|| TREE_CODE (decl) == RESULT_DECL
|| (TREE_CODE (decl) == VAR_DECL
&& !TREE_STATIC (decl)
&& !TREE_PUBLIC (decl)
&& !DECL_EXTERNAL (decl)
&& !DECL_REGISTER (decl)))
abort ();
So, until I figure out what to do, it only transforms those that do not
return values.
Index: c-simplify.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/c-simplify.c,v
retrieving revision 1.1.2.14
diff -c -3 -p -w -B -b -r1.1.2.14 c-simplify.c
*** c-simplify.c 1 May 2002 00:30:40 -0000 1.1.2.14
--- c-simplify.c 2 May 2002 14:30:54 -0000
*************** simplify_decl_stmt (t, post_p)
*** 879,885 ****
post_p);
DECL_INITIAL (decl) = NULL_TREE;
}
!
prev = t;
t = TREE_CHAIN (t);
}
--- 879,889 ----
post_p);
DECL_INITIAL (decl) = NULL_TREE;
}
! else if (DECL_INITIAL (decl)
! && TREE_CODE_CLASS (TREE_CODE (DECL_INITIAL (decl))) == 's')
! {
! simplify_stmt (DECL_INITIAL (decl));
! }
prev = t;
t = TREE_CHAIN (t);
}
*************** simplify_expr (expr, pre_p, post_p, simp
*** 1049,1055 ****
--- 1053,1123 ----
SIMPLE grammar. */
case STMT_EXPR:
simplify_stmt (STMT_EXPR_STMT (expr));
+
+ /* These tests determine if the STMT_EXPR returns a value. If it does, we need
+ to store that value somewhere. */
+ if (TREE_CODE (STMT_EXPR_STMT (expr)) == COMPOUND_STMT
+ && TREE_CODE (COMPOUND_BODY (STMT_EXPR_STMT (expr))) == SCOPE_STMT
+ && TREE_CODE (TREE_TYPE (expr)) != VOID_TYPE)
+ {
expr_s = expr;
+ /* This produces code that *looks* right, but there are
+ problems in duplicating the magic of STMT_EXPR's that return values.
+ In particular, the way we expand them to RTL. */
+ #if 0
+ tree body = STMT_EXPR_STMT (expr);
+ tree temp;
+ tree last;
+ tree temp2;
+ tree new_stmt;
+ tree last_expr;
+ /* Find the last statement in the statement expression. */
+ while (TREE_CODE (body) == COMPOUND_STMT)
+ {
+ body = COMPOUND_BODY (body);
+ last = TREE_CHAIN (body);
+ while (TREE_CHAIN (last))
+ {
+ temp = last;
+ last = TREE_CHAIN (last);
+ }
+ body = temp;
+ }
+ type = TREE_TYPE (EXPR_STMT_EXPR (temp));
+ tmp = create_tmp_var (type);
+ last_expr = EXPR_STMT_EXPR (temp);
+ /* last_expr might just be a simple variable, or it could be a modify_expression. */
+ TREE_ADDRESSABLE (last_expr) = 1;
+ if (TREE_CODE_CLASS (TREE_CODE (last_expr)) == 'e')
+ {
+ temp2 = build_modify_expr (tmp, NOP_EXPR, TREE_OPERAND (last_expr, 0));
+ new_stmt = build_stmt (EXPR_STMT ,temp2);
+ TREE_CHAIN (new_stmt) = TREE_CHAIN (temp);
+ TREE_CHAIN (temp) = new_stmt;
+ }
+ else
+ {
+ temp2 = build_modify_expr (tmp, NOP_EXPR, last_expr);
+ EXPR_STMT_EXPR (temp) = temp2;
+ }
+ add_tree (STMT_EXPR_STMT (expr), pre_p);
+ expr_s = TREE_OPERAND (temp2, 0);
+ #endif
+ }
+ else
+ {
+ tree temp = COMPOUND_BODY (STMT_EXPR_STMT (expr));
+ tree last = TREE_CHAIN (temp);
+ /* Find the last statement in the statement expression. */
+ while (TREE_CHAIN (last))
+ {
+ temp = last;
+ last = TREE_CHAIN (last);
+ }
+ expr_s = NULL_TREE;
+ add_tree (STMT_EXPR_STMT (expr), pre_p);
+ return expr_s;
+ }
break;
case SAVE_EXPR:
*************** static int
*** 2478,2483 ****
--- 2546,2553 ----
expr_has_effect (expr)
tree expr;
{
+ if (expr == NULL_TREE)
+ return 0;
return (TREE_SIDE_EFFECTS (expr)
|| (TREE_CODE (expr) == CONVERT_EXPR
&& VOID_TYPE_P (TREE_TYPE (expr))));