1
0
mirror of https://github.com/golang/go synced 2024-11-26 05:07:59 -07:00

cmd/internal/obj/riscv: simplify addition with constant

This CL simplifies riscv addition (add r, imm) to
(ADDI (ADDI r, imm/2), imm-imm/2) if imm is in specific ranges.
(-4096 <= imm <= -2049 or 2048 <= imm <= 4094)

There is little impact to the go1 benchmark, while the total
size of pkg/linux_riscv64 decreased by about 11KB.

Change-Id: I236eb8af3b83bb35ce9c0b318fc1d235e8ab9a4e
GitHub-Last-Rev: a2f56a0763
GitHub-Pull-Request: golang/go#48110
Reviewed-on: https://go-review.googlesource.com/c/go/+/346689
Run-TryBot: Ben Shi <powerman1st@163.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Joel Sing <joel@sing.id.au>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Trust: Michael Munday <mike.munday@lowrisc.org>
This commit is contained in:
Ben Shi 2021-09-02 00:06:42 +00:00 committed by Joel Sing
parent 4fb79569d2
commit 37d4532867
2 changed files with 29 additions and 0 deletions

View File

@ -774,6 +774,27 @@ func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
break // no need to split
}
// Split into two additions if possible.
imm := q.From.Offset
const minInt12, maxInt12 = -(1 << 11), (1 << 11) - 1
if q.As == AADDI && 2*minInt12 <= imm && imm <= 2*maxInt12 {
imm0, imm1 := imm/2, imm-imm/2
// ADDI $(imm/2), REG, TO
p.Spadj = 0 // needed if TO is SP
p.As = AADDI
p.From = obj.Addr{Type: obj.TYPE_CONST, Offset: imm0}
p.Reg = q.Reg
p.To = q.To
p = obj.Appendp(p, newprog)
// ADDI $(imm-imm/2), TO, TO
p.Spadj = q.Spadj
p.As = AADDI
p.From = obj.Addr{Type: obj.TYPE_CONST, Offset: imm1}
p.Reg = q.To.Reg
p.To = q.To
break
}
p.As = ALUI
p.From = obj.Addr{Type: obj.TYPE_CONST, Offset: high}
p.Reg = 0

View File

@ -575,3 +575,11 @@ func constantFold3(i, j int) int {
r := (5 * i) * (6 * j)
return r
}
func addConst(i int64) (int64, int64) {
// riscv64:`ADDI`,-`LUI`
a := i + 3001
// riscv64:`LUI`,`ADDIW`
b := i + 5009
return a, b
}