From 9be1921042b6b81b7f16fa8640ddc5868af6d31e Mon Sep 17 00:00:00 2001 From: Ben Shi Date: Mon, 9 Apr 2018 11:12:15 +0000 Subject: [PATCH] cmd/internal/obj/arm64: optimize constant pool "MOVD $0xaaaaaaaa, R2" "MOVD $-0x55555555, R3" For the above instructions, 64-bit constants 0x00000000 aaaaaaaa and 0xffffffff aaaaaaab are stored in the constant pool. This CL optimizes them to "MOVWU $0xaaaaaaaa, R2" "MOVW $-0x05555555, R3" and 32-bit constants 0xaaaaaaaa and 0xaaaaaaab are stored in the constant pool. There is a little size reduction (about total 5KB) in both the go executable and the library files. Change-Id: I7c4bfa6cd9c07da99c69a8f9c15010a0cce3b735 Reviewed-on: https://go-review.googlesource.com/105775 Reviewed-by: Cherry Zhang Run-TryBot: Cherry Zhang TryBot-Result: Gobot Gobot --- src/cmd/internal/obj/arm64/asm7.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/cmd/internal/obj/arm64/asm7.go b/src/cmd/internal/obj/arm64/asm7.go index def589faf4..043a16c45a 100644 --- a/src/cmd/internal/obj/arm64/asm7.go +++ b/src/cmd/internal/obj/arm64/asm7.go @@ -933,6 +933,11 @@ func (c *ctxt7) flushpool(p *obj.Prog, skip int) { } /* + * MOVD foo(SB), R is actually + * MOVD addr, REGTMP + * MOVD REGTMP, R + * where addr is the address of the DWORD containing the address of foo. + * * TODO: hash */ func (c *ctxt7) addpool(p *obj.Prog, a *obj.Addr) { @@ -942,11 +947,17 @@ func (c *ctxt7) addpool(p *obj.Prog, a *obj.Addr) { t.As = AWORD sz := 4 - // MOVD foo(SB), R is actually - // MOVD addr, REGTMP - // MOVD REGTMP, R - // where addr is the address of the DWORD containing the address of foo. - if p.As == AMOVD && a.Type != obj.TYPE_MEM || cls == C_ADDR || cls == C_VCON || lit != int64(int32(lit)) || uint64(lit) != uint64(uint32(lit)) { + if p.As == AMOVD && a.Type == obj.TYPE_CONST { + // simplify MOVD to MOVW/MOVWU to reduce constant pool size + if lit == int64(int32(lit)) { // -0x80000000 ~ 0x7fffffff + p.As = AMOVW + } else if uint64(lit) == uint64(uint32(lit)) { // 0 ~ 0xffffffff + p.As = AMOVWU + } else { // 64-bit + t.As = ADWORD + sz = 8 + } + } else if p.As == AMOVD && a.Type != obj.TYPE_MEM || cls == C_ADDR || cls == C_VCON || lit != int64(int32(lit)) || uint64(lit) != uint64(uint32(lit)) { // conservative: don't know if we want signed or unsigned extension. // in case of ambiguity, store 64-bit t.As = ADWORD -- 2.50.0