New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: useless copies inserted by the register allocator #14504
Comments
Yes, we need to backwards-propagate more "I would like the result in this register" information. I've started on a CL for this a while ago but haven't gotten back to it. |
CL https://golang.org/cl/19952 mentions this issue. |
Here is another test case, originally reported at: https://groups.google.com/forum/#!topic/golang-dev/LSD504gbeBM package fac
func Fac(n int64) int64 {
if n < 2 {
return 1
}
return n * Fac(n-1)
} Assembly dump with 0x0000 00000 (fac.go:3) TEXT "".Fac(SB), $16-16
0x0000 00000 (fac.go:3) MOVQ (TLS), CX
0x0009 00009 (fac.go:3) CMPQ SP, 16(CX)
0x000d 00013 (fac.go:3) JLS 84
0x000f 00015 (fac.go:3) SUBQ $16, SP
0x0013 00019 (fac.go:3) FUNCDATA $0, gclocals·23e8278e2b69a3a75fa59b23c49ed6ad(SB)
0x0013 00019 (fac.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0013 00019 (fac.go:4) MOVQ "".n+24(FP), CX
0x0018 00024 (fac.go:4) CMPQ CX, $2
0x001c 00028 (fac.go:4) JLT $0, 70
0x001e 00030 (fac.go:7) LEAQ -1(CX), AX
0x0022 00034 (fac.go:7) MOVQ AX, (SP)
0x0026 00038 (fac.go:7) PCDATA $0, $0
0x0026 00038 (fac.go:7) CALL "".Fac(SB)
0x002b 00043 (fac.go:7) MOVQ 8(SP), CX Please have a look at load to DX below, then copy to AX. Later DX is no longer used. Instead we could load to AX in first place. Note: IMULQ needs AX operand. 0x0030 00048 (fac.go:7) MOVQ "".n+24(FP), DX
0x0035 00053 (fac.go:7) MOVQ DX, AX
0x0038 00056 (fac.go:7) IMULQ CX, AX
0x003c 00060 (fac.go:7) MOVQ AX, "".~r1+32(FP)
0x0041 00065 (fac.go:7) ADDQ $16, SP
0x0045 00069 (fac.go:7) RET
0x0046 00070 (fac.go:5) MOVQ $1, "".~r1+32(FP)
0x004f 00079 (fac.go:5) ADDQ $16, SP
0x0053 00083 (fac.go:5) RET
0x0054 00084 (fac.go:5) NOP
0x0054 00084 (fac.go:3) CALL runtime.morestack_noctxt(SB)
0x0059 00089 (fac.go:3) JMP 0 |
CL https://golang.org/cl/22160 mentions this issue. |
While investigating some missed optimizations I found that the register allocator inserts some useless copies. In two cases a function is compiled almost the same but one has a useless copy in it.
For example (*mspan).layout
BAD:
OK:
BAD has an extra "v31 = Copy v55 : AX". In BAD v31 and v55 are in the same block, but in OK v31 and v55 are in blocks b4 & b6 which haven't been merged in a previous pass.
The text was updated successfully, but these errors were encountered: