Skip to content

Commit 3b2f67a

Browse files
committed
cmd/compile: remove check that Zero's arg has the correct base type
It doesn't have to. The type in the aux field is authoritative. There are cases involving casting from interface{} where pointers have a placeholder pointer type (because the type is not known when the IData op is generated). The check was introduced in CL 13447. Fixes #39459 Change-Id: Id77a57577806a271aeebd20bea5d92d08ee7aa6b Reviewed-on: https://go-review.googlesource.com/c/go/+/239817 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 334752d commit 3b2f67a

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/cmd/compile/internal/ssa/deadstore.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ func dse(f *Func) {
7373
}
7474

7575
// Walk backwards looking for dead stores. Keep track of shadowed addresses.
76-
// An "address" is an SSA Value which encodes both the address and size of
77-
// the write. This code will not remove dead stores to the same address
78-
// of different types.
76+
// A "shadowed address" is a pointer and a size describing a memory region that
77+
// is known to be written. We keep track of shadowed addresses in the shadowed
78+
// map, mapping the ID of the address to the size of the shadowed region.
79+
// Since we're walking backwards, writes to a shadowed region are useless,
80+
// as they will be immediately overwritten.
7981
shadowed.clear()
8082
v := last
8183

@@ -93,17 +95,13 @@ func dse(f *Func) {
9395
sz = v.AuxInt
9496
}
9597
if shadowedSize := int64(shadowed.get(v.Args[0].ID)); shadowedSize != -1 && shadowedSize >= sz {
96-
// Modify store into a copy
98+
// Modify the store/zero into a copy of the memory state,
99+
// effectively eliding the store operation.
97100
if v.Op == OpStore {
98101
// store addr value mem
99102
v.SetArgs1(v.Args[2])
100103
} else {
101104
// zero addr mem
102-
typesz := v.Args[0].Type.Elem().Size()
103-
if sz != typesz {
104-
f.Fatalf("mismatched zero/store sizes: %d and %d [%s]",
105-
sz, typesz, v.LongString())
106-
}
107105
v.SetArgs1(v.Args[1])
108106
}
109107
v.Aux = nil

test/fixedbugs/issue39459.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
type T struct { // big enough to be an unSSAable type
10+
a, b, c, d, e, f int
11+
}
12+
13+
func f(x interface{}, p *int) {
14+
_ = *p // trigger nil check here, removing it from below
15+
switch x := x.(type) {
16+
case *T:
17+
// Zero twice, so one of them will be removed by the deadstore pass
18+
*x = T{}
19+
*p = 0 // store op to prevent Zero ops from being optimized by the earlier opt pass rewrite rules
20+
*x = T{}
21+
}
22+
}

0 commit comments

Comments
 (0)