[gcc r17-2670] middle-end: Fold averages with equal operands [PR122715]

Jeff Law law@gcc.gnu.org
Thu Jul 23 14:47:14 GMT 2026


https://gcc.gnu.org/g:3b126071b10e5b12e7694f7a237f746e8db6d82c

commit r17-2670-g3b126071b10e5b12e7694f7a237f746e8db6d82c
Author: Odysseas Georgoudis <odygrd@hotmail.com>
Date:   Thu Jul 23 08:46:24 2026 -0600

    middle-end: Fold averages with equal operands [PR122715]
    
    IFN_AVG_FLOOR (x, x) and IFN_AVG_CEIL (x, x) both
    produce x.  Fold these cases in match.pd.
    
            PR middle-end/122715
    
    gcc/ChangeLog:
    
            * match.pd: Fold IFN_AVG_FLOOR and IFN_AVG_CEIL with equal
            operands.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/pr122715.c: New test.

Diff:
---
 gcc/match.pd                    |  6 ++++++
 gcc/testsuite/gcc.dg/pr122715.c | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 723780b8c381..536d5125a0b6 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2320,7 +2320,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     (view_convert (rshift (view_convert:ntype @0) @1))
     (convert (rshift (convert:ntype @0) @1))))))
 
+/* IFN_AVG_FLOOR (x, x) and IFN_AVG_CEIL (x, x) are both x.  */
 #if GIMPLE
+(for avg (IFN_AVG_FLOOR IFN_AVG_CEIL)
+ (simplify
+  (avg @0 @0)
+  @0))
+
  /* Fold ((x + y) >> 1 into IFN_AVG_FLOOR (x, y) if x and y are vectors in
     which each element is known to have at least one leading zero bit.  */
 (simplify
diff --git a/gcc/testsuite/gcc.dg/pr122715.c b/gcc/testsuite/gcc.dg/pr122715.c
new file mode 100644
index 000000000000..a767c1fe432f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr122715.c
@@ -0,0 +1,22 @@
+/* PR middle-end/122715 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fgimple -fdump-tree-optimized" } */
+
+typedef unsigned int v4u32 __attribute__((vector_size(16)));
+
+v4u32 __GIMPLE() avg_floor(v4u32 x)
+{
+  v4u32 res;
+  res_1 = .AVG_FLOOR (x, x);
+  return res_1;
+}
+
+v4u32 __GIMPLE() avg_ceil(v4u32 x)
+{
+  v4u32 res;
+  res_1 = .AVG_CEIL (x, x);
+  return res_1;
+}
+
+/* { dg-final { scan-tree-dump-not {\.AVG_} "optimized" } } */
+/* { dg-final { scan-tree-dump-times {return x_} 2 "optimized" } } */


More information about the Gcc-cvs mailing list