diff --git a/gcc/match.pd b/gcc/match.pd index 940292d0d4974e227eb02b6c16bf0db198396285..7150b8e78cab3cbc6a1d8372a4e61467cb8cb551 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4276,7 +4276,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ? wi::max_value (TYPE_PRECISION (type), SIGNED) : wi::min_value (TYPE_PRECISION (type), SIGNED)))))) && single_use (@3)) - (mult (plusminus @2 { build_one_cst (type); }) @0)))))) + (mult (plusminus @2 { build_one_cst (type); }) @0))))) + /* (A * B) + (-C) -> (B - C/A) * A, if C is a multiple of A. */ + (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))) + (simplify + (plus (mult:cs integer_nonzerop@0 @1) INTEGER_CST@2) + /* Exclude the case that @2 == min to prevent UB when calculating abs + and (B - C/A). */ + (if (TREE_CODE (type) == INTEGER_TYPE + && wi::neg_p (wi::to_wide (@2)) + && wi::to_wide (@2) != wi::min_value (TYPE_PRECISION (type), SIGNED)) + (with { + wide_int c0 = wi::to_wide (@0); + wide_int c2 = wi::to_wide (@2); + wide_int c2_abs = wi::abs (c2); } + (if (wi::multiple_of_p (c2_abs, c0, TYPE_SIGN (type))) + (with { + /* Calculate @2 / @0 in order to factorize the expression. */ + wide_int div_res = wi::sdiv_trunc (c2, c0); + tree div_cst = wide_int_to_tree (type, div_res); } + (mult (plus @1 { div_cst; }) @0)))))))) #if GIMPLE /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and diff --git a/gcc/testsuite/gcc.dg/pr109393.c b/gcc/testsuite/gcc.dg/pr109393.c new file mode 100644 index 0000000000000000000000000000000000000000..17bf9330796486630145185e7c2fce0123e1d9e9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr109393.c @@ -0,0 +1,23 @@ +/* PR tree-optimization/109393 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int foo(int *a, int j) +{ + int k = j - 1; + return a[j - 1] == a[k]; +} + +int foo2(int *a, int j) +{ + int k = j - 5; + return a[j - 5] == a[k]; +} + +int bar(int *a, int j) +{ + int k = j - 1; + return (&a[j + 1] - 2) == &a[k]; +} + +/* { dg-final { scan-tree-dump-times "return 1;" 3 "optimized" } } */ \ No newline at end of file