Skip to content

Commit 6317f92

Browse files
committed
cmd/compile: fix CSE with commutative ops
CSE opportunities were being missed for commutative ops. We used to order the args of commutative ops (by arg ID) once at the start of CSE. But that may not be enough. i1 = (Load ptr mem) i2 = (Load ptr mem) x1 = (Add i1 j) x2 = (Add i2 j) Equivalent commutative ops x1 and x2 may not get their args ordered in the same way because because at the start of CSE, we don't know that the i values will be CSEd. If x1 and x2 get opposite orders we won't CSE them. Instead, (re)order the args of commutative operations by their equivalence class IDs each time we partition an equivalence class. Change-Id: Ic609fa83b85299782a5e85bf93dc6023fccf4b0c Reviewed-on: https://go-review.googlesource.com/33632 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Todd Neal <[email protected]>
1 parent 34b563f commit 6317f92

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ func cse(f *Func) {
3939
if v.Type.IsMemory() {
4040
continue // memory values can never cse
4141
}
42-
if opcodeTable[v.Op].commutative && len(v.Args) == 2 && v.Args[1].ID < v.Args[0].ID {
43-
// Order the arguments of binary commutative operations.
44-
v.Args[0], v.Args[1] = v.Args[1], v.Args[0]
45-
}
4642
a = append(a, v)
4743
}
4844
}
@@ -92,6 +88,15 @@ func cse(f *Func) {
9288
for i := 0; i < len(partition); i++ {
9389
e := partition[i]
9490

91+
if opcodeTable[e[0].Op].commutative {
92+
// Order the first two args before comparison.
93+
for _, v := range e {
94+
if valueEqClass[v.Args[0].ID] > valueEqClass[v.Args[1].ID] {
95+
v.Args[0], v.Args[1] = v.Args[1], v.Args[0]
96+
}
97+
}
98+
}
99+
95100
// Sort by eq class of arguments.
96101
byArgClass.a = e
97102
byArgClass.eqClass = valueEqClass
@@ -101,6 +106,7 @@ func cse(f *Func) {
101106
splitPoints = append(splitPoints[:0], 0)
102107
for j := 1; j < len(e); j++ {
103108
v, w := e[j-1], e[j]
109+
// Note: commutative args already correctly ordered by byArgClass.
104110
eqArgs := true
105111
for k, a := range v.Args {
106112
b := w.Args[k]

test/prove.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func f11c(a []int, i int) {
273273

274274
func f11d(a []int, i int) {
275275
useInt(a[2*i+7])
276-
useInt(a[2*i+7])
276+
useInt(a[2*i+7]) // ERROR "Proved boolean IsInBounds$"
277277
}
278278

279279
func f12(a []int, b int) {
@@ -438,6 +438,19 @@ func f13i(a uint) int {
438438
return 3
439439
}
440440

441+
func f14(p, q *int, a []int) {
442+
// This crazy ordering usually gives i1 the lowest value ID,
443+
// j the middle value ID, and i2 the highest value ID.
444+
// That used to confuse CSE because it ordered the args
445+
// of the two + ops below differently.
446+
// That in turn foiled bounds check elimination.
447+
i1 := *p
448+
j := *q
449+
i2 := *p
450+
useInt(a[i1+j])
451+
useInt(a[i2+j]) // ERROR "Proved boolean IsInBounds$"
452+
}
453+
441454
//go:noinline
442455
func useInt(a int) {
443456
}

0 commit comments

Comments
 (0)