diff options
author | Richard Biener <rguenther@suse.de> | 2015-07-24 10:31:21 +0000 |
---|---|---|
committer | Richard Biener <rguenth@gcc.gnu.org> | 2015-07-24 10:31:21 +0000 |
commit | 24f1db9c0d7900c641ca795fd631c14fdf174b49 (patch) | |
tree | c85bbadb864cda18e5f96a42bd3d4f53fb691f79 /gcc/fold-const.c | |
parent | 25a627733f92db8a4e00a40c3eb6b1b394b5bdfd (diff) | |
download | gcc-24f1db9c0d7900c641ca795fd631c14fdf174b49.zip gcc-24f1db9c0d7900c641ca795fd631c14fdf174b49.tar.gz gcc-24f1db9c0d7900c641ca795fd631c14fdf174b49.tar.bz2 |
fold-const.c (maybe_canonicalize_comparison_1): Move A code CST canonicalization ...
2015-07-24 Richard Biener <rguenther@suse.de>
* fold-const.c (maybe_canonicalize_comparison_1): Move
A code CST canonicalization ...
* match.pd: ... to a pattern here.
cp/
* init.c (build_vec_init): Build iterator bound in the same
type as the iterator.
From-SVN: r226143
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r-- | gcc/fold-const.c | 98 |
1 files changed, 32 insertions, 66 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 54a6b13..c404857 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8092,26 +8092,21 @@ maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type, enum tree_code code0 = TREE_CODE (arg0); tree t, cst0 = NULL_TREE; int sgn0; - bool swap = false; - - /* Match A +- CST code arg1 and CST code arg1. We can change the - first form only if overflow is undefined. */ - if (!(((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0)) - && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))) - /* In principle pointers also have undefined overflow behavior, - but that causes problems elsewhere. */ - && !POINTER_TYPE_P (TREE_TYPE (arg0)) - && (code0 == MINUS_EXPR - || code0 == PLUS_EXPR) - && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) - || code0 == INTEGER_CST)) + + /* Match A +- CST code arg1. We can change this only if overflow + is undefined. */ + if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))) + /* In principle pointers also have undefined overflow behavior, + but that causes problems elsewhere. */ + && !POINTER_TYPE_P (TREE_TYPE (arg0)) + && (code0 == MINUS_EXPR + || code0 == PLUS_EXPR) + && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)) return NULL_TREE; /* Identify the constant in arg0 and its sign. */ - if (code0 == INTEGER_CST) - cst0 = arg0; - else - cst0 = TREE_OPERAND (arg0, 1); + cst0 = TREE_OPERAND (arg0, 1); sgn0 = tree_int_cst_sgn (cst0); /* Overflowed constants and zero will cause problems. */ @@ -8121,47 +8116,25 @@ maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type, /* See if we can reduce the magnitude of the constant in arg0 by changing the comparison code. */ - if (code0 == INTEGER_CST) - { - /* CST <= arg1 -> CST-1 < arg1. */ - if (code == LE_EXPR && sgn0 == 1) - code = LT_EXPR; - /* -CST < arg1 -> -CST-1 <= arg1. */ - else if (code == LT_EXPR && sgn0 == -1) - code = LE_EXPR; - /* CST > arg1 -> CST-1 >= arg1. */ - else if (code == GT_EXPR && sgn0 == 1) - code = GE_EXPR; - /* -CST >= arg1 -> -CST-1 > arg1. */ - else if (code == GE_EXPR && sgn0 == -1) - code = GT_EXPR; - else - return NULL_TREE; - /* arg1 code' CST' might be more canonical. */ - swap = true; - } + /* A - CST < arg1 -> A - CST-1 <= arg1. */ + if (code == LT_EXPR + && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) + code = LE_EXPR; + /* A + CST > arg1 -> A + CST-1 >= arg1. */ + else if (code == GT_EXPR + && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) + code = GE_EXPR; + /* A + CST <= arg1 -> A + CST-1 < arg1. */ + else if (code == LE_EXPR + && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) + code = LT_EXPR; + /* A - CST >= arg1 -> A - CST-1 > arg1. */ + else if (code == GE_EXPR + && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) + code = GT_EXPR; else - { - /* A - CST < arg1 -> A - CST-1 <= arg1. */ - if (code == LT_EXPR - && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) - code = LE_EXPR; - /* A + CST > arg1 -> A + CST-1 >= arg1. */ - else if (code == GT_EXPR - && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) - code = GE_EXPR; - /* A + CST <= arg1 -> A + CST-1 < arg1. */ - else if (code == LE_EXPR - && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) - code = LT_EXPR; - /* A - CST >= arg1 -> A - CST-1 > arg1. */ - else if (code == GE_EXPR - && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) - code = GT_EXPR; - else - return NULL_TREE; - *strict_overflow_p = true; - } + return NULL_TREE; + *strict_overflow_p = true; /* Now build the constant reduced in magnitude. But not if that would produce one outside of its types range. */ @@ -8172,21 +8145,14 @@ maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type, || (sgn0 == -1 && TYPE_MAX_VALUE (TREE_TYPE (cst0)) && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0)))))) - /* We cannot swap the comparison here as that would cause us to - endlessly recurse. */ return NULL_TREE; t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR, cst0, build_int_cst (TREE_TYPE (cst0), 1)); - if (code0 != INTEGER_CST) - t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t); + t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t); t = fold_convert (TREE_TYPE (arg1), t); - /* If swapping might yield to a more canonical form, do so. */ - if (swap) - return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t); - else - return fold_build2_loc (loc, code, type, t, arg1); + return fold_build2_loc (loc, code, type, t, arg1); } /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined |