Hi,

>> I'm also failing to see why you can't enhance the existing

Please find attached the patch that enhances the existing pattern.
Please review the patch and let me know if any further modifications
are required.

Thanks,
Naveen
diff --git a/gcc/match.pd b/gcc/match.pd
index 6c8ebd5..bd47a91 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1871,10 +1871,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (for cmp (simple_comparison)
      scmp (swapped_simple_comparison)
  (simplify
-  (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
+  (cmp (convert?@3 (bit_not@2 @0)) CONSTANT_CLASS_P@1)
   (if (single_use (@2)
-       && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
-   (scmp @0 (bit_not @1)))))
+       && ((TREE_CODE (@1) == INTEGER_CST && TREE_TYPE (@3) == TREE_TYPE (@2))
+            || (TREE_CODE (@1) == VECTOR_CST
+		&& (VECTOR_TYPE_P (TREE_TYPE (@3))
+		    == VECTOR_TYPE_P (TREE_TYPE (@2)))
+		&& (TYPE_VECTOR_SUBPARTS (TREE_TYPE (@3))
+		    == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@2)))
+		&& (TYPE_MODE (TREE_TYPE (TREE_TYPE (@3)))
+		    == TYPE_MODE (TREE_TYPE (TREE_TYPE (@2)))))))
+   (scmp @0 (bit_not @1))
+  (if (TYPE_PRECISION (TREE_TYPE (@3)) == TYPE_PRECISION (TREE_TYPE (@2))
+       && (TREE_CODE (@1) == INTEGER_CST))
+   (with { tree newtype = TREE_TYPE (@1); }
+    (scmp (convert:newtype @0) (bit_not @1)))))))
 
 (for cmp (simple_comparison)
  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
diff --git a/gcc/testsuite/gcc.dg/pr31531.c b/gcc/testsuite/gcc.dg/pr31531.c
new file mode 100644
index 0000000..cf9dd82
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr31531.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple" } */
+/* { dg-require-effective-target int32 } */
+
+int isnegative_optimized_4 (unsigned int X)
+{
+  int result;
+  if ((~X) >> 31)
+    result = 0;
+  else
+    result = 1;
+  return result;
+}
+
+/* { dg-final { scan-tree-dump-times "signed int X.0" 1 "gimple" } } */

Reply via email to