On Wed, Jun 24, 2026 at 3:26 AM Kael Andrew Franco
<[email protected]> wrote:
>
> From 5ff3dfadc6294f452aa8332dfdd9663dfbe4db0f Mon Sep 17 00:00:00 2001
> From: Kael Andrew Alonzo Franco <[email protected]>
> Date: Tue, 23 Jun 2026 14:34:29 -0400
> Subject: [PATCH] match: X % Y < Y -> Y >= 0 and X % Y >= Y -> Y < 0 [PR125737]
>
> LLVM trunk optimizes those patterns so add those patterns to GCC.
>
> Bootstrapped and tested on x86_64-pc-linux-gnu

Ok, pushed with a modified commit message; not to reference LLVM here
and better explain what is being done/added.
https://gcc.gnu.org/pipermail/gcc-cvs/2026-June/456460.html

Next time please don't reference LLVM unless necessary and add
slightly more to the commit message than saying copying LLVM :).

Thanks,
Andrea

>
> PR tree-optimization/125737
>
> gcc/ChangeLog:
>
> PR tree-optimization/125737
> * match.pd: Optimize X % Y < Y -> Y >= 0 and X % Y >= Y -> Y < 0
>
> gcc/testsuite/ChangeLog:
>
> PR tree-optimization/125737
> * gcc.dg/pr125737.c: Move test to pr125737-1.c.
> * gcc.dg/pr125737-1.c: Add test for (a % b) >= b when a and b are nonnegative.
> * gcc.dg/pr125737-2.c: New test for (a % b) {<,>=} b.
>
> Signed-off-by: Kael Franco <[email protected]>
> ---
>  gcc/match.pd                                      | 10 ++++++----
>  gcc/testsuite/gcc.dg/{pr125737.c => pr125737-1.c} | 10 +++++++++-
>  gcc/testsuite/gcc.dg/pr125737-2.c                 | 14 ++++++++++++++
>  3 files changed, 29 insertions(+), 5 deletions(-)
>  rename gcc/testsuite/gcc.dg/{pr125737.c => pr125737-1.c} (51%)
>  create mode 100644 gcc/testsuite/gcc.dg/pr125737-2.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 8c410c2f3b3..7319be803f5 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1730,11 +1730,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>       (convert (bit_and (negate (convert:utype { pmop[0]; }))
>         (convert:utype @1)))))))
>
> -/* X % Y is smaller than Y.  */
> -(for cmp (lt ge)
> +/* X % Y < Y -> Y >= 0
> +   X % Y >= Y -> Y < 0  */
> +(for ltge (lt ge)
> +     gelt (ge lt)
>   (simplify
> -  (cmp:c (trunc_mod tree_expr_nonnegative_p@0 tree_expr_nonnegative_p@1) @1)
> -  { constant_boolean_node (cmp == LT_EXPR, type); }))
> +  (ltge:c (trunc_mod @0 @1) @1)
> +  (gelt @1 { build_zero_cst (TREE_TYPE (@1)); })))
>
>  /* x | ~0 -> ~0  */
>  (simplify
> diff --git a/gcc/testsuite/gcc.dg/pr125737.c 
> b/gcc/testsuite/gcc.dg/pr125737-1.c
> similarity index 51%
> rename from gcc/testsuite/gcc.dg/pr125737.c
> rename to gcc/testsuite/gcc.dg/pr125737-1.c
> index 93d0fcd215a..85347a63c7f 100644
> --- a/gcc/testsuite/gcc.dg/pr125737.c
> +++ b/gcc/testsuite/gcc.dg/pr125737-1.c
> @@ -9,4 +9,12 @@ f (unsigned short a, unsigned short b) {
>    return c < bb;
>  }
>
> -/* { dg-final { scan-tree-dump-times "return 1;" 1 "optimized" } } */
> +int
> +g (unsigned short a, unsigned short b) {
> +  int aa = a;
> +  int bb = b;
> +  int c = aa % bb;
> +  return (c >= bb) == 0;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/pr125737-2.c 
> b/gcc/testsuite/gcc.dg/pr125737-2.c
> new file mode 100644
> index 00000000000..0090fa04ca8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr125737-2.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +_Bool
> +a_mod_b_lt_b (int a, int b) {
> +  return ((a % b) < b) == (b >= 0);
> +}
> +
> +_Bool
> +a_mod_b_ge_b (int a, int b) {
> +  return ((a % b) >= b) == (b < 0);
> +}
> +
> +/* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */
> --
> 2.54.0
>
>

Reply via email to