Patchwork [4.8,i386] : Enable post-reload compare optimization pass (PR28685)

login
register
mail settings
Submitter Uros Bizjak
Date Feb. 5, 2012, 3:27 p.m.
Message ID <CAFULd4Z+BZ3-rLckm0s5cQtjnrJ8evAneccZbNvwS78ynLO5vA@mail.gmail.com>
Download mbox | patch
Permalink /patch/139651/
State New
Headers show

Comments

Uros Bizjak - Feb. 5, 2012, 3:27 p.m.
Hello!

Attached patch enables post-reload compare optimization pass for x86 targets.

As shown in following test, patch removes redundant compare:

--cut here--
int test(int a, int b)
{
  int lt = a < b;
  int eq = a == b;
  if (lt)
    return 1;
  return eq;
}
--cut here--

gcc -O2 on x86_64, a cmove target:

test:
        xorl    %edx, %edx
        cmpl    %esi, %edi
        movl    $1, %eax
        sete    %dl
        cmovge  %edx, %eax
        ret

gcc-O2 -fno-compare-elim:

test:
        xorl    %edx, %edx
        cmpl    %esi, %edi
        movl    $1, %eax
        sete    %dl
        cmpl    %esi, %edi
        cmovge  %edx, %eax
        ret

2012-02-05  Uros Bizjak  <ubizjak@gmail.com>

	PR target/28685
	* config/i386/i386.c (TARGET_FLAGS_REGNUM): New.

testsuite/ChangeLog:

	PR target/28685
	* gcc.target/i386/pr28685.c: New test.

The patch was tested on x86_64-pc-linux-gnu {,-m32}. The patch will be
committed to 4.8 once stage 1 opens.

Uros.
Steven Bosscher - Feb. 5, 2012, 5:27 p.m.
On Sun, Feb 5, 2012 at 4:27 PM, Uros Bizjak <ubizjak@gmail.com> wrote:
> 2012-02-05  Uros Bizjak  <ubizjak@gmail.com>
>
>        PR target/28685
>        * config/i386/i386.c (TARGET_FLAGS_REGNUM): New.
>

Hmm, how is this (apparently new in 2011) TARGET_FLAGS_REGNUM
different from the older targetm.fixed_condition_code_regs?

Ciao!
Steven
Uros Bizjak - Feb. 5, 2012, 5:34 p.m.
On Sun, Feb 5, 2012 at 6:27 PM, Steven Bosscher <stevenb.gcc@gmail.com> wrote:

>>        PR target/28685
>>        * config/i386/i386.c (TARGET_FLAGS_REGNUM): New.
>>
>
> Hmm, how is this (apparently new in 2011) TARGET_FLAGS_REGNUM
> different from the older targetm.fixed_condition_code_regs?

This is how backend enables the pass, please see
gate_compare_elim_after_reload function in gcc/compare-elim.c.

FWIW, the value could be anything != INVALID_REGNUM.

Uros.
Richard Henderson - Feb. 6, 2012, 8:59 p.m.
On 02/05/2012 07:27 AM, Uros Bizjak wrote:
> Hello!
> 
> Attached patch enables post-reload compare optimization pass for x86 targets.

Hmm.  Well, the only thing that's going to work for x86 is the double-compare
elimination portion.

If we want to use this pass for x86, then for 4.8 we should also fix the
discrepancy between the compare-elim canonical

  [(operate)
   (set-cc)]

and the combine canonical

  [(set-cc)
   (operate)]

(Because of the simplicity of the substitution in compare-elim, I prefer
the former as the canonical canonical.)

And, really, we ought to come up with some trick to eliminate some of the
redundancy in patterns in the md file too.


r~
Uros Bizjak - Feb. 6, 2012, 9:30 p.m.
On Mon, Feb 6, 2012 at 9:59 PM, Richard Henderson <rth@redhat.com> wrote:
> On 02/05/2012 07:27 AM, Uros Bizjak wrote:
>> Hello!
>>
>> Attached patch enables post-reload compare optimization pass for x86 targets.
>
> Hmm.  Well, the only thing that's going to work for x86 is the double-compare
> elimination portion.
>
> If we want to use this pass for x86, then for 4.8 we should also fix the
> discrepancy between the compare-elim canonical
>
>  [(operate)
>   (set-cc)]
>
> and the combine canonical
>
>  [(set-cc)
>   (operate)]
>
> (Because of the simplicity of the substitution in compare-elim, I prefer
> the former as the canonical canonical.)

You are probably referring to following testcase:

--cut here--
int test (int a, int b)
{
  int lt = a + b < 0;
  int eq = a + b == 0;
  if (lt)
    return 1;
  return eq;
}
--cut here--

where combine creates:

Trying 8 -> 9:
Successfully matched this instruction:
(parallel [
        (set (reg:CCZ 17 flags)
            (compare:CCZ (plus:SI (reg/v:SI 63 [ a ])
                    (reg/v:SI 64 [ b ]))
                (const_int 0 [0])))
        (set (reg:SI 60 [ D.1710 ])
            (plus:SI (reg/v:SI 63 [ a ])
                (reg/v:SI 64 [ b ])))
    ])

Uros.
Mike Stump - Feb. 6, 2012, 9:34 p.m.
On Feb 6, 2012, at 12:59 PM, Richard Henderson wrote:
> And, really, we ought to come up with some trick to eliminate some of the
> redundancy in patterns in the md file too.

:-)  That'd be awesome...

Patch

Index: config/i386/i386.c
===================================================================
--- config/i386/i386.c	(revision 183904)
+++ config/i386/i386.c	(working copy)
@@ -38685,6 +38685,9 @@ 
 #undef TARGET_EXPAND_TO_RTL_HOOK
 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
 
+#undef TARGET_FLAGS_REGNUM
+#define TARGET_FLAGS_REGNUM FLAGS_REG
+
 #undef TARGET_LEGITIMATE_ADDRESS_P
 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
 
Index: testsuite/gcc.target/i386/pr28685.c
===================================================================
--- testsuite/gcc.target/i386/pr28685.c	(revision 0)
+++ testsuite/gcc.target/i386/pr28685.c	(revision 0)
@@ -0,0 +1,12 @@ 
+/* { dg-options "-O2" } */
+
+int test(int a, int b)
+{
+  int lt = a < b;
+  int eq = a == b;
+  if (lt)
+    return 1;
+  return eq;
+}
+
+/* { dg-final { scan-assembler-times "cmp" 1 } } */