On Wed, Oct 25, 2023 at 5:37 AM Andrew Pinski <[email protected]> wrote:
>
> This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified
> to `abs(a)`. if C1 was originally *_MIN then change it over to use absu
> instead
> of abs.
>
> Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> PR tree-optimization/111957
>
> gcc/ChangeLog:
>
> * match.pd (`a != C1 ? abs(a) : C2`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/phi-opt-40.c: New test.
> ---
> gcc/match.pd | 10 +++++++++
> gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++
> 2 files changed, 35 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 5df04ebba77..370ee35de52 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
> @3))
>
> +/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
> +(for op (abs absu)
> + (simplify
> + (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
> + (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
Why not use
(cond (ne @0 INTEGER_CST@1) (op@3 @0) @1)
? OK with that change.
Richard.
> + (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
> + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
> + (convert (absu:utype @0)))
> + @3))))
> +
> /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
> X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
> (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> new file mode 100644
> index 00000000000..a9011ce97fb
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> @@ -0,0 +1,25 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-phiopt" } */
> +/* PR tree-optimization/111957 */
> +
> +int f(int a)
> +{
> + if (a)
> + return a > 0 ? a : -a;
> + return 0;
> +}
> +
> +int f1(int x)
> +{
> + int intmin = (-1u >> 1);
> + intmin = -intmin - 1;
> + if (x != intmin)
> + return x > 0 ? x : -x;
> + return intmin;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */
> +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */
> --
> 2.34.1
>