* simplify-rtx.c (simplify_relational_operation_1): Optimize
authorsayle <sayle@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 12 Feb 2007 01:43:50 +0000 (01:43 +0000)
committersayle <sayle@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 12 Feb 2007 01:43:50 +0000 (01:43 +0000)
comparisons of POPCOUNT against zero.
(simplify_const_relational_operation): Likewise.

* gcc.target/ia64/builtin-popcount-1.c: New test case.
* gcc.target/ia64/builtin-popcount-2.c: Likewise.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@121838 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/simplify-rtx.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/ia64/builtin-popcount-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/ia64/builtin-popcount-2.c [new file with mode: 0644]

index 73feade..a5f9960 100644 (file)
@@ -1,3 +1,9 @@
+2007-02-11  Roger Sayle  <roger@eyesopen.com>
+
+       * simplify-rtx.c (simplify_relational_operation_1): Optimize
+       comparisons of POPCOUNT against zero.
+       (simplify_const_relational_operation): Likewise.
+
 2007-02-11  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
 
        * doc/invoke.texi (Wextra): Delete outdated paragraph.
index 8d8bbe5..6f7c37d 100644 (file)
@@ -3802,6 +3802,27 @@ simplify_relational_operation_1 (enum rtx_code code, enum machine_mode mode,
                                    simplify_gen_binary (XOR, cmp_mode,
                                                         XEXP (op0, 1), op1));
 
+  if (op0code == POPCOUNT && op1 == const0_rtx)
+    switch (code)
+      {
+      case EQ:
+      case LE:
+      case LEU:
+       /* (eq (popcount x) (const_int 0)) -> (eq x (const_int 0)).  */
+       return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
+                                       XEXP (op0, 0), const0_rtx);
+
+      case NE:
+      case GT:
+      case GTU:
+       /* (ne (popcount x) (const_int 0)) -> (ne x (const_int 0)).  */
+       return simplify_gen_relational (EQ, mode, GET_MODE (XEXP (op0, 0)),
+                                       XEXP (op0, 0), const0_rtx);
+
+      default:
+       break;
+      }
+
   return NULL_RTX;
 }
 
@@ -4067,6 +4088,10 @@ simplify_const_relational_operation (enum rtx_code code,
              if (GET_CODE (tem) == ABS)
                return const0_rtx;
            }
+
+         /* Optimize popcount (x) < 0.  */
+         if (GET_CODE (trueop0) == POPCOUNT && trueop1 == const0_rtx)
+           return const_true_rtx;
          break;
 
        case GE:
@@ -4081,6 +4106,10 @@ simplify_const_relational_operation (enum rtx_code code,
              if (GET_CODE (tem) == ABS)
                return const_true_rtx;
            }
+
+         /* Optimize popcount (x) >= 0.  */
+         if (GET_CODE (trueop0) == POPCOUNT && trueop1 == const0_rtx)
+           return const_true_rtx;
          break;
 
        case UNGE:
index b6a6c0a..92670f4 100644 (file)
@@ -1,3 +1,8 @@
+2007-02-11  Roger Sayle  <roger@eyesopen.com>
+
+       * gcc.target/ia64/builtin-popcount-1.c: New test case.
+       * gcc.target/ia64/builtin-popcount-2.c: Likewise.
+
 2007-02-11  Tobias Schlüter  <tobi@gcc.gnu.org>
 
        PR fortran/30478
diff --git a/gcc/testsuite/gcc.target/ia64/builtin-popcount-1.c b/gcc/testsuite/gcc.target/ia64/builtin-popcount-1.c
new file mode 100644 (file)
index 0000000..c9641d0
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler "popcnt" } } */
+
+int foo (int x)
+{
+  return __builtin_popcount (x);
+}
+
diff --git a/gcc/testsuite/gcc.target/ia64/builtin-popcount-2.c b/gcc/testsuite/gcc.target/ia64/builtin-popcount-2.c
new file mode 100644 (file)
index 0000000..50ced72
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler-not "popcnt" } } */
+
+int foo (int x)
+{
+  return __builtin_popcount (x) == 0;
+}
+