diff options
author | James A. Morrison <phython@gcc.gnu.org> | 2005-02-15 21:58:21 +0000 |
---|---|---|
committer | James A. Morrison <phython@gcc.gnu.org> | 2005-02-15 21:58:21 +0000 |
commit | 40f84775b3a7b9bc9ebd0886d1ad470921c07589 (patch) | |
tree | 0738a0f6031b2fb475ab674da3644b67dcb22345 /gcc | |
parent | 14be52a22cda9918524fa0e4b6bb14ac1c466fdd (diff) | |
download | gcc-40f84775b3a7b9bc9ebd0886d1ad470921c07589.zip gcc-40f84775b3a7b9bc9ebd0886d1ad470921c07589.tar.gz gcc-40f84775b3a7b9bc9ebd0886d1ad470921c07589.tar.bz2 |
fold-const.c (fold): Fold -(~A) to A + 1.
2005-02-15 James A. Morrison <phython@gcc.gnu.org>
* fold-const.c (fold): Fold -(~A) to A + 1. Fold ~(-A) to A - 1.
Fold ~(A - 1) and ~(A + -1) to -A.
From-SVN: r95074
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/fold-const.c | 15 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/pr15785-1.c | 42 |
4 files changed, 67 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 42e602b..a3b955e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,10 @@ 2005-02-15 James A. Morrison <phython@gcc.gnu.org> + * fold-const.c (fold): Fold -(~A) to A + 1. Fold ~(-A) to A - 1. + Fold ~(A - 1) and ~(A + -1) to -A. + +2005-02-15 James A. Morrison <phython@gcc.gnu.org> + PR pch/14940 PR target/19300 * config/host-linux.c (linux_gt_pch_use_address): Copy from diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 03285ec..ef31a32 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -6931,6 +6931,10 @@ fold (tree expr) case NEGATE_EXPR: if (negate_expr_p (arg0)) return fold_convert (type, negate_expr (arg0)); + /* Convert - (~A) to A + 1. */ + if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == BIT_NOT_EXPR) + return fold (build2 (PLUS_EXPR, type, TREE_OPERAND (arg0, 0), + build_int_cst (type, 1))); return t; case ABS_EXPR: @@ -6985,6 +6989,17 @@ fold (tree expr) return fold_not_const (arg0, type); else if (TREE_CODE (arg0) == BIT_NOT_EXPR) return TREE_OPERAND (arg0, 0); + /* Convert ~ (-A) to A - 1. */ + else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR) + return fold (build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0), + build_int_cst (type, 1))); + /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ + else if (INTEGRAL_TYPE_P (type) + && ((TREE_CODE (arg0) == MINUS_EXPR + && integer_onep (TREE_OPERAND (arg0, 1))) + || (TREE_CODE (arg0) == PLUS_EXPR + && integer_all_onesp (TREE_OPERAND (arg0, 1))))) + return fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0))); return t; case PLUS_EXPR: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1a66c5e..fc56108 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2005-02-15 James A. Morrison <phython@gcc.gnu.org> + + PR tree-optimization/15785 + * gcc.dg/pr15785-1.c: New test. + 2005-02-15 Alexandre Oliva <aoliva@redhat.com> PR c++/17788 diff --git a/gcc/testsuite/gcc.dg/pr15785-1.c b/gcc/testsuite/gcc.dg/pr15785-1.c new file mode 100644 index 0000000..47cd3d7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr15785-1.c @@ -0,0 +1,42 @@ +/* { dg-do link } */ + +extern void link_error (); + +void a (int x) { + if (~ (~x) - x) + link_error (); +} +void b (int x) { + if (- (-x) - x) + link_error (); +} + +void c (int x) { + if (!(- (~x) - x)) + link_error (); +} + +void d (int x) { + if (!(~ (-x) - x)) + link_error (); +} + +void e (int x) { + if (x + ~(x - 1)) + link_error (); +} + +void f (int x) { + if (x + ~(x + (-1))) + link_error (); +} + +int main (int argc, char *argv[]) { + a(argc); + b(argc); + c(argc); + d(argc); + e(argc); + f(argc); + return 0; +} |