From 8d06c80914d7374d16887b0e817eadb41d898aa8 Mon Sep 17 00:00:00 2001 From: "James A. Morrison" Date: Fri, 4 Mar 2005 02:48:30 +0000 Subject: [PATCH] re PR tree-optimization/15784 (fold misses binary optimization) 2005-03-03 James A. Morrison PR tree-optimization/15784 * fold-const.c (fold): Fold ~A + 1 to -1. Fold -A - 1 and -1 - A to ~A. From-SVN: r95870 --- gcc/ChangeLog | 6 ++++++ gcc/fold-const.c | 15 +++++++++++++++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/pr15784-4.c | 12 ++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr15784-4.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 67c26a21d4c..19b45f231d9 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2005-03-03 James A. Morrison + + PR tree-optimization/15784 + * fold-const.c (fold): Fold ~A + 1 to -1. Fold -A - 1 + and -1 - A to ~A. + 2005-03-03 David Edelsohn * config/rs6000/predicates.md (branch_comparison_operator): diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 72e557eeb71..2c6d71ec030 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -7223,6 +7223,11 @@ fold (tree expr) if (TREE_CODE (arg0) == NEGATE_EXPR && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1)) return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0))); + /* Convert ~A + 1 to -A. */ + if (INTEGRAL_TYPE_P (type) + && TREE_CODE (arg0) == BIT_NOT_EXPR + && integer_onep (arg1)) + return fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0))); if (TREE_CODE (type) == COMPLEX_TYPE) { @@ -7661,6 +7666,16 @@ fold (tree expr) && reorder_operands_p (arg0, arg1)) return fold (build2 (MINUS_EXPR, type, negate_expr (arg1), TREE_OPERAND (arg0, 0))); + /* Convert -A - 1 to ~A. */ + if (INTEGRAL_TYPE_P (type) + && TREE_CODE (arg0) == NEGATE_EXPR + && integer_onep (arg1)) + return fold (build1 (BIT_NOT_EXPR, type, TREE_OPERAND (arg0, 0))); + + /* Convert -1 - A to ~A. */ + if (INTEGRAL_TYPE_P (type) + && integer_all_onesp (arg0)) + return fold (build1 (BIT_NOT_EXPR, type, arg1)); if (TREE_CODE (type) == COMPLEX_TYPE) { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e589f7bff52..b7a3692e6a2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2005-03-03 James A. Morrison + + * gcc.dg/pr15784-4.c: New test. + 2005-03-03 Geoffrey Keating * gcc.c-torture/execute/pr17133.c: New. diff --git a/gcc/testsuite/gcc.dg/pr15784-4.c b/gcc/testsuite/gcc.dg/pr15784-4.c new file mode 100644 index 00000000000..021b6375efc --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr15784-4.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +int a (int x) { + return ~x + 1; /* -x */ +} + +int b (int x) { + return -x -1; /* ~x */ +} + +/* { dg-final { scan-tree-dump "~x;" "optimized" } } */ +/* { dg-final { scan-tree-dump "-x;" "optimized" } } */ -- 2.30.2