[PATCH] RISC-V: Optimize lui and auipc relaxations for undefweak symbols
Nelson Chu
nelson.chu@sifive.com
Wed Sep 18 09:19:00 GMT 2019
Dear binutils and Jim,
This patch try to optimize the LUI and AUIPC relaxations for undefined
weak symbols.
Since the undefined weak symbol’s value is always be zero, we can do
the following linker relaxations:
* For LUI:
lui a0, hi(undef_weak) → addi a0, x0, 0
addi a0, a0, lo(undef_weak)
* For AUIPC:
If the value of (zero - pc) is in range of AUIPC, then the current
implementation in the relocation stage will keep the original pattern
(AUIPC + ADDI).
I think we can do the further optimization in the relaxation stage as
following case:
auipc a0, hi(pc-relative-imm) → lui a0, hi(undef) → addi a0, x0, 0
addi a0, a0, lo(pc-relative-imm) addi a0, a0, lo(undef)
I also update the weakref32/64 testsuites for LD. As mentioned above,
the auipc in these cases will not be converted to LUI since the (value
- pc = -0x90000000 = 0x70000000) is in range of AUIPC. After attaching
the patch, we will get the single Li.
Convert the AUIPC + ADDI
90000000 <_start>:
90000000: 70000797 auipc a5,0x70000
90000004: 00078793 mv a5,a5
...
to
90000000 <_start>:
90000000: 00000793 li a5,0
Thanks
Best Regards
Nelson
============================================================
For the lui and auipc relaxations, since the symbol value of an undefined weak
symbol is always be zero, we can optimize the patterns into a single LI/MV/ADDI
instruction.
bfd/
* elfnn-riscv.c (riscv_pcgp_hi_reloc): Add new bfd_boolean variable.
(riscv_record_pcgp_hi_reloc): Likewise.
(_bfd_riscv_relax_section): Add new variable to let relaxations know
whether the undefined weak symbol optimization is enabled.
(relax_func_t): Likewise.
(_bfd_riscv_relax_call): Likewise.
(_bfd_riscv_relax_tls_le): Likewise.
(_bfd_riscv_relax_align): Likewise.
(_bfd_riscv_relax_delete): Likewise.
(_bfd_riscv_relax_lui): Support the optimization for lui.
(_bfd_riscv_relax_pc): Support the optimization for auipc.
ld/
* testsuite/ld-riscv-elf/weakref32.d: Updated.
* testsuite/ld-riscv-elf/weakref32.s: Updated.
* testsuite/ld-riscv-elf/weakref64.d: Updated.
* testsuite/ld-riscv-elf/weakref64.s: Updated.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-RISC-V-Optimize-lui-and-auipc-relaxations-for-undefw.patch
Type: application/octet-stream
Size: 19735 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20190918/d618706a/attachment.obj>
More information about the Binutils
mailing list