This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[ast-optimizer-branch] Fix gcc.dg/20020220-2.c [patch]
- From: Diego Novillo <dnovillo at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 12 Jun 2002 21:29:31 -0400
- Subject: [ast-optimizer-branch] Fix gcc.dg/20020220-2.c [patch]
- Organization: Red Hat Canada
This patch fixes gcc.dg/20020220-2.c by emitting warnings on
unused computations and expressions with no side effects. Since
these expressions are removed by the simplifier, the RTL expander
never got a chance to emit the warning.
Long term, we should be removing the warnings from the RTL
expander, but this can wait.
Bootstrapped on x86. Testing is currently underway. Will commit
after it's done.
Diego.
2002-06-12 Diego Novillo <dnovillo@redhat.com>
* Makefile.in (c-simplify.o): Add dependency on flags.h,
langhooks.h, toplev.h and rtl.h.
* c-simplify.c: Include flags.h, rtl.h and toplev.h.
(simplify_expr_stmt): New function.
(simplify_stmt): Call it.
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Makefile.in,v
retrieving revision 1.701.2.32
diff -d -p -d -u -p -r1.701.2.32 Makefile.in
--- Makefile.in 11 Jun 2002 13:36:32 -0000 1.701.2.32
+++ Makefile.in 13 Jun 2002 01:07:58 -0000
@@ -1384,7 +1384,8 @@ tree-optimize.o : tree-optimize.c tree-o
$(SYSTEM_H) $(RTL_H) $(TREE_H) $(TM_P_H) $(BASIC_BLOCK_H) $(EXPR_H) \
$(GGC_H) output.h diagnostic.h ssa.h errors.h flags.h
c-simplify.o : c-simplify.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) errors.h \
- $(C_TREE_H) $(C_COMMON_H) diagnostic.h tree-simple.h varray.h
+ $(C_TREE_H) $(C_COMMON_H) diagnostic.h tree-simple.h varray.h flags.h \
+ langhooks.h toplev.h rtl.h
simple-break-elim.o : simple-break-elim.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) \
$(C_TREE_H) $(C_COMMON_H) diagnostic.h tree-dchain.h
simple-goto-elim.o : simple-goto-elim.c $(CONFIG_H) $(SYSTEM_H) $(TREE_H) \
Index: c-simplify.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/c-simplify.c,v
retrieving revision 1.1.2.33
diff -d -p -d -u -p -r1.1.2.33 c-simplify.c
--- c-simplify.c 12 Jun 2002 16:07:10 -0000 1.1.2.33
+++ c-simplify.c 13 Jun 2002 01:07:59 -0000
@@ -35,6 +35,9 @@ Software Foundation, 59 Temple Place - S
#include "tree-inline.h"
#include "diagnostic.h"
#include "langhooks.h"
+#include "flags.h"
+#include "rtl.h"
+#include "toplev.h"
/** The simplification pass converts the language-dependent trees
(ld-trees) emitted by the parser into language-independent trees
@@ -56,6 +59,7 @@ Software Foundation, 59 Temple Place - S
/* Local declarations. */
static void simplify_stmt PARAMS ((tree));
+static void simplify_expr_stmt PARAMS ((tree, tree *, tree *));
static void simplify_for_stmt PARAMS ((tree, tree *));
static void simplify_while_stmt PARAMS ((tree, tree *));
static void simplify_do_stmt PARAMS ((tree));
@@ -187,11 +191,10 @@ simplify_stmt (stmt)
while (stmt && stmt != error_mark_node)
{
tree next, pre, post;
- int keep_stmt_p, stmt_was_null;
+ int keep_stmt_p;
pre = NULL;
post = NULL;
- stmt_was_null = 0;
next = TREE_CHAIN (stmt);
if (dump_file && (dump_flags & TDF_DETAILS))
@@ -230,18 +233,7 @@ simplify_stmt (stmt)
break;
case EXPR_STMT:
- /* Simplification of a statement expression will nullify the
- statement if all its side effects are moved to PRE and POST.
- In this case we will not want to emit the simplified
- statement. However, if the statement was already null before
- simplification, we should leave it to avoid changing the
- semantics of the program. */
- if (!expr_has_effect (EXPR_STMT_EXPR (stmt)))
- stmt_was_null = 1;
-
- walk_tree (&EXPR_STMT_EXPR (stmt), mostly_copy_tree_r, NULL, NULL);
- simplify_expr (&EXPR_STMT_EXPR (stmt), &pre, &post, is_simple_expr,
- stmt);
+ simplify_expr_stmt (stmt, &pre, &post);
break;
case RETURN_STMT:
@@ -295,7 +287,7 @@ simplify_stmt (stmt)
keep the original statement or not. If the statement had no
effect before simplification, we emit it anyway to avoid changing
the semantics of the original program. */
- keep_stmt_p = (stmt_was_null || stmt_has_effect (stmt));
+ keep_stmt_p = stmt_has_effect (stmt);
TREE_CHAIN (prev) = NULL_TREE;
TREE_CHAIN (stmt) = NULL_TREE;
@@ -334,6 +326,45 @@ simplify_stmt (stmt)
}
}
+/** Simplify an EXPR_STMT node.
+
+ STMT is the statement node.
+
+ PRE_P points to the list where side effects that must happen before
+ STMT should be stored.
+
+ POST_P points to the list where side effects that must happen after
+ STMT should be stored. */
+
+static void
+simplify_expr_stmt (stmt, pre_p, post_p)
+ tree stmt;
+ tree *pre_p;
+ tree *post_p;
+{
+ /* Simplification of a statement expression will nullify the
+ statement if all its side effects are moved to *PRE_P and *POST_P.
+
+ In this case we will not want to emit the simplified statement.
+ However, we may still want to emit a warning, so we do that before
+ simplification. */
+ if (extra_warnings || warn_unused_value)
+ {
+ const char *fname = DECL_SOURCE_FILE (current_function_decl);
+ int lineno = STMT_LINENO (stmt);
+
+ if (!stmt_has_effect (stmt))
+ warning_with_file_and_line (fname, lineno, "statement with no effect");
+ else if (warn_unused_value)
+ {
+ set_file_and_line_for_stmt (fname, lineno);
+ warn_if_unused_value (EXPR_STMT_EXPR (stmt));
+ }
+ }
+
+ walk_tree (&EXPR_STMT_EXPR (stmt), mostly_copy_tree_r, NULL, NULL);
+ simplify_expr (&EXPR_STMT_EXPR (stmt), pre_p, post_p, is_simple_expr, stmt);
+}
/** Simplify a FOR_STMT node. This will convert: