]> git.ipfire.org Git - thirdparty/gcc.git/commit
LoongArch: Replace -mexplicit-relocs=auto simple-used address peephole2 with combine
authorXi Ruoyao <xry111@xry111.site>
Mon, 11 Dec 2023 20:54:21 +0000 (04:54 +0800)
committerXi Ruoyao <xry111@xry111.site>
Fri, 29 Dec 2023 12:07:53 +0000 (20:07 +0800)
commit8b61d109b130f0e6551803cc30f3c607d4fde81c
treea439e2a2aaec647d1a56f11ffca7ceac3f33294d
parent1e7f9abb892443719c82bb17910caa8fb5eeec15
LoongArch: Replace -mexplicit-relocs=auto simple-used address peephole2 with combine

The problem with peephole2 is it uses a naive sliding-window algorithm
and misses many cases.  For example:

    float a[10000];
    float t() { return a[0] + a[8000]; }

is compiled to:

    la.local    $r13,a
    la.local    $r12,a+32768
    fld.s       $f1,$r13,0
    fld.s       $f0,$r12,-768
    fadd.s      $f0,$f1,$f0

by trunk.  But as we've explained in r14-4851, the following would be
better with -mexplicit-relocs=auto:

    pcalau12i   $r13,%pc_hi20(a)
    pcalau12i   $r12,%pc_hi20(a+32000)
    fld.s       $f1,$r13,%pc_lo12(a)
    fld.s       $f0,$r12,%pc_lo12(a+32000)
    fadd.s      $f0,$f1,$f0

However the sliding-window algorithm just won't detect the pcalau12i/fld
pair to be optimized.  Use a define_insn_and_rewrite in combine pass
will work around the issue.

gcc/ChangeLog:

* config/loongarch/predicates.md
(symbolic_pcrel_offset_operand): New define_predicate.
(mem_simple_ldst_operand): Likewise.
* config/loongarch/loongarch-protos.h
(loongarch_rewrite_mem_for_simple_ldst): Declare.
* config/loongarch/loongarch.cc
(loongarch_rewrite_mem_for_simple_ldst): Implement.
* config/loongarch/loongarch.md (simple_load<mode>): New
define_insn_and_rewrite.
(simple_load_<su>ext<SUBDI:mode><GPR:mode>): Likewise.
(simple_store<mode>): Likewise.
(define_peephole2): Remove la.local/[f]ld peepholes.

gcc/testsuite/ChangeLog:

* gcc.target/loongarch/explicit-relocs-auto-single-load-store-2.c:
New test.
* gcc.target/loongarch/explicit-relocs-auto-single-load-store-3.c:
New test.
gcc/config/loongarch/loongarch-protos.h
gcc/config/loongarch/loongarch.cc
gcc/config/loongarch/loongarch.md
gcc/config/loongarch/predicates.md
gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-single-load-store-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-single-load-store-3.c [new file with mode: 0644]