* [PATCH] bfin: Popcount-related improvements to machine description.
@ 2021-10-17 13:08 Roger Sayle
2021-10-17 16:20 ` Jeff Law
0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2021-10-17 13:08 UTC (permalink / raw)
To: 'GCC Patches'
[-- Attachment #1: Type: text/plain, Size: 2259 bytes --]
Blackfin processors support a ONES instruction that implements a
32-bit popcount returning a 16-bit result. This instruction was
previously described by GCC's bfin backed using a UNSPEC, but with
this patch uses a POPCOUNT:SI rtx to capture the semantics, allowing
it to evaluated at compile-time. I've decided to keep the instruction
name the same (avoiding any changes to the __builtin_bfin_ones
machinery), but have provided popcountsi2 and popcounthi2 expanders
so that the middle-end can use this instruction to implement
__builtin_popcount (and __builtin_parity).
The new testcase ones.c
short foo ()
{
int t = 5;
short r = __builtin_bfin_ones(t);
return r;
}
previously generated:
_foo: nop;
nop;
R0 = 5 (X);
R0.L = ONES R0;
rts;
with this patch, now generates:
_foo: nop;
nop;
nop;
R0 = 2 (X);
rts;
The new testcase popcount.c
int foo(int x)
{
return __builtin_popcount(x);
}
previously generated:
_foo: [--SP] = RETS;
SP += -12;
call ___popcountsi2;
SP += 12;
RETS = [SP++];
rts;
now generates:
_foo: nop;
nop;
R0.L = ONES R0;
R0 = R0.L (Z);
rts;
And the new testcase parity.c
int foo(int x)
{
return __builtin_parity(x);
}
previously generated:
_foo: [--SP] = RETS;
SP += -12;
call ___paritysi2;
SP += 12;
RETS = [SP++];
rts;
now generates:
_foo: nop;
R1 = 1 (X);
R0.L = ONES R0;
R0 = R1 & R0;
rts;
This patch has been tested on a cross-compiler to bfin-elf hosted
on x86_64-pc-linux-gnu, but without a toolchain, and shows no
regressions in the compile-only parts of the testsuite.
Ok for mainline?
2021-10-17 Roger Sayle <roger@nextmovesoftware.com>
gcc/ChangeLog
* config/bfin/bfin.md (define_constants): Remove UNSPEC_ONES.
(define_insn "ones"): Replace UNSPEC_ONES with a truncate of
a popcount, allowing compile-time evaluation/simplification.
(popcountsi2, popcounthi2): New expanders using a "ones" insn.
gcc/testsuite/ChangeLog
* gcc.target/bfin/ones.c: New test case.
* gcc.target/bfin/parity.c: New test case.
* gcc.target/bfin/ones.c: New test case.
Thanks in advance,
Roger
--
[-- Attachment #2: patchj3.txt --]
[-- Type: text/plain, Size: 1436 bytes --]
diff --git a/gcc/config/bfin/bfin.md b/gcc/config/bfin/bfin.md
index 1ec0bbb..8b311f3 100644
--- a/gcc/config/bfin/bfin.md
+++ b/gcc/config/bfin/bfin.md
@@ -138,8 +138,7 @@
;; Distinguish a 32-bit version of an insn from a 16-bit version.
(UNSPEC_32BIT 11)
(UNSPEC_NOP 12)
- (UNSPEC_ONES 13)
- (UNSPEC_ATOMIC 14)])
+ (UNSPEC_ATOMIC 13)])
(define_constants
[(UNSPEC_VOLATILE_CSYNC 1)
@@ -1398,12 +1397,32 @@
(define_insn "ones"
[(set (match_operand:HI 0 "register_operand" "=d")
- (unspec:HI [(match_operand:SI 1 "register_operand" "d")]
- UNSPEC_ONES))]
+ (truncate:HI
+ (popcount:SI (match_operand:SI 1 "register_operand" "d"))))]
""
"%h0 = ONES %1;"
[(set_attr "type" "alu0")])
+(define_expand "popcountsi2"
+ [(set (match_dup 2)
+ (truncate:HI (popcount:SI (match_operand:SI 1 "register_operand" ""))))
+ (set (match_operand:SI 0 "register_operand")
+ (zero_extend:SI (match_dup 2)))]
+ ""
+{
+ operands[2] = gen_reg_rtx (HImode);
+})
+
+(define_expand "popcounthi2"
+ [(set (match_dup 2)
+ (zero_extend:SI (match_operand:HI 1 "register_operand" "")))
+ (set (match_operand:HI 0 "register_operand")
+ (truncate:HI (popcount:SI (match_dup 2))))]
+ ""
+{
+ operands[2] = gen_reg_rtx (SImode);
+})
+
(define_insn "smaxsi3"
[(set (match_operand:SI 0 "register_operand" "=d")
(smax:SI (match_operand:SI 1 "register_operand" "d")
[-- Attachment #3: ones.c --]
[-- Type: text/plain, Size: 191 bytes --]
/* { dg-do compile } */
/* { dg-options "-O2" } */
short foo ()
{
int t = 5;
short r = __builtin_bfin_ones(t);
return r;
}
/* { dg-final { scan-assembler-not "ONES" } } */
[-- Attachment #4: parity.c --]
[-- Type: text/plain, Size: 156 bytes --]
/* { dg-do compile } */
/* { dg-options "-O2" } */
int foo(int x)
{
return __builtin_parity(x);
}
/* { dg-final { scan-assembler "ONES" } } */
[-- Attachment #5: popcount.c --]
[-- Type: text/plain, Size: 158 bytes --]
/* { dg-do compile } */
/* { dg-options "-O2" } */
int foo(int x)
{
return __builtin_popcount(x);
}
/* { dg-final { scan-assembler "ONES" } } */
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] bfin: Popcount-related improvements to machine description.
2021-10-17 13:08 [PATCH] bfin: Popcount-related improvements to machine description Roger Sayle
@ 2021-10-17 16:20 ` Jeff Law
0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2021-10-17 16:20 UTC (permalink / raw)
To: Roger Sayle, 'GCC Patches'
On 10/17/2021 7:08 AM, Roger Sayle wrote:
> Blackfin processors support a ONES instruction that implements a
> 32-bit popcount returning a 16-bit result. This instruction was
> previously described by GCC's bfin backed using a UNSPEC, but with
> this patch uses a POPCOUNT:SI rtx to capture the semantics, allowing
> it to evaluated at compile-time. I've decided to keep the instruction
> name the same (avoiding any changes to the __builtin_bfin_ones
> machinery), but have provided popcountsi2 and popcounthi2 expanders
> so that the middle-end can use this instruction to implement
> __builtin_popcount (and __builtin_parity).
>
> The new testcase ones.c
> short foo ()
> {
> int t = 5;
> short r = __builtin_bfin_ones(t);
> return r;
> }
>
> previously generated:
> _foo: nop;
> nop;
> R0 = 5 (X);
> R0.L = ONES R0;
> rts;
>
> with this patch, now generates:
> _foo: nop;
> nop;
> nop;
> R0 = 2 (X);
> rts;
>
> The new testcase popcount.c
> int foo(int x)
> {
> return __builtin_popcount(x);
> }
>
> previously generated:
> _foo: [--SP] = RETS;
> SP += -12;
> call ___popcountsi2;
> SP += 12;
> RETS = [SP++];
> rts;
>
> now generates:
> _foo: nop;
> nop;
> R0.L = ONES R0;
> R0 = R0.L (Z);
> rts;
>
> And the new testcase parity.c
> int foo(int x)
> {
> return __builtin_parity(x);
> }
>
> previously generated:
> _foo: [--SP] = RETS;
> SP += -12;
> call ___paritysi2;
> SP += 12;
> RETS = [SP++];
> rts;
>
> now generates:
> _foo: nop;
> R1 = 1 (X);
> R0.L = ONES R0;
> R0 = R1 & R0;
> rts;
>
>
> This patch has been tested on a cross-compiler to bfin-elf hosted
> on x86_64-pc-linux-gnu, but without a toolchain, and shows no
> regressions in the compile-only parts of the testsuite.
> Ok for mainline?
>
>
> 2021-10-17 Roger Sayle <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
> * config/bfin/bfin.md (define_constants): Remove UNSPEC_ONES.
> (define_insn "ones"): Replace UNSPEC_ONES with a truncate of
> a popcount, allowing compile-time evaluation/simplification.
> (popcountsi2, popcounthi2): New expanders using a "ones" insn.
>
> gcc/testsuite/ChangeLog
> * gcc.target/bfin/ones.c: New test case.
> * gcc.target/bfin/parity.c: New test case.
> * gcc.target/bfin/ones.c: New test case.
OK
jeff
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-10-17 16:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-17 13:08 [PATCH] bfin: Popcount-related improvements to machine description Roger Sayle
2021-10-17 16:20 ` Jeff Law
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).