From cde9d0c7dc4114f5de6e16e3c547a60c8cd2f41d Mon Sep 17 00:00:00 2001 From: ghazi Date: Mon, 21 May 2001 01:21:23 +0000 Subject: [PATCH] * fold-const.c (tree_expr_nonnegative_p): Detect more non-negative cases. testsuite: * g++.old-deja/g++.warn/compare1.C: New test. * gcc.dg/compare4.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@42365 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 5 +++ gcc/fold-const.c | 13 +++++++ gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/g++.old-deja/g++.warn/compare1.C | 36 +++++++++++++++++++ gcc/testsuite/gcc.dg/compare4.c | 48 ++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 gcc/testsuite/g++.old-deja/g++.warn/compare1.C create mode 100644 gcc/testsuite/gcc.dg/compare4.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1071782..582f100 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2001-05-20 Kaveh R. Ghazi + + * fold-const.c (tree_expr_nonnegative_p): Detect more non-negative + cases. + 2001-05-21 Joseph S. Myers * extend.texi: Clarify documentation of extensions included in ISO diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 3bedea1..2cf534f 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -7351,11 +7351,24 @@ tree_expr_nonnegative_p (t) { switch (TREE_CODE (t)) { + case ABS_EXPR: + case FFS_EXPR: + return 1; case INTEGER_CST: return tree_int_cst_sgn (t) >= 0; case COND_EXPR: return tree_expr_nonnegative_p (TREE_OPERAND (t, 1)) && tree_expr_nonnegative_p (TREE_OPERAND (t, 2)); + case COMPOUND_EXPR: + return tree_expr_nonnegative_p (TREE_OPERAND (t, 1)); + case MIN_EXPR: + return tree_expr_nonnegative_p (TREE_OPERAND (t, 0)) + && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)); + case MAX_EXPR: + return tree_expr_nonnegative_p (TREE_OPERAND (t, 0)) + || tree_expr_nonnegative_p (TREE_OPERAND (t, 1)); + case MODIFY_EXPR: + return tree_expr_nonnegative_p (TREE_OPERAND (t, 1)); case BIND_EXPR: return tree_expr_nonnegative_p (TREE_OPERAND (t, 1)); case RTL_EXPR: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 79d4f0a..510cc89 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2001-05-20 Kaveh R. Ghazi + + * g++.old-deja/g++.warn/compare1.C: New test. + * gcc.dg/compare4.c: New test. + 2001-05-20 Nathan Sidwell * g++.old-deja/g++.other/optimize1.C: New test. diff --git a/gcc/testsuite/g++.old-deja/g++.warn/compare1.C b/gcc/testsuite/g++.old-deja/g++.warn/compare1.C new file mode 100644 index 0000000..687d986 --- /dev/null +++ b/gcc/testsuite/g++.old-deja/g++.warn/compare1.C @@ -0,0 +1,36 @@ +// Build don't link: +// Special g++ Options: -ansi -pedantic-errors -Wsign-compare + +// Copyright (C) 2001 Free Software Foundation, Inc. +// Contributed by Kaveh R. Ghazi 5/13/2001 + +int foo(int x, int y, unsigned u) +{ + /* A MAX_EXPR is non-negative if EITHER argument to the MAX_EXPR is + determined to be non-negative. */ + if (u < (x >? -1)) // WARNING - signed and unsigned + return x; + if (u < (x >? 10)) + return x; + if ((10 >? x) < u) + return x; + if (u < (x >? (y ? (x==y) : 10))) + return x; + if (((y ? 10 : (x==y)) >? x) < u) + return x; + + /* A MIN_EXPR is non-negative if BOTH arguments to the MIN_EXPR are + determined to be non-negative. */ + if (u < ((x?11:8) 5/13/2001. */ + +/* { dg-do compile } */ +/* { dg-options "-Wsign-compare" } */ + +extern void bar(void); + +int foo(int x, int y, unsigned u) +{ + /* A COMPOUND_EXPR is non-negative if the last element is known to + be non-negative. */ + if (u < (bar(), -1)) /*{ dg-warning "signed and unsigned" "COMPOUND_EXPR" }*/ + return x; + if (u < (bar(), 10)) + return x; + if ((bar(), 10) < u) + return x; + if (u < (x ? (bar(),bar(),bar(),bar(),x==y) : 10)) + return x; + if ((x ? 10 : (bar(),bar(),bar(),bar(),x==y)) < u) + return x; + + /* Test an ABS_EXPR, which is by definition non-negative. */ + if (u < __builtin_abs(x)) + return x; + if (__builtin_abs(x) < u) + return x; + if (u < (x ? __builtin_abs(x) : 10)) + return x; + if ((x ? 10: __builtin_abs(x)) < u) + return x; + + /* A MODIFY_EXPR is non-negative if the new value is known to be + non-negative. */ + if (u < (x = -1)) /* { dg-warning "signed and unsigned" "MODIFY_EXPR" } */ + return x; + if (u < (x = 10)) + return x; + if ((x = 10) < u) + return x; + if (u < (x = (y ? (x==y) : 10))) + return x; + if ((x = (y ? 10 : (x==y))) < u) + return x; + + return 0; +} -- 2.7.4