]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: CSE copied tuple selectors
authorCherry Zhang <cherryyz@google.com>
Wed, 17 Aug 2016 17:29:19 +0000 (13:29 -0400)
committerCherry Zhang <cherryyz@google.com>
Wed, 17 Aug 2016 21:03:26 +0000 (21:03 +0000)
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 #16741.

Change-Id: I2fc8b9513d430f10d6104275cfff5fb75d3ef3d9
Reviewed-on: https://go-review.googlesource.com/27236
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/ssa/cse.go
test/fixedbugs/issue16741.go [new file with mode: 0644]

index a000c577d19cb759a55a6529e036f19fe139afe7..c0ddc83681cf17f5fbc7f83c2bd2452fca0d3b00 100644 (file)
@@ -166,7 +166,10 @@ func cse(f *Func) {
        // if we rewrite a tuple generator to a new one in a different block,
        // copy its selectors to the new generator's block, so tuple generator
        // and selectors stay together.
+       // be careful not to copy same selectors more than once (issue 16741).
+       copiedSelects := make(map[ID][]*Value)
        for _, b := range f.Blocks {
+       out:
                for _, v := range b.Values {
                        if rewrite[v.ID] != nil {
                                continue
@@ -180,8 +183,16 @@ func cse(f *Func) {
                        t := rewrite[v.Args[0].ID]
                        if t != nil && t.Block != b {
                                // v.Args[0] is tuple generator, CSE'd into a different block as t, v is left behind
+                               for _, c := range copiedSelects[t.ID] {
+                                       if v.Op == c.Op {
+                                               // an equivalent selector is already copied
+                                               rewrite[v.ID] = c
+                                               continue out
+                                       }
+                               }
                                c := v.copyInto(t.Block)
                                rewrite[v.ID] = c
+                               copiedSelects[t.ID] = append(copiedSelects[t.ID], c)
                        }
                }
        }
diff --git a/test/fixedbugs/issue16741.go b/test/fixedbugs/issue16741.go
new file mode 100644 (file)
index 0000000..9946062
--- /dev/null
@@ -0,0 +1,17 @@
+// compile
+
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Make sure CSE of multi-output opcodes works correctly
+// with select0/1 operations.
+
+package main
+
+func div(d, r int64) int64 {
+       if m := d % r; m > 0 {
+               return d/r + 1
+       }
+       return d / r
+}