From 129bd066049f065e522990e63bb10ff92b3c018d Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 3 Dec 2019 10:20:43 +0100 Subject: [PATCH] re PR tree-optimization/92734 (Missing match.pd simplification done by fold_binary_loc on generic) PR tree-optimization/92734 * match.pd ((CST1 - A) +- CST2 -> CST3 - A, CST1 - (CST2 - A) -> CST3 + A): Handle nop casts around inner subtraction. * gcc.dg/tree-ssa/pr92734.c: New test. From-SVN: r278925 --- gcc/ChangeLog | 7 +++++ gcc/match.pd | 42 +++++++++++++++++++------ gcc/testsuite/ChangeLog | 3 ++ gcc/testsuite/gcc.dg/tree-ssa/pr92734.c | 31 ++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr92734.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a89c4352aa3..68d1c288c51 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2019-12-03 Jakub Jelinek + + PR tree-optimization/92734 + * match.pd ((CST1 - A) +- CST2 -> CST3 - A, + CST1 - (CST2 - A) -> CST3 + A): Handle nop casts around + inner subtraction. + 2019-12-03 Uroš Bizjak Jakub Jelinek diff --git a/gcc/match.pd b/gcc/match.pd index 14f6a9d5078..d3312e5f279 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2237,17 +2237,39 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (CST1 - A) +- CST2 -> CST3 - A */ (for outer_op (plus minus) (simplify - (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) - (with { tree cst = const_binop (outer_op, type, @1, @2); } - (if (cst && !TREE_OVERFLOW (cst)) - (minus { cst; } @0))))) - - /* CST1 - (CST2 - A) -> CST3 + A */ + (outer_op (nop_convert (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2) + /* If one of the types wraps, use that one. */ + (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) + /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse + forever if something doesn't simplify into a constant. */ + (if (!CONSTANT_CLASS_P (@0)) + (minus (outer_op (view_convert @1) @2) (view_convert @0))) + (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) + (view_convert (minus (outer_op @1 (view_convert @2)) @0)) + (if (types_match (type, @0)) + (with { tree cst = const_binop (outer_op, type, @1, @2); } + (if (cst && !TREE_OVERFLOW (cst)) + (minus { cst; } @0)))))))) + + /* CST1 - (CST2 - A) -> CST3 + A + Use view_convert because it is safe for vectors and equivalent for + scalars. */ (simplify - (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0)) - (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } - (if (cst && !TREE_OVERFLOW (cst)) - (plus { cst; } @0)))) + (minus CONSTANT_CLASS_P@1 (nop_convert (minus CONSTANT_CLASS_P@2 @0))) + /* If one of the types wraps, use that one. */ + (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) + /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse + forever if something doesn't simplify into a constant. */ + (if (!CONSTANT_CLASS_P (@0)) + (plus (view_convert @0) (minus @1 (view_convert @2)))) + (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) + (view_convert (plus @0 (minus (view_convert @1) @2))) + (if (types_match (type, @0)) + (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } + (if (cst && !TREE_OVERFLOW (cst)) + (plus { cst; } @0))))))) /* ((T)(A)) + CST -> (T)(A + CST) */ #if GIMPLE diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2d8c350d575..07193065f7d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2019-12-03 Jakub Jelinek + PR tree-optimization/92734 + * gcc.dg/tree-ssa/pr92734.c: New test. + PR target/92744 * g++.dg/dfp/pr92744.C: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr92734.c b/gcc/testsuite/gcc.dg/tree-ssa/pr92734.c new file mode 100644 index 00000000000..2921bb4196e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr92734.c @@ -0,0 +1,31 @@ +/* PR tree-optimization/92734 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-forwprop1" } */ +/* { dg-final { scan-tree-dump-times "return t_\[0-9]*\\\(D\\\);" 4 "forwprop1" } } */ + +int +f1 (int t) +{ + return 1 - (int) (1U - t); +} + +int +f2 (int t) +{ + int a = 7U - t; + return 7 - a; +} + +int +f3 (int t) +{ + int a = 32U - t; + return 32 - a; +} + +int +f4 (int t) +{ + int a = 32 - t; + return (int) (32 - (unsigned) a); +} -- 2.30.2