From 0a0c9c88fdee8317c1f9e10c5e61d3cf7428c7c1 Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Wed, 3 Dec 2025 00:42:12 +1100 Subject: cmd/internal/obj/riscv: factor out constant materialisation identification Change-Id: Ia7fedd934ecc2b11cb0de445f299dc5c6004e7b0 Reviewed-on: https://go-review.googlesource.com/c/go/+/748960 Reviewed-by: Meng Zhuo LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Mark Ryan Reviewed-by: Cherry Mui --- src/cmd/internal/obj/riscv/obj.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/cmd') diff --git a/src/cmd/internal/obj/riscv/obj.go b/src/cmd/internal/obj/riscv/obj.go index e9005fe0db..ac2b54187d 100644 --- a/src/cmd/internal/obj/riscv/obj.go +++ b/src/cmd/internal/obj/riscv/obj.go @@ -139,11 +139,11 @@ func progedit(ctxt *obj.Link, p *obj.Prog, newprog obj.ProgAlloc) { p.As = AEBREAK case AMOV: - if p.From.Type == obj.TYPE_CONST && p.From.Name == obj.NAME_NONE && p.From.Reg == obj.REG_NONE && int64(int32(p.From.Offset)) != p.From.Offset { - if isShiftConst(p.From.Offset) { + if p.From.Type == obj.TYPE_CONST && p.From.Name == obj.NAME_NONE && p.From.Reg == obj.REG_NONE { + if isMaterialisableConst(p.From.Offset) { break } - // Put >32-bit constants in memory and load them. + // Put non-materialisable constants in memory and load them. p.From.Type = obj.TYPE_MEM p.From.Sym = ctxt.Int64Sym(p.From.Offset) p.From.Name = obj.NAME_EXTERN @@ -3479,6 +3479,17 @@ func isShiftConst(v int64) bool { return ok && (lsh > 0 || rsh > 0) } +// isMaterialisableConst indicates whether a constant can be materialised +// via a small sequence of instructions. +func isMaterialisableConst(v int64) bool { + // Signed 32 bit value that can be constructed with one or two instructions + // (ADDIW or LUI+ADDIW). + if int64(int32(v)) == v { + return true + } + return isShiftConst(v) +} + type instruction struct { p *obj.Prog // Prog that instruction is for as obj.As // Assembler opcode -- cgit v1.3-5-g9baa