[PATCH v2 2/3] riscv: memcpy_noalignment: Fold SZREG/BLOCK_SIZE alignment to single andi
Peter Bergner
bergner@tenstorrent.com
Tue Sep 16 21:54:22 GMT 2025
On 9/3/25 4:45 AM, Yao Zihong wrote:
> Simplify the alignment steps for SZREG and BLOCK_SIZE multiples. The previous
> three-instruction sequences
>
> addi a7, a2, -SZREG
> andi a7, a7, -SZREG
> addi a7, a7, SZREG
>
> and
>
> addi a7, a2, -BLOCK_SIZE
> andi a7, a7, -BLOCK_SIZE
> addi a7, a7, BLOCK_SIZE
>
> are equivalent to a single
>
> andi a7, a2, -SZREG
> andi a7, a2, -BLOCK_SIZE
>
> because SZREG and BLOCK_SIZE are powers of two in this context, making the
> surrounding addi steps cancel out. Folding to one instruction reduces code
> size and favors compressed encodings, with identical semantics.
Agreed. ...except that the andi insns will never be compressed as is.
The "andi a7, a2, -BLOCK_SIZE" cannot be converted to the compressed
form because rd != rs, plus -BLOCK_SIZE is too large for the signed
6-bit offset, so the "and favors compressed encodings" comment isn't
actually true.
The "andi a7, a2, -SZREG" cannot be compressed as is because rd != rs
too. -SZREG is a valid offset, but you'd need to rewrite the code a fair
amount to get rd == rs. Up to you whether you want to attempt that.
So LGTM, but maybe leave the "favors compressed encodings" comment out.
Reviewed-by: Peter Bergner <bergner@tenstorrent.com>
Peter
More information about the Libc-alpha
mailing list