Open
Description
Go version
go version devel go1.24-7fcd4a7 Mon Aug 19 23:36:23 2024 +0000 linux/amd64
What did you do?
I was reading https://www.dolthub.com/blog/2024-08-23-the-4-chan-go-programmer and there was a piece of code that caught my attention (keep in mind that it is a contrived example, but could reveal a bigger problem). So I decided to check the generated assembly (https://go.godbolt.org/z/oxqbPnPxn).
package test
func f() {
i := 1
setInt1(&i)
}
func setInt1(i *int) {
setInt2(&i)
}
func setInt2(i **int) {
setInt3(&i)
}
func setInt3(i ***int) {
setInt4(&i)
}
func setInt4(i ****int) {
****i = 100
}
What did you see happen?
In some of the functions there were redundant instructions (marked with ; REDUNDANT
).
setInt4
is compiled to:
MOVQ (AX), AX
MOVQ (AX), AX
MOVQ (AX), AX
MOVQ $100, (AX)
RET
setInt3
is compiled to:
MOVQ AX, command-line-arguments.i+8(SP) ; REDUNDANT
MOVQ (AX), AX
MOVQ (AX), AX
XCHGL AX, AX
MOVQ $100, (AX)
RET
setInt2
is compiled to:
MOVQ AX, command-line-arguments.i+8(SP) ; REDUNDANT
LEAQ command-line-arguments.i+8(SP), AX ; REDUNDANT
MOVQ (AX), AX ; REDUNDANT
MOVQ (AX), AX
XCHGL AX, AX
XCHGL AX, AX
MOVQ $100, (AX)
RET
setInt1
is compiled to:
(...stack handling...)
MOVQ AX, command-line-arguments.i+24(SP) ; REDUNDANT
LEAQ command-line-arguments.i+24(SP), AX ; REDUNDANT
MOVQ AX, command-line-arguments.i(SP) ; REDUNDANT
LEAQ command-line-arguments.i(SP), AX ; REDUNDANT
MOVQ (AX), AX ; REDUNDANT
MOVQ (AX), AX ; REDUNDANT
NOP
XCHGL AX, AX
XCHGL AX, AX
MOVQ $100, (AX)
(...stack handling...)
RET
What did you expect to see?
I expected to see no redundant instructions.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status