From: Pan Li <pan2...@intel.com>

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...@intel.com>
---
 gcc/match.pd | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 5b30a1e9990..18098920007 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3314,6 +3314,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)
@@ -3322,7 +3329,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)
@@ -3340,17 +3347,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)
@@ -3359,9 +3362,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);
@@ -3370,10 +3371,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)
@@ -3383,7 +3383,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);
@@ -3393,7 +3393,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  */
@@ -3401,17 +3401,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)))))
 
-- 
2.43.0

Reply via email to