]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Match: Refactor the signed SAT_* match for saturated value [NFC]
authorPan Li <pan2.li@intel.com>
Thu, 12 Dec 2024 02:48:08 +0000 (10:48 +0800)
committerPan Li <pan2.li@intel.com>
Tue, 7 Jan 2025 23:35:43 +0000 (07:35 +0800)
This patch would like to refactor the all signed SAT_* patterns for
the saturated value.  Aka, overflow to INT_MAX when > 0 and downflow
to INT_MIN when < 0.  Thus, we can remove sorts of duplicated expression
in different patterns.

The below test suites are passed for this patch.
* The rv64gcv fully regression test.
* The x86 bootstrap test.
* The x86 fully regression test.

gcc/ChangeLog:

* match.pd: Extract saturated value match for signed SAT_*.

Signed-off-by: Pan Li <pan2.li@intel.com>
gcc/match.pd

index 8b72eaf713a0039bb1b2c5b790dfe826d0d3e207..cb48c8c2505e8bcd2bc7a7798a4cded6895c0532 100644 (file)
@@ -3330,6 +3330,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     }
     (if (wi::eq_p (trunc_max, int_cst_1) && wi::eq_p (max, int_cst_2)))))))
 
+(if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
+ /* SAT_VAL = (-(T)(X < 0) ^ MAX)  */
+ (match (signed_integer_sat_val @0)
+  (bit_xor:c (nop_convert? (negate
+                           (nop_convert? (convert (lt @0 integer_zerop)))))
+            max_value)))
+
 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
  (match (signed_integer_sat_add @0 @1)
   /* T SUM = (T)((UT)X + (UT)Y)
@@ -3338,7 +3345,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                                                           (nop_convert @1))))
                        (bit_not (bit_xor:c @0 @1)))
             integer_zerop)
-        (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)
+        (signed_integer_sat_val @0)
         @2))
  (match (signed_integer_sat_add @0 @1)
   /* T SUM = (T)((UT)X + (UT)Y)
@@ -3356,17 +3363,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                                                         (nop_convert @1))))
                        integer_zerop)
                    (ge (bit_xor:c @0 @1) integer_zerop))
-        (bit_xor:c (nop_convert (negate (nop_convert (convert
-                                                     (lt @0 integer_zerop)))))
-                   max_value)
+        (signed_integer_sat_val @0)
         @2))
  (match (signed_integer_sat_add @0 @1)
    /* SUM = .ADD_OVERFLOW (X, Y)
       SAT_S_ADD = IMAGPART_EXPR (SUM) != 0 ? (-(T)(X < 0) ^ MAX) : SUM  */
   (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) integer_zerop)
-        (bit_xor:c (nop_convert?
-                    (negate (nop_convert? (convert (lt @0 integer_zerop)))))
-                   max_value)
+        (signed_integer_sat_val @0)
         (realpart @2)))
  (match (signed_integer_sat_add @0 @1)
   /* T SUM = (T)((UT)X + (UT)Y)
@@ -3375,9 +3378,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                                                         (nop_convert @1))))
                        integer_zerop)
                    (bit_not (lt (bit_xor:c @0 @1) integer_zerop)))
-        (bit_xor:c (nop_convert (negate (nop_convert (convert
-                                                      (lt @0 integer_zerop)))))
-                   max_value)
+        (signed_integer_sat_val @0)
         @2))
  (match (signed_integer_sat_add @0 @1)
   /* T SUM = (T)((UT)X + (UT)IMM);
@@ -3386,10 +3387,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cond^ (lt (bit_and:c (bit_xor:c @0 (nop_convert@2 (plus (nop_convert @0)
                                                           INTEGER_CST@1)))
                        (bit_xor:c @0 INTEGER_CST@3)) integer_zerop)
-        (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)
+        (signed_integer_sat_val @0)
         @2)
-  (if (wi::bit_and (wi::to_wide (@1), wi::to_wide (@3)) == 0)))
-)
+  (if (wi::bit_and (wi::to_wide (@1), wi::to_wide (@3)) == 0))))
 
 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
  (match (signed_integer_sat_sub @0 @1)
@@ -3399,7 +3399,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                        (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
                                                          (nop_convert @1)))))
             integer_zerop)
-        (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)
+        (signed_integer_sat_val @0)
         @2))
  (match (signed_integer_sat_sub @0 @1)
   /* T Z = (T)((UT)X - (UT)Y);
@@ -3409,7 +3409,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                                                          (nop_convert @1)))))
             integer_zerop)
         @2
-        (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)))
+        (signed_integer_sat_val @0)))
  (match (signed_integer_sat_sub @0 @1)
   /* T Z = (T)((UT)X - (UT)Y);
      SAT_S_SUB = (X ^ Y) < 0 & (X ^ Z) < 0 ? (-(T)(X < 0) ^ MAX) : Z  */
@@ -3417,17 +3417,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                                                          (nop_convert @1))))
                        integer_zerop)
                    (lt (bit_xor:c @0 @1) integer_zerop))
-        (bit_xor:c (nop_convert (negate (nop_convert (convert
-                                                      (lt @0 integer_zerop)))))
-                   max_value)
+        (signed_integer_sat_val @0)
         @2))
  (match (signed_integer_sat_sub @0 @1)
   /* Z = .SUB_OVERFLOW (X, Y)
      SAT_S_SUB = IMAGPART (Z) != 0 ? (-(T)(X < 0) ^ MAX) : REALPART (Z)  */
   (cond^ (ne (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
-        (bit_xor:c (nop_convert?
-                   (negate (nop_convert? (convert (lt @0 integer_zerop)))))
-                   max_value)
+        (signed_integer_sat_val @0)
         (realpart @2))
   (if (types_match (type, @0, @1)))))