Skip to content

Commit

Permalink
[xstormy16] Efficient HImode rotate left by a single bit.
Browse files Browse the repository at this point in the history
This patch contains some minor tweak to xstormy16's machine description
most significantly providing a pattern for HImode rotate left by a single
bit that requires only two instructions.

unsigned short foo(unsigned short x)
{
  return (x << 1) | (x >> 15);
}

currently with -O2 generates:
foo:    mov r7,r2
        shr r7,#15
        shl r2,#1
        or r2,r7
        ret

with this patch, GCC now generates:
foo:	shl r2,#1 | adc r2,#0
        ret

Additionally neghi2 is converted to a define_insn (so that the RTL
optimizers see the negation semantics), and HImode rotations by
8-bits can now be recognized and implemented using swpb.

2023-04-29  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* config/stormy16/stormy16.md (neghi2): Convert from a define_expand
	to a define_insn.
	(*rotatehi_1): New define_insn for efficient 2 insn sequence.
	(*rotatehi_8, *rotaterthi_8): New define_insn to emit a swpb.

gcc/testsuite/ChangeLog
	* gcc.target/xstormy16/neghi2.c: New test case.
	* gcc.target/xstormy16/rotatehi-1.c: Likewise.
  • Loading branch information
rogersayle committed Apr 29, 2023
1 parent 58f3cbb commit e2b204c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
28 changes: 23 additions & 5 deletions gcc/config/stormy16/stormy16.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,13 @@

;; Negation

(define_expand "neghi2"
[(set (match_operand:HI 0 "register_operand" "")
(not:HI (match_operand:HI 1 "register_operand" "")))
(parallel [(set (match_dup 0) (plus:HI (match_dup 0) (const_int 1)))
(define_insn "neghi2"
[(parallel [(set (match_operand:HI 0 "register_operand" "=r")
(neg:HI (match_operand:HI 1 "register_operand" "0")))
(clobber (reg:BI CARRY_REG))])]
""
"")
"not %0 | add %0,#1"
[(set_attr "length" "4")])

;; ::::::::::::::::::::
;; ::
Expand Down Expand Up @@ -558,6 +558,24 @@
(clobber (reg:BI CARRY_REG))]
""
"shr %0,%2")

;; HImode rotate left by 1 bit
(define_insn "*rotatehi_1"
[(set (match_operand:HI 0 "register_operand" "=r")
(rotate:HI (match_operand:HI 1 "register_operand" "0")
(const_int 1)))
(clobber (reg:BI CARRY_REG))]
""
"shl %0,#1 | adc %0,#0"
[(set_attr "length" "4")])

;; HImode rotate left by 8 bits
(define_insn "*<code>hi_8"
[(set (match_operand:HI 0 "register_operand" "=r")
(any_rotate:HI (match_operand:HI 1 "register_operand" "0")
(const_int 8)))]
""
"swpb %0")

;; ::::::::::::::::::::
;; ::
Expand Down
8 changes: 8 additions & 0 deletions gcc/testsuite/gcc.target/xstormy16/neghi2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* { dg-do compile } */
/* { dg-options "-O2" } */

short neg(short x)
{
return -x;
}
/* { dg-final { scan-assembler "not r2 | add r2,#1" } } */
10 changes: 10 additions & 0 deletions gcc/testsuite/gcc.target/xstormy16/rotatehi-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* { dg-do compile } */
/* { dg-options "-O2" } */

unsigned short foo(unsigned short x)
{
return (x << 1) | (x >> 15);
}

/* { dg-final { scan-assembler "shl r2,#1" } } */
/* { dg-final { scan-assembler "adc r2,#0" } } */

0 comments on commit e2b204c

Please sign in to comment.