https://gcc.gnu.org/g:f126e9e03f0e3df1c31597c732ca1ba64c56f932

commit r17-6522-gf126e9e03f0e3df1c31597c732ca1ba64c56f932
Author: Andrea Pinski <[email protected]>
Date:   Thu Nov 19 21:31:35 2026 -0800

    match: adding missing :c to gt in mul_carry_low_sum and mul_carry_cross_sum
    
    I noticed this while doing the next patch which adds a `cond^` version of 
them.
    Basically both mul_carry_low_sum and mul_carry_cross_sum were missing a 
`:c` on
    the gt as the order of the operands could be swapped around.
    
    I found a testcase where this currently makes a difference. It is just
    a modified version of long-mul-two-carry.c with taking the address of
    cross_sum in each function to force ssa version to be higher than the
    other side.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
    gcc/ChangeLog:
    
            * match.pd (mul_carry_low_sum): Add a `:c` on the gt.
            (mul_carry_cross_sum): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/long-mul-two-carry-1a.c: New test.
    
    Signed-off-by: Andrea Pinski <[email protected]>

Diff:
---
 gcc/match.pd                                       |   4 +-
 .../gcc.dg/tree-ssa/long-mul-two-carry-1a.c        | 145 +++++++++++++++++++++
 2 files changed, 147 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index f12f3f3e6276..8c8cf93e17b0 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -13547,7 +13547,7 @@ and,
    constraints, and @shift0 ties the shift amount to the inner constants.  */
 (match (mul_carry_low_sum @op0 @op1 @mul_hilo0 @mul_hilo1 @mul_hilo2 @shift0)
   (lshift
-    (convert (gt
+    (convert (gt:c
       @mul_hilo0
       (mul_low_sum @op0 @op1 @mul_hilo1 @mul_hilo2 @shift0
        @1)))
@@ -13557,7 +13557,7 @@ and,
    with no inherent type or halfwidth constraint.  */
 (match (mul_carry_cross_sum @mul_hilo0 @mul_hilo1 @mul_hilo2)
   (lshift
-    (convert (gt
+    (convert (gt:c
       @mul_hilo0
       (mul_cross_sum @mul_hilo1 @mul_hilo2)))
     INTEGER_CST@0)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/long-mul-two-carry-1a.c 
b/gcc/testsuite/gcc.dg/tree-ssa/long-mul-two-carry-1a.c
new file mode 100644
index 000000000000..dd035c396d1e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/long-mul-two-carry-1a.c
@@ -0,0 +1,145 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -fdump-tree-forwprop-details" } */
+
+typedef __UINT32_TYPE__ uint32_t;
+typedef __UINT64_TYPE__ uint64_t;
+
+/* High part using two separate carries (cross carry + low carry).  */
+uint64_t mulh_two_carry (uint64_t x, uint64_t y)
+{
+  uint64_t x_hi = x >> 32;
+  uint64_t x_lo = x & 0xFFFFFFFFUL;
+  uint64_t y_hi = y >> 32;
+  uint64_t y_lo = y & 0xFFFFFFFFUL;
+
+  uint64_t lolo = x_lo * y_lo;
+  uint64_t hilo = x_hi * y_lo;
+  uint64_t lohi = x_lo * y_hi;
+  uint64_t hihi = x_hi * y_hi;
+
+  uint64_t cross_sum = hilo + lohi;
+  uint64_t cross_carry = (uint64_t)(cross_sum < hilo) << 32;
+
+  uint64_t cross_shifted = cross_sum << 32;
+  uint64_t low_result = lolo + cross_shifted;
+  uint64_t low_carry = (uint64_t)(low_result < cross_shifted);
+
+  uint64_t high = hihi + (cross_sum >> 32) + cross_carry + low_carry;
+  &cross_sum;
+
+  return high;
+}
+
+/* Commuted operand order.  */
+uint64_t mulh_two_carry_comm (uint64_t x, uint64_t y)
+{
+  uint64_t x_hi = x >> 32;
+  uint64_t x_lo = x & 0xFFFFFFFFUL;
+  uint64_t y_hi = y >> 32;
+  uint64_t y_lo = y & 0xFFFFFFFFUL;
+
+  uint64_t lolo = x_lo * y_lo;
+  uint64_t lohi = x_lo * y_hi;
+  uint64_t hilo = x_hi * y_lo;
+  uint64_t hihi = x_hi * y_hi;
+
+  uint64_t cross_sum = lohi + hilo;
+  uint64_t cross_carry = (uint64_t)(cross_sum < lohi) << 32;
+
+  uint64_t cross_shifted = cross_sum << 32;
+  uint64_t low_result = cross_shifted + lolo;
+  uint64_t low_carry = (uint64_t)(low_result < lolo);
+
+  uint64_t high = hihi + (cross_sum >> 32) + cross_carry + low_carry;
+  &cross_sum;
+
+  return high;
+}
+
+/* 32-bit variant.  */
+uint32_t mulh_two_carry_32 (uint32_t x, uint32_t y)
+{
+  uint32_t x_hi = x >> 16;
+  uint32_t x_lo = x & 0xFFFF;
+  uint32_t y_hi = y >> 16;
+  uint32_t y_lo = y & 0xFFFF;
+
+  uint32_t lolo = x_lo * y_lo;
+  uint32_t hilo = x_hi * y_lo;
+  uint32_t lohi = x_lo * y_hi;
+  uint32_t hihi = x_hi * y_hi;
+
+  uint32_t cross_sum = hilo + lohi;
+  uint32_t cross_carry = (uint32_t)(cross_sum < hilo) << 16;
+
+  uint32_t cross_shifted = cross_sum << 16;
+  uint32_t low_result = lolo + cross_shifted;
+  uint32_t low_carry = (uint32_t)(low_result < cross_shifted);
+
+  uint32_t high = hihi + (cross_sum >> 16) + cross_carry + low_carry;
+  &cross_sum;
+
+  return high;
+}
+
+/* Full multiply: both high and low parts.  */
+uint64_t full_mul_two_carry (uint64_t x, uint64_t y, uint64_t *lo)
+{
+  uint64_t x_hi = x >> 32;
+  uint64_t x_lo = x & 0xFFFFFFFFUL;
+  uint64_t y_hi = y >> 32;
+  uint64_t y_lo = y & 0xFFFFFFFFUL;
+
+  uint64_t lolo = x_lo * y_lo;
+  uint64_t hilo = x_hi * y_lo;
+  uint64_t lohi = x_lo * y_hi;
+  uint64_t hihi = x_hi * y_hi;
+
+  uint64_t cross_sum = hilo + lohi;
+  uint64_t cross_carry = (uint64_t)(cross_sum < hilo) << 32;
+
+  uint64_t cross_shifted = cross_sum << 32;
+  uint64_t low_result = lolo + cross_shifted;
+  uint64_t low_carry = (uint64_t)(low_result < cross_shifted);
+
+  uint64_t high = hihi + (cross_sum >> 32) + cross_carry + low_carry;
+  &cross_sum;
+
+  *lo = low_result;
+  return high;
+}
+
+/* Low carry written as an if-branch + 2-arg PHI.  Exercises
+   match_long_mul_phi's LMK_CARRY_LOW path (shift = 0).  */
+uint64_t mulh_two_carry_low_phi (uint64_t x, uint64_t y)
+{
+  uint64_t x_hi = x >> 32;
+  uint64_t x_lo = x & 0xFFFFFFFFUL;
+  uint64_t y_hi = y >> 32;
+  uint64_t y_lo = y & 0xFFFFFFFFUL;
+
+  uint64_t lolo = x_lo * y_lo;
+  uint64_t hilo = x_hi * y_lo;
+  uint64_t lohi = x_lo * y_hi;
+  uint64_t hihi = x_hi * y_hi;
+
+  uint64_t cross_sum = hilo + lohi;
+  uint64_t cross_carry = (uint64_t)(cross_sum < hilo) << 32;
+  uint64_t cross_shifted = cross_sum << 32;
+  uint64_t low_result = lolo + cross_shifted;
+
+  uint64_t high = hihi + (cross_sum >> 32) + cross_carry;
+  if (low_result < cross_shifted)
+    high += 1;
+  &cross_sum;
+
+  return high;
+}
+
+/* The LOW fold for full_mul_two_carry lands in forwprop4 because
+   `lolo + cross_shifted' also feeds the HIGH fold's low-carry
+   compare; long_mul_check_low_plus_defer holds it until the HIGH
+   fold has consumed the compare.  */
+/* { dg-final { scan-tree-dump-times "Long multiplication high part folded\\." 
4 "forwprop3" } } */
+/* { dg-final { scan-tree-dump-times "Long multiplication high part folded 
\\(carry PHI\\)" 1 "forwprop3" } } */
+/* { dg-final { scan-tree-dump-times "Long multiplication low part folded\\." 
1 "forwprop4" } } */

Reply via email to