This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Improve ABS optimizations in dominator optimizer
- From: law at redhat dot com
- To: gcc-patches at gcc dot gnu dot org
- Date: Fri, 14 May 2004 11:28:15 -0600
- Subject: Improve ABS optimizations in dominator optimizer
- Reply-to: law at redhat dot com
This fixes optimization/14673 -- we were basically missing an opportunity
to optimize an abs into a neg.
Given x LT 0 == true we would transform ABS into negation
x GE 0 == true we would eliminate ABS completely
[ These were performed with a single test of the available expression table. ]
That's not precisely what we want since if we had recorded x LE 0 == true
neither test would trigger, but we want to transform ABS into negation.
The right way to handle this is to do something like
x LE 0 == true turn abs into negation
x GE 0 == true eliminate abs completely
Which requires two checks of the available expression table. Easy 'nuff.
Bootstrapped and regression tested on i686-pc-linux-gnu.
* tree-ssa-dom.c (simplify_rhs_and_lookup_avail_expr): Reorganize
so that it picks up more opportunities to eliminate ABS expressions
or turn them into negations.
* gcc.dg/tree-ssa/20040514-2.c: New test.
Index: tree-ssa-dom.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-ssa-dom.c,v
retrieving revision 2.2
diff -c -p -r2.2 tree-ssa-dom.c
*** tree-ssa-dom.c 13 May 2004 15:21:53 -0000 2.2
--- tree-ssa-dom.c 14 May 2004 17:17:42 -0000
*************** simplify_rhs_and_lookup_avail_expr (stru
*** 1826,1832 ****
if (! dummy_cond)
{
! dummy_cond = build (LT_EXPR, boolean_type_node,
op, integer_zero_node);
dummy_cond = build (COND_EXPR, void_type_node,
dummy_cond, NULL, NULL);
--- 1826,1832 ----
if (! dummy_cond)
{
! dummy_cond = build (LE_EXPR, boolean_type_node,
op, integer_zero_node);
dummy_cond = build (COND_EXPR, void_type_node,
dummy_cond, NULL, NULL);
*************** simplify_rhs_and_lookup_avail_expr (stru
*** 1834,1840 ****
}
else
{
! TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), LT_EXPR);
TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
= convert (type, integer_zero_node);
--- 1834,1840 ----
}
else
{
! TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), LE_EXPR);
TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
= convert (type, integer_zero_node);
*************** simplify_rhs_and_lookup_avail_expr (stru
*** 1842,1847 ****
--- 1842,1867 ----
val = simplify_cond_and_lookup_avail_expr (dummy_cond,
&bd->avail_exprs,
NULL, false);
+
+ if (!val)
+ {
+ TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GE_EXPR);
+ TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
+ TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
+ = convert (type, integer_zero_node);
+
+ val = simplify_cond_and_lookup_avail_expr (dummy_cond,
+ &bd->avail_exprs,
+ NULL, false);
+
+ if (val)
+ {
+ if (integer_zerop (val))
+ val = integer_one_node;
+ else if (integer_onep (val))
+ val = integer_zero_node;
+ }
+ }
}
if (val
Index: testsuite/gcc.dg/tree-ssa/20040514-2.c
===================================================================
RCS file: testsuite/gcc.dg/tree-ssa/20040514-2.c
diff -N testsuite/gcc.dg/tree-ssa/20040514-2.c
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- testsuite/gcc.dg/tree-ssa/20040514-2.c 14 May 2004 17:24:44 -0000
***************
*** 0 ****
--- 1,15 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O1 -fdump-tree-dom3" } */
+ int
+ foo2 (distance, i, j)
+ int distance;
+ int i, j;
+ {
+ int t = distance;
+ if (t <= 0)
+ t = ((t) >= 0 ? (t) : -(t));
+ return t;
+ }
+
+ /* There should be no ABS_EXPR. */
+ /* { dg-final { scan-tree-dump-times "ABS_EXPR " 0 "dom3"} } */