This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[tree-ssa] Optimizing silly ELSE clauses
- From: law at redhat dot com
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 14 Aug 2003 09:23:09 -0600
- Subject: [tree-ssa] Optimizing silly ELSE clauses
- Reply-to: law at redhat dot com
This one was a total surprise... I was reviewing cases where cse1's
path following code was finding optimization opportunities and bumped
into this one several times.
What's happening is we have something like this:
if (var.1)
{
...
var.2 = ...
...
}
else
{
(void)0;
}
var.3 = PHI (var.2, 0);
ie, if we reach the PHI via the ELSE clause then we want var.3 to have
the value zero, otherwise we want it to have the value var.2.
Seeing this kind of construct actually tells us that the const/copy
propagation in the dominator optimizer is doing its job reasonably well.
Anyway when we translate that back into normal form we get something like
this:
if (var)
{
...
var = ...
...
}
else
{
var = 0;
}
It's not terribly difficult to see that the var = 0 statement, is totally
unnecessary since var will have the value zero at that point anyway.
However, this is somewhat difficult to teach to the out-of-ssa pass.
So instead we just have final cleanup phase cleanup such constructs. Which
in turn means that we don't have to have the path-following code in cse1
do the work.
* tree-cfg.c (remove_useless_stmts_and_vars): For a COND_EXPR
where the condition is a variable and the ELSE clause merely
sets that variable to zero, remove the ELSE clause.
Index: tree-cfg.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-cfg.c,v
retrieving revision 1.1.4.145
diff -c -3 -p -r1.1.4.145 tree-cfg.c
*** tree-cfg.c 13 Aug 2003 19:56:51 -0000 1.1.4.145
--- tree-cfg.c 14 Aug 2003 13:20:57 -0000
*************** remove_useless_stmts_and_vars (tree *fir
*** 1581,1586 ****
--- 1581,1593 ----
*stmt_p = then_clause;
repeat = 1;
}
+ else if (TREE_CODE (cond) == VAR_DECL)
+ {
+ if (TREE_CODE (else_clause) == MODIFY_EXPR
+ && TREE_OPERAND (else_clause, 0) == cond
+ && integer_zerop (TREE_OPERAND (else_clause, 1)))
+ COND_EXPR_ELSE (*stmt_p) = build_empty_stmt ();
+ }
}
else if (code == SWITCH_EXPR)
repeat |= remove_useless_stmts_and_vars (&SWITCH_BODY (*stmt_p),