feel like binutils could exploit the little-known algebraic property of "commutativity" to make this smaller
It already does the equivalent transformation for `add` so this is indeed a binutils issue not a GCC issue. I guess it was just missed when they added the Zcb compressions
@palmer Turns out to be a really easy fix. I haven't done the GNU copyright paperwork, but do you think this patch would be accepted as a tiny-change?
Also I agree, the asm compression stuff is pretty leaky, and also poorly specified. Like if I have a c.j to a different section this seems to actually unconditionally emit a 32-bit opcode with an R_RISCV_JAL
Today on "why the assembler do that": using an uncompressed alias for load-immediate 0 into an x8-x15 register. There are a ton of these in MicroPython firmware. Weirdly they all seem to be in the soft float lib.
I assume they're going in as `addi` instead of `li`, and binutils doesn't have a compressed alias for that? Compressing these would save 254 bytes in a 376 kB binary, which isn't much but it's free real estate
So yeah, culprit: missing aliases for `mv rd, zero` and `move rd, zero` -> `c.li rd, 0`.
I don't love the way this is all hacked in with instruction aliases, but IMO if objdump spits it back out as `li rd, 0`, and that instruction is compressible, it should be compressed by the assembler too.