diff mbox

PATCH] Fix PR 31531: A microoptimization of isnegative of signed integer

Message ID SN2PR0701MB1024F4E510034BC0F61FC0348EAD0@SN2PR0701MB1024.namprd07.prod.outlook.com
State New
Headers show

Commit Message

Hurugalawadi, Naveen Feb. 16, 2016, 4:50 a.m. UTC
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

Comments

Hurugalawadi, Naveen March 3, 2016, 6:25 a.m. UTC | #1
Hi,

Please find attached the patch that enhances the existing pattern.

Please review the patch at the following link and let me know
if there should be any modifications in it:-

https://gcc.gnu.org/ml/gcc-patches/2016-02/msg01035.html

Thanks,
Naveen
Hurugalawadi, Naveen March 22, 2016, 6:52 a.m. UTC | #2
Hi,

Please find attached the patch that enhances the existing pattern.

Please review the patch at the following link and let me know
if there should be any modifications in it:-

https://gcc.gnu.org/ml/gcc-patches/2016-02/msg01035.html

Thanks,
Naveen
Hurugalawadi, Naveen April 5, 2016, 9:12 a.m. UTC | #3
Hi,

Please review the patch at the following link and let me know
if there should be any modifications in it:-

https://gcc.gnu.org/ml/gcc-patches/2016-02/msg01035.html

Thanks,
Naveen
Richard Biener April 6, 2016, 10:58 a.m. UTC | #4
On Tue, Feb 16, 2016 at 5:50 AM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
> 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.

What's the motivation of splitting this into a equal type (borken for
the vector case)
and a non-equal type case?  Simply only allow nop-conversions here
(tree_nop_conversion_p)
and unconditionally emit

 (scmp (view_convert:newtype @0) (bit_not @1))

?  The conversion will be omitted if it turns out to be not necessary
and a view_convert
will be turned into a regular conversion for non-vector cases.

Richard.

> Thanks,
> Naveen
diff mbox

Patch

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" } } */