Conversation

feel like binutils could exploit the little-known algebraic property of "commutativity" to make this smaller

2
0
0

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

1
0
0
@wren6991 ya, probably. I don't think anyone's really looking at the C sub-extensions all that closely -- or really any of the non-server stuff in general, that's where the vast majority of the upstream effort ended up going over the past few years. The embedded platforms tend to be a mess of vendor-specific extensions, it's just too chaotic for us to do a good job.

So if you file a GCC missed-optimization bug this will probably get fixed, but I'd expect there's a ton more of this kind of stuff floating around.

FWIW I think we should move away from the assembler tricks, but that's kind of a different discussion...
1
0
0

@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

2
0
0
@wren6991 most things don't need to be trivial anymore, they can just be DCO like normal projects.
0
0
1
@wren6991 and on the jump thing: you're probably right, there's historically been a ton of issues around C-type instructions where we accept stuff we shouldn't and just do something arbitrary -- that's true in binutils, but if you start looking closely you'll see it in hardware too.

Again something to fix, it's probably another trivial bug somewhere. It's really just a lack of review/testing, again there's just too many sub-targets to try and get everything up to a good quality bar.
0
0
0

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

1
0
0

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.

0
0
0