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 <mengzhuo1203@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Mark Ryan <markdryan@meta.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Joel Sing
2025-12-03 00:42:12 +11:00
parent def4e491be
commit 0a0c9c88fd

View File

@@ -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