mold icon indicating copy to clipboard operation
mold copied to clipboard

RV64: Relax AUIPC+JALR to C.JAL

Open rui314 opened this issue 3 years ago • 1 comments

Currently, we relax AUIPC+JALR only to JAL, but if the C extension is available and the displacement is within its range, we should optimize it further to C.JAL. Doing this should save extra one byte for each instruction.

rui314 avatar Apr 25 '22 02:04 rui314

It looks like most if not all Unix-like systems assume that the C extension is supported, so we may not have to detect if C.JAL is allowed. Instead, we may want to assume that C.JAL is always usable.

rui314 avatar Apr 29 '22 04:04 rui314

It seems that C.JAL is an RV32IC-only instruction, so this optimization may not work with RV64.

ksco avatar Aug 26 '22 09:08 ksco

Are you sure? I thought that it's also a part of the RV64 C extension.

rui314 avatar Aug 27 '22 02:08 rui314

Yes, C.ADDIW is an RV64-only instruction, they share the same opcode.

Copied from the Spec:

C.JAL is an RV32C-only instruction that performs the same operation as C.J, but additionally writes the address of the instruction following the jump (pc+2) to the link register, x1. C.JAL expands to jal x1, offset.

ksco avatar Aug 27 '22 03:08 ksco

Oh, that's true. Thank you for pointing it out!

rui314 avatar Aug 27 '22 03:08 rui314