From f461569a23508a49b913787cc76ab42bc24146f4 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Mon, 29 Jun 2015 14:17:13 +0000 Subject: [PATCH] fold-const.c (fold_binary_loc): Move X - (X / Y) * Y -> X % Y to ... * fold-const.c (fold_binary_loc): Move X - (X / Y) * Y -> X % Y to ... * match.pd: ... pattern here. Co-Authored-By: Marc Glisse From-SVN: r225120 --- gcc/ChangeLog | 6 ++++++ gcc/fold-const.c | 13 ------------- gcc/match.pd | 6 ++++++ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4427fd48702..3ce7acb0085 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2015-06-29 Marek Polacek + Marc Glisse + + * fold-const.c (fold_binary_loc): Move X - (X / Y) * Y -> X % Y to ... + * match.pd: ... pattern here. + 2015-06-29 Tom de Vries * tree-parloops.c (try_transform_to_exit_first_loop_alt): Simplify diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 7b9502e2d38..a447452193d 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -10509,19 +10509,6 @@ fold_binary_loc (location_t loc, fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0))); - /* X - (X / Y) * Y is X % Y. */ - if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) - && TREE_CODE (arg1) == MULT_EXPR - && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR - && operand_equal_p (arg0, - TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0) - && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1), - TREE_OPERAND (arg1, 1), 0)) - return - fold_convert_loc (loc, type, - fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0), - arg0, TREE_OPERAND (arg1, 1))); - if (! FLOAT_TYPE_P (type)) { /* Fold A - (A & B) into ~B & A. */ diff --git a/gcc/match.pd b/gcc/match.pd index 0189a966bc8..0cf3d218389 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -238,6 +238,12 @@ along with GCC; see the file COPYING3. If not see && tree_nop_conversion_p (type, TREE_TYPE (@1))) (trunc_mod @0 (convert @1)))) +/* X - (X / Y) * Y is the same as X % Y. */ +(simplify + (minus (convert? @0) (convert? (mult (trunc_div @0 @1) @1))) + (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + (convert (trunc_mod @0 @1)))) + /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, i.e. "X % C" into "X & (C - 1)", if X and C are positive. Also optimize A % (C << N) where C is a power of 2, -- 2.30.2