https://gcc.gnu.org/g:9ddcaf3528a14f44c1bfb6512a8ced8d4a89cb6f

commit r17-6155-g9ddcaf3528a14f44c1bfb6512a8ced8d4a89cb6f
Author: Kishan Parmar <[email protected]>
Date:   Thu Nov 12 12:05:27 2026 +0530

    rs6000: Improve codegen for __builtin_bswap16 on GPRs [PR121076]
    
    The fallback expansion of __builtin_bswap16 on pre-Power10 used a
    sequence of multiple rlwinm/or instructions:
    
      mr      r9,r3
      rlwinm  r3,r9,24,24,31
      rlwinm  r10,r9,8,16,23
      or      r3,r3,r10
      rlwinm  r3,r3,0,0xffff
    
    This was functionally correct but less optimal.
    
    Rewrite the splitter to use a rotate-insert idiom, producing:
    
      mr      r9,r3
      slwi    r3,r9,8
      rlwimi  r3,r9,24,24,31
      rlwinm  r3,r3,0,0xffff
    
    This sequence avoids the extra mask/or operations.
    
    2026-11-12  Kishan Parmar  <[email protected]>
    
    gcc/
            PR target/121076
            * config/rs6000/rs6000.md (bswaphi2_reg): Replace
            rotate-mask/rotate-mask/or sequence with shift and
            rotate-mask-insert sequence.
    
    gcc/testsuite/
            * gcc.target/powerpc/pr121076.c: New test.

Diff:
---
 gcc/config/rs6000/rs6000.md                 | 17 +++++++----------
 gcc/testsuite/gcc.target/powerpc/pr121076.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
index d2d2a95eaa9a..85ba7e01113a 100644
--- a/gcc/config/rs6000/rs6000.md
+++ b/gcc/config/rs6000/rs6000.md
@@ -2802,21 +2802,18 @@
    xxbrh %x0,%x1"
   "reload_completed && !TARGET_POWER10 && int_reg_operand (operands[0], 
HImode)"
   [(set (match_dup 3)
-       (and:SI (lshiftrt:SI (match_dup 4)
-                            (const_int 8))
-               (const_int 255)))
-   (set (match_dup 2)
-       (and:SI (ashift:SI (match_dup 4)
-                          (const_int 8))
-               (const_int 65280)))             ;; 0xff00
+       (ashift:SI (match_dup 4)
+                  (const_int 8)))
    (set (match_dup 3)
-       (ior:SI (match_dup 3)
-               (match_dup 2)))]
+       (ior:SI (and:SI (match_dup 3)
+                       (const_int -256))
+               (and:SI (lshiftrt:SI (match_dup 4) (const_int 8))
+                       (const_int 255))))]         ;; 0x00ff
 {
   operands[3] = simplify_gen_subreg (SImode, operands[0], HImode, 0);
   operands[4] = simplify_gen_subreg (SImode, operands[1], HImode, 0);
 }
-  [(set_attr "length" "*,12,*")
+  [(set_attr "length" "*,8,*")
    (set_attr "type" "shift,*,vecperm")
    (set_attr "isa" "p10,*,p9v")])
 
diff --git a/gcc/testsuite/gcc.target/powerpc/pr121076.c 
b/gcc/testsuite/gcc.target/powerpc/pr121076.c
new file mode 100644
index 000000000000..09d2ffb4e86a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pr121076.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-mcpu=power9 -O2" } */
+
+unsigned short
+swap (unsigned short in)
+{
+  return __builtin_bswap16 (in);
+}
+
+/* { dg-final { scan-assembler-times "slwi" 1 } } */
+/* { dg-final { scan-assembler-times "rlwimi" 1 } } */
+/* { dg-final { scan-assembler-times "rlwinm" 1 } } */
+/* { dg-final { scan-assembler-not "or" } } */
+

Reply via email to