From 9921bbf9b2e27568d952fe6ee5bc083c93bbf7fd Mon Sep 17 00:00:00 2001 From: Wilco Dijkstra Date: Wed, 12 Feb 2020 18:19:25 +0000 Subject: [PATCH] [AArch64] Improve popcount expansion The popcount expansion uses umov to extend the result and move it back to the integer register file. If we model ADDV as a zero-extending operation, fmov can be used to move back to the integer side. This results in a ~0.5% speedup on deepsjeng on Cortex-A57. A typical __builtin_popcount expansion is now: fmov s0, w0 cnt v0.8b, v0.8b addv b0, v0.8b fmov w0, s0 gcc/ * config/aarch64/aarch64-simd.md (aarch64_zero_extend_reduc_plus_): New pattern. * config/aarch64/aarch64.md (popcount2): Use it instead of generating separate ADDV and zero_extend patterns. * config/aarch64/iterators.md (VDQV_E): New iterator. testsuite/ * gcc.target/aarch64/popcnt2.c: New test. --- gcc/ChangeLog | 8 ++++++++ gcc/config/aarch64/aarch64-simd.md | 11 +++++++++++ gcc/config/aarch64/aarch64.md | 4 +--- gcc/config/aarch64/iterators.md | 3 +++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.target/aarch64/popcnt2.c | 21 +++++++++++++++++++++ 6 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.target/aarch64/popcnt2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 9ff0a943e4f..36e75b35fb8 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2020-02-12 Wilco Dijkstra + + * config/aarch64/aarch64-simd.md + (aarch64_zero_extend_reduc_plus_): New pattern. + * config/aarch64/aarch64.md (popcount2): Use it instead of + generating separate ADDV and zero_extend patterns. + * config/aarch64/iterators.md (VDQV_E): New iterator. + 2020-02-12 Jeff Law * config/h8300/h8300.md (cpymemsi, movmd): Remove dead patterns, diff --git a/gcc/config/aarch64/aarch64-simd.md b/gcc/config/aarch64/aarch64-simd.md index 4c651f45d0c..33d705a76ef 100644 --- a/gcc/config/aarch64/aarch64-simd.md +++ b/gcc/config/aarch64/aarch64-simd.md @@ -2474,6 +2474,17 @@ [(set_attr "type" "neon_reduc_add")] ) +;; ADDV with result zero-extended to SI/DImode (for popcount). +(define_insn "aarch64_zero_extend_reduc_plus_" + [(set (match_operand:GPI 0 "register_operand" "=w") + (zero_extend:GPI + (unspec: [(match_operand:VDQV_E 1 "register_operand" "w")] + UNSPEC_ADDV)))] + "TARGET_SIMD" + "add\\t%0, %1." + [(set_attr "type" "neon_reduc_add")] +) + (define_insn "aarch64_reduc_plus_internalv2si" [(set (match_operand:V2SI 0 "register_operand" "=w") (unspec:V2SI [(match_operand:V2SI 1 "register_operand" "w")] diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md index fbf90d907ba..7ad4e918578 100644 --- a/gcc/config/aarch64/aarch64.md +++ b/gcc/config/aarch64/aarch64.md @@ -4848,7 +4848,6 @@ { rtx v = gen_reg_rtx (V8QImode); rtx v1 = gen_reg_rtx (V8QImode); - rtx r = gen_reg_rtx (QImode); rtx in = operands[1]; rtx out = operands[0]; if(mode == SImode) @@ -4862,8 +4861,7 @@ } emit_move_insn (v, gen_lowpart (V8QImode, in)); emit_insn (gen_popcountv8qi2 (v1, v)); - emit_insn (gen_reduc_plus_scal_v8qi (r, v1)); - emit_insn (gen_zero_extendqi2 (out, r)); + emit_insn (gen_aarch64_zero_extend_reduc_plus_v8qi (out, v1)); DONE; }) diff --git a/gcc/config/aarch64/iterators.md b/gcc/config/aarch64/iterators.md index 7c62f164347..d17d79a30da 100644 --- a/gcc/config/aarch64/iterators.md +++ b/gcc/config/aarch64/iterators.md @@ -208,6 +208,9 @@ ;; Advanced SIMD modes (except V2DI) for Integer reduction across lanes. (define_mode_iterator VDQV_S [V8QI V16QI V4HI V8HI V4SI]) +;; Advanced SIMD modes for Integer reduction across lanes (zero/sign extended). +(define_mode_iterator VDQV_E [V8QI V16QI V4HI V8HI]) + ;; All double integer narrow-able modes. (define_mode_iterator VDN [V4HI V2SI DI]) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e2f590b48f4..18d643485d6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2020-02-12 Wilco Dijkstra + + * gcc.target/aarch64/popcnt2.c: New test. + 2020-02-12 Marek Polacek PR c++/88819 diff --git a/gcc/testsuite/gcc.target/aarch64/popcnt2.c b/gcc/testsuite/gcc.target/aarch64/popcnt2.c new file mode 100644 index 00000000000..e321858afa4 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/popcnt2.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +unsigned +foo (int x) +{ + return __builtin_popcount (x); +} + +unsigned long +foo1 (int x) +{ + return __builtin_popcount (x); +} + +/* { dg-final { scan-assembler-not {popcount} } } */ +/* { dg-final { scan-assembler-times {cnt\t} 2 } } */ +/* { dg-final { scan-assembler-times {fmov} 4 } } */ +/* { dg-final { scan-assembler-not {umov} } } */ +/* { dg-final { scan-assembler-not {uxtw} } } */ +/* { dg-final { scan-assembler-not {sxtw} } } */ -- 2.30.2