aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2004-10-23 16:44:44 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-07 21:03:42 -0700
commita7d01832386064ee425f2bc6866198950faaecc7 (patch)
tree4f3fac6c4a79ba65b9eaba142a771a6f57b9cbdc /expand.c
parentMake "show_bb()" show the source location of the basic blocks (diff)
downloadsparse-a7d01832386064ee425f2bc6866198950faaecc7.tar.gz
sparse-a7d01832386064ee425f2bc6866198950faaecc7.tar.bz2
sparse-a7d01832386064ee425f2bc6866198950faaecc7.zip
Do some trivial statement simplification.
Turn a trivial STMT_COMPOUND into its single statement. And if, as a result, we can turn a EXPR_STATEMENT of a STMT_EXPRESSION into it's simple expression, all the better.
Diffstat (limited to 'expand.c')
-rw-r--r--expand.c70
1 files changed, 52 insertions, 18 deletions
diff --git a/expand.c b/expand.c
index 968abeb..e3bbc66 100644
--- a/expand.c
+++ b/expand.c
@@ -831,8 +831,14 @@ static int expand_expression(struct expression *expr)
case EXPR_CONDITIONAL:
return expand_conditional(expr);
- case EXPR_STATEMENT:
- return expand_statement(expr->statement);
+ case EXPR_STATEMENT: {
+ struct statement *stmt = expr->statement;
+ int cost = expand_statement(stmt);
+
+ if (stmt->type == STMT_EXPRESSION)
+ *expr = *stmt->expression;
+ return cost;
+ }
case EXPR_LABEL:
return 0;
@@ -925,6 +931,48 @@ static int expand_if_statement(struct statement *stmt)
return SIDE_EFFECTS;
}
+/*
+ * Expanding a compound statement is really just
+ * about adding up the costs of each individual
+ * statement.
+ *
+ * We also collapse a simple compound statement:
+ * this would trigger for simple inline functions,
+ * except we would have to check the "return"
+ * symbol usage. Next time.
+ */
+static int expand_compound(struct statement *stmt)
+{
+ struct symbol *sym;
+ struct statement *s, *last;
+ int cost, symbols, statements;
+
+ symbols = 0;
+ FOR_EACH_PTR(stmt->syms, sym) {
+ symbols++;
+ expand_symbol(sym);
+ } END_FOR_EACH_PTR(sym);
+
+ if (stmt->ret) {
+ symbols++;
+ expand_symbol(stmt->ret);
+ }
+
+ cost = 0;
+ last = NULL;
+ statements = 0;
+ FOR_EACH_PTR(stmt->stmts, s) {
+ statements++;
+ last = s;
+ cost += expand_statement(s);
+ } END_FOR_EACH_PTR(s);
+
+ if (!symbols && statements == 1)
+ *stmt = *last;
+
+ return cost;
+}
+
static int expand_statement(struct statement *stmt)
{
if (!stmt)
@@ -938,22 +986,8 @@ static int expand_statement(struct statement *stmt)
case STMT_EXPRESSION:
return expand_expression(stmt->expression);
- case STMT_COMPOUND: {
- struct symbol *sym;
- struct statement *s;
- int cost;
-
- FOR_EACH_PTR(stmt->syms, sym) {
- expand_symbol(sym);
- } END_FOR_EACH_PTR(sym);
- expand_symbol(stmt->ret);
-
- cost = 0;
- FOR_EACH_PTR(stmt->stmts, s) {
- cost += expand_statement(s);
- } END_FOR_EACH_PTR(s);
- return cost;
- }
+ case STMT_COMPOUND:
+ return expand_compound(stmt);
case STMT_IF:
return expand_if_statement(stmt);