Skip to content

Commit 3d5cf72

Browse files
committed
cmd/compile: CSE copied tuple selectors
In CSE if a tuple generator is CSE'd to a different block, its selectors are copied to the same block. In this case, also CES the copied selectors. Test copied from Keith's CL 27202. Fixes golang#16741. Change-Id: I2fc8b9513d430f10d6104275cfff5fb75d3ef3d9 Reviewed-on: https://go-review.googlesource.com/27236 Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 2b8e143 commit 3d5cf72

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ func cse(f *Func) {
166166
// if we rewrite a tuple generator to a new one in a different block,
167167
// copy its selectors to the new generator's block, so tuple generator
168168
// and selectors stay together.
169+
// be careful not to copy same selectors more than once (issue 16741).
170+
copiedSelects := make(map[ID][]*Value)
169171
for _, b := range f.Blocks {
172+
out:
170173
for _, v := range b.Values {
171174
if rewrite[v.ID] != nil {
172175
continue
@@ -180,8 +183,16 @@ func cse(f *Func) {
180183
t := rewrite[v.Args[0].ID]
181184
if t != nil && t.Block != b {
182185
// v.Args[0] is tuple generator, CSE'd into a different block as t, v is left behind
186+
for _, c := range copiedSelects[t.ID] {
187+
if v.Op == c.Op {
188+
// an equivalent selector is already copied
189+
rewrite[v.ID] = c
190+
continue out
191+
}
192+
}
183193
c := v.copyInto(t.Block)
184194
rewrite[v.ID] = c
195+
copiedSelects[t.ID] = append(copiedSelects[t.ID], c)
185196
}
186197
}
187198
}

test/fixedbugs/issue16741.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile
2+
3+
// Copyright 2016 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+
// Make sure CSE of multi-output opcodes works correctly
8+
// with select0/1 operations.
9+
10+
package main
11+
12+
func div(d, r int64) int64 {
13+
if m := d % r; m > 0 {
14+
return d/r + 1
15+
}
16+
return d / r
17+
}

0 commit comments

Comments
 (0)