cmd/compile: eliminate stores to unread auto variables

This is a crude compiler pass to eliminate stores to auto variables
that are only ever written to.

Eliminates an unnecessary store to x from the following code:

func f() int {
	var x := 1
	return *(&x)
}

Fixes #19765.

Change-Id: If2c63a8ae67b8c590b6e0cc98a9610939a3eeffa
Reviewed-on: https://go-review.googlesource.com/38746
Run-TryBot: Michael Munday <mike.munday@ibm.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Michael Munday 2017-03-29 14:01:41 -04:00
parent 18b48afec9
commit 744ebfde04
8 changed files with 158 additions and 23 deletions

View file

@ -973,13 +973,23 @@ var linuxAMD64Tests = []*asmTest{
// make sure assembly output has matching offset and base register.
`
func f72(a, b int) int {
var x [16]byte // use some frame
_ = x
//go:noinline
func() {_, _ = a, b} () // use some frame
return b
}
`,
[]string{"b\\+40\\(SP\\)"},
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]0-8"},
},
}
var linux386Tests = []*asmTest{
@ -1015,6 +1025,16 @@ var linux386Tests = []*asmTest{
}`,
[]string{"\tADDL\t[$]19", "\tIMULL"}, // (n+19)*a
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]0-4"},
},
}
var linuxS390XTests = []*asmTest{
@ -1293,6 +1313,16 @@ var linuxS390XTests = []*asmTest{
`,
[]string{"\tFLOGR\t"},
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]0-8"},
},
}
var linuxARMTests = []*asmTest{
@ -1404,13 +1434,23 @@ var linuxARMTests = []*asmTest{
// make sure assembly output has matching offset and base register.
`
func f13(a, b int) int {
var x [16]byte // use some frame
_ = x
//go:noinline
func() {_, _ = a, b} () // use some frame
return b
}
`,
[]string{"b\\+4\\(FP\\)"},
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]-4-4"},
},
}
var linuxARM64Tests = []*asmTest{
@ -1584,6 +1624,16 @@ var linuxARM64Tests = []*asmTest{
`,
[]string{"\tMOVD\t\"\"\\.a\\+[0-9]+\\(FP\\), R[0-9]+", "\tMOVD\tR[0-9]+, \"\"\\.b\\+[0-9]+\\(FP\\)"},
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]-8-8"},
},
}
var linuxMIPSTests = []*asmTest{
@ -1667,6 +1717,16 @@ var linuxMIPSTests = []*asmTest{
`,
[]string{"\tCLZ\t"},
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]-4-4"},
},
}
var linuxPPC64LETests = []*asmTest{
@ -1751,6 +1811,16 @@ var linuxPPC64LETests = []*asmTest{
`,
[]string{"\tROTL\t"},
},
{
// check that stack store is optimized away
`
func $() int {
var x int
return *(&x)
}
`,
[]string{"TEXT\t.*, [$]0-8"},
},
}
// TestLineNumber checks to make sure the generated assembly has line numbers