diff mbox

Canonicalize compares in combine [3/3] ARM backend part

Message ID 4DB19D15.9020509@codesourcery.com
State New
Headers show

Commit Message

Chung-Lin Tang April 22, 2011, 3:21 p.m. UTC
Hi Richard, this part's for you.

The ARM backend changes needed are very little after the prior patches,
basically just a case in arm_canonicalize_comparison() to detect
(zero_extend:SI (subreg:QI (reg:SI ...) 0)), and swap it into (and:SI
(reg:SI) #255).

Had we not tried the combine modifications, this testcase probably could
have also be solved by implementing another version of the corresponding
*andsi3_compare0/_scratch patterns, with ZERO_EXTEND in the body, and
"ands" in the output assembly. Maybe that's an acceptable solution too...

About the (ab)use of CANONICALIZE_COMPARISON, if it really should be
another macro/hook, then this ARM patch will need updating, but the code
should be similar.

Thanks,
Chung-Lin

Comments

Chung-Lin Tang May 9, 2011, 3:02 p.m. UTC | #1
Ping.

On 04/22/2011 11:21 PM, Chung-Lin Tang wrote:
> Hi Richard, this part's for you.
> 
> The ARM backend changes needed are very little after the prior patches,
> basically just a case in arm_canonicalize_comparison() to detect
> (zero_extend:SI (subreg:QI (reg:SI ...) 0)), and swap it into (and:SI
> (reg:SI) #255).
> 
> Had we not tried the combine modifications, this testcase probably could
> have also be solved by implementing another version of the corresponding
> *andsi3_compare0/_scratch patterns, with ZERO_EXTEND in the body, and
> "ands" in the output assembly. Maybe that's an acceptable solution too...
> 
> About the (ab)use of CANONICALIZE_COMPARISON, if it really should be
> another macro/hook, then this ARM patch will need updating, but the code
> should be similar.
> 
> Thanks,
> Chung-Lin
Chung-Lin Tang June 2, 2011, 4:59 a.m. UTC | #2
Ping.
On 2011/5/9 11:02 PM, Chung-Lin Tang wrote:
> Ping.
> 
> On 04/22/2011 11:21 PM, Chung-Lin Tang wrote:
>> Hi Richard, this part's for you.
>>
>> The ARM backend changes needed are very little after the prior patches,
>> basically just a case in arm_canonicalize_comparison() to detect
>> (zero_extend:SI (subreg:QI (reg:SI ...) 0)), and swap it into (and:SI
>> (reg:SI) #255).
>>
>> Had we not tried the combine modifications, this testcase probably could
>> have also be solved by implementing another version of the corresponding
>> *andsi3_compare0/_scratch patterns, with ZERO_EXTEND in the body, and
>> "ands" in the output assembly. Maybe that's an acceptable solution too...
>>
>> About the (ab)use of CANONICALIZE_COMPARISON, if it really should be
>> another macro/hook, then this ARM patch will need updating, but the code
>> should be similar.
>>
>> Thanks,
>> Chung-Lin
>
Richard Earnshaw June 15, 2011, 1:12 p.m. UTC | #3
On 22/04/11 16:21, Chung-Lin Tang wrote:
> Hi Richard, this part's for you.
> 
> The ARM backend changes needed are very little after the prior patches,
> basically just a case in arm_canonicalize_comparison() to detect
> (zero_extend:SI (subreg:QI (reg:SI ...) 0)), and swap it into (and:SI
> (reg:SI) #255).
> 
> Had we not tried the combine modifications, this testcase probably could
> have also be solved by implementing another version of the corresponding
> *andsi3_compare0/_scratch patterns, with ZERO_EXTEND in the body, and
> "ands" in the output assembly. Maybe that's an acceptable solution too...
> 
> About the (ab)use of CANONICALIZE_COMPARISON, if it really should be
> another macro/hook, then this ARM patch will need updating, but the code
> should be similar.
> 
> Thanks,
> Chung-Lin
> 
> 
> 3-arm-parts.diff
> 
> 
> Index: config/arm/arm.c
> ===================================================================
> --- config/arm/arm.c	(revision 172860)
> +++ config/arm/arm.c	(working copy)
> @@ -3276,6 +3276,19 @@
>        return code;
>      }
>  
> +  /* If *op0 is (zero_extend:SI (subreg:QI (reg:SI) 0)) and comparing
> +     with const0_rtx, change it to (and:SI (reg:SI) (const_int 255)),
> +     to facilitate possible combining with a cmp into 'ands'.  */
> +  if (mode == SImode
> +      && GET_CODE (*op0) == ZERO_EXTEND
> +      && GET_CODE (XEXP (*op0, 0)) == SUBREG
> +      && GET_MODE (XEXP (*op0, 0)) == QImode
> +      && GET_MODE (SUBREG_REG (XEXP (*op0, 0))) == SImode
> +      && SUBREG_BYTE (XEXP (*op0, 0)) == 0
> +      && *op1 == const0_rtx)
> +    *op0 = gen_rtx_AND (SImode, SUBREG_REG (XEXP (*op0, 0)),
> +			GEN_INT (255));
> +

This is wrong for big-endian code.  You should use subreg_lowpart_p to
check the subreg expression (after you've checked that you do have a
subreg, of course).

R.
diff mbox

Patch

Index: config/arm/arm.c
===================================================================
--- config/arm/arm.c	(revision 172860)
+++ config/arm/arm.c	(working copy)
@@ -3276,6 +3276,19 @@ 
       return code;
     }
 
+  /* If *op0 is (zero_extend:SI (subreg:QI (reg:SI) 0)) and comparing
+     with const0_rtx, change it to (and:SI (reg:SI) (const_int 255)),
+     to facilitate possible combining with a cmp into 'ands'.  */
+  if (mode == SImode
+      && GET_CODE (*op0) == ZERO_EXTEND
+      && GET_CODE (XEXP (*op0, 0)) == SUBREG
+      && GET_MODE (XEXP (*op0, 0)) == QImode
+      && GET_MODE (SUBREG_REG (XEXP (*op0, 0))) == SImode
+      && SUBREG_BYTE (XEXP (*op0, 0)) == 0
+      && *op1 == const0_rtx)
+    *op0 = gen_rtx_AND (SImode, SUBREG_REG (XEXP (*op0, 0)),
+			GEN_INT (255));
+
   /* Comparisons smaller than DImode.  Only adjust comparisons against
      an out-of-range constant.  */
   if (GET_CODE (*op1) != CONST_INT
Index: testsuite/gcc.target/arm/combine-movs.c
===================================================================
--- testsuite/gcc.target/arm/combine-movs.c	(revision 0)
+++ testsuite/gcc.target/arm/combine-movs.c	(revision 0)
@@ -0,0 +1,10 @@ 
+/* { dg-options "-O" }  */
+
+void foo (unsigned long r[], unsigned int d)
+{
+  int i, n = d / 32;
+  for (i = 0; i < n; ++i)
+    r[i] = 0;
+}
+
+/* { dg-final { scan-assembler "movs\tr\[0-9\]" } } */
Index: testsuite/gcc.target/arm/unsigned-extend-2.c
===================================================================
--- testsuite/gcc.target/arm/unsigned-extend-2.c	(revision 0)
+++ testsuite/gcc.target/arm/unsigned-extend-2.c	(revision 0)
@@ -0,0 +1,17 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -march=armv6" } */
+
+unsigned short foo (unsigned short x)
+{
+  unsigned char i = 0;
+  for (i = 0; i < 8; i++)
+    {
+      x >>= 1;
+      x &= 0x7fff;
+    }
+  return x;
+}
+
+/* { dg-final { scan-assembler "ands" } } */
+/* { dg-final { scan-assembler-not "uxtb" } } */
+/* { dg-final { scan-assembler-not "cmp" } } */