re PR target/93002 (while(i--) optimization)
authorJakub Jelinek <jakub@redhat.com>
Fri, 20 Dec 2019 08:22:46 +0000 (09:22 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 20 Dec 2019 08:22:46 +0000 (09:22 +0100)
PR target/93002
* config/i386/i386.md (dec reg; cmp $-1, reg; jne lab): New
define_peephole2.

* gcc.target/i386/pr93002.c: New test.

From-SVN: r279632

gcc/ChangeLog
gcc/config/i386/i386.md
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/i386/pr93002.c [new file with mode: 0644]

index 28f5994ea0dcc3a78cd3a40a867c60dfacb53cf4..e486d7cffde7abe016bfaac288e6dc8f062b47d6 100644 (file)
@@ -1,3 +1,9 @@
+2019-12-20  Jakub Jelinek  <jakub@redhat.com>
+
+       PR target/93002
+       * config/i386/i386.md (dec reg; cmp $-1, reg; jne lab): New
+       define_peephole2.
+
 2019-12-19  Julian Brown  <julian@codesourcery.com>
 
        * gimplify.c (gimplify_omp_var_data): Add GOVD_MAP_HAS_ATTACHMENTS.
index 672fd1c2749b59871f755fb12b1593d819fa6d1f..b0176c8bf944e7cf404330b83f7e31258ce766c5 100644 (file)
   [(set (reg:CC FLAGS_REG)
        (compare:CC (match_dup 0) (match_dup 1)))])
 
+;; decl %eax; cmpl $-1, %eax; jne .Lxx; can be optimized into
+;; subl $1, %eax; jnc .Lxx;
+(define_peephole2
+  [(parallel
+     [(set (match_operand:SWI 0 "general_reg_operand")
+          (plus:SWI (match_dup 0) (const_int -1)))
+      (clobber (reg FLAGS_REG))])
+   (set (reg:CCZ FLAGS_REG)
+       (compare:CCZ (match_dup 0) (const_int -1)))
+   (set (pc)
+       (if_then_else (match_operator 1 "bt_comparison_operator"
+                       [(reg:CCZ FLAGS_REG) (const_int 0)])
+                     (match_operand 2)
+                     (pc)))]
+   "peep2_regno_dead_p (3, FLAGS_REG)"
+   [(parallel
+      [(set (reg:CC FLAGS_REG)
+           (compare:CC (match_dup 0) (const_int 1)))
+       (set (match_dup 0)
+           (minus:SWI (match_dup 0) (const_int 1)))])
+    (set (pc)
+        (if_then_else (match_dup 3)
+                      (match_dup 2)
+                      (pc)))]
+{
+  rtx cc = gen_rtx_REG (CCmode, FLAGS_REG);
+  operands[3] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == NE
+                               ? GEU : LTU, VOIDmode, cc, const0_rtx);
+})
+
 (define_insn "*subsi_3_zext"
   [(set (reg FLAGS_REG)
        (compare (match_operand:SI 1 "register_operand" "0")
index 8c2abb64d37ef9ed4f16ace03652f33d93d987a9..c4231c82de7835622c56f9e3f8775395b8b44c55 100644 (file)
@@ -1,3 +1,8 @@
+2019-12-20  Jakub Jelinek  <jakub@redhat.com>
+
+       PR target/93002
+       * gcc.target/i386/pr93002.c: New test.
+
 2019-12-19  Julian Brown  <julian@codesourcery.com>
 
         * gfortran.dg/goacc/derived-types.f90: New test.
diff --git a/gcc/testsuite/gcc.target/i386/pr93002.c b/gcc/testsuite/gcc.target/i386/pr93002.c
new file mode 100644 (file)
index 0000000..0248fcc
--- /dev/null
@@ -0,0 +1,22 @@
+/* PR target/93002 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler-not "cmp\[^\n\r]*-1" } } */
+
+volatile int sink;
+
+void
+foo (void)
+{
+  unsigned i = 1000;
+  while (i--)
+    sink = i;
+}
+
+void
+bar (void)
+{
+  int i = 2000;
+  while (i--)
+    sink = i;
+}