Skip to content
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: shorten code to load a pointer to a global #20452

Closed
josharian opened this issue May 22, 2017 · 2 comments
Closed

cmd/compile: shorten code to load a pointer to a global #20452

josharian opened this issue May 22, 2017 · 2 comments

Comments

@josharian
Copy link
Contributor

package p

var s string = "abc"

func f() {
	g(&s)
}

//go:noinline
func g(s *string) {}

With NOPs and the like removed, f compiles to:

"".f STEXT size=62 args=0x0 locals=0x10
	0x0000 00000 (t.go:5)	TEXT	"".f(SB), $16-0
	0x0000 00000 (t.go:5)	MOVQ	(TLS), CX
	0x0009 00009 (t.go:5)	CMPQ	SP, 16(CX)
	0x000d 00013 (t.go:5)	JLS	55
	0x000f 00015 (t.go:5)	SUBQ	$16, SP
	0x0013 00019 (t.go:5)	MOVQ	BP, 8(SP)
	0x0018 00024 (t.go:5)	LEAQ	8(SP), BP
	0x001d 00029 (t.go:5)	LEAQ	"".s(SB), AX
	0x0024 00036 (t.go:6)	MOVQ	AX, (SP)
	0x0028 00040 (t.go:6)	CALL	"".g(SB)
	0x002d 00045 (t.go:7)	MOVQ	8(SP), BP
	0x0032 00050 (t.go:7)	ADDQ	$16, SP
	0x0036 00054 (t.go:7)	RET
	0x0037 00055 (t.go:5)	CALL	runtime.morestack_noctxt(SB)
	0x003c 00060 (t.go:5)	JMP	0
	rel 5+4 t=16 TLS+0
	rel 32+4 t=15 "".s+0
	rel 41+4 t=8 "".g+0
	rel 56+4 t=8 runtime.morestack_noctxt+0

The instructions at 29 and 36 look inefficient. "".s(SB) is a linker-resolved relocation, so seems like we should be able to do this in a single MOVQ instruction: MOVQ "".s(SB), (SP).

@randall77 is that correct?

@randall77
Copy link
Contributor

I don't think this would work. Such an instruction would need two modR/M fields, which isn't allowed.

foo:
	movq	100(%rip),(%rsp)
 gcc -c tmp1.s
tmp1.s:2:17: error: invalid operand for instruction
 movq 100(%rip),(%rsp)
                ^~~~~~

@josharian
Copy link
Contributor Author

Ahh. Thanks.

@golang golang locked and limited conversation to collaborators May 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants