Skip to content

Commit 73269b8

Browse files
committed
cmd/compile: generate commutative rules when a condition is present
The commutative rule generator has an optimization where given (Add x y) with no other uses of x and y in the matching rule, it doesn't generate the commutative match (Add y x). However, if there is also a condition referring to x or y, such as (Add x y) && isFoo(x), then we should generate the commutative rule. This change parses the condition, extracts all idents, and takes them into consideration. This doesn't yield any new optimizations now. However, it is the right thing to do; otherwise we'll have to track it down and fix it again later. It is also expensive now, in terms of additional generated code. However, it will be much, much less expensive soon, once our generated code for commutative ops gets smaller. Change-Id: I52c2016c884bbc7789bf8dfe9b9c56061bc028ad Reviewed-on: https://go-review.googlesource.com/c/go/+/213702 Run-TryBot: Josh Bleecher Snyder <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 6817210 commit 73269b8

File tree

6 files changed

+4108
-552
lines changed

6 files changed

+4108
-552
lines changed

src/cmd/compile/internal/ssa/gen/rulegen.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ func expandOr(r string) []string {
13391339
// Potentially exponential, be careful.
13401340
func commute(r string, arch arch) []string {
13411341
match, cond, result := Rule{rule: r}.parse()
1342-
a := commute1(match, varCount(match), arch)
1342+
a := commute1(match, varCount(match, cond), arch)
13431343
for i, m := range a {
13441344
if cond != "" {
13451345
m += " && " + cond
@@ -1428,10 +1428,22 @@ func commute1(m string, cnt map[string]int, arch arch) []string {
14281428
}
14291429

14301430
// varCount returns a map which counts the number of occurrences of
1431-
// Value variables in m.
1432-
func varCount(m string) map[string]int {
1431+
// Value variables in the s-expression "match" and the Go expression "cond".
1432+
func varCount(match, cond string) map[string]int {
14331433
cnt := map[string]int{}
1434-
varCount1(m, cnt)
1434+
varCount1(match, cnt)
1435+
if cond != "" {
1436+
expr, err := parser.ParseExpr(cond)
1437+
if err != nil {
1438+
log.Fatalf("failed to parse cond %q: %v", cond, err)
1439+
}
1440+
ast.Inspect(expr, func(n ast.Node) bool {
1441+
if id, ok := n.(*ast.Ident); ok {
1442+
cnt[id.Name]++
1443+
}
1444+
return true
1445+
})
1446+
}
14351447
return cnt
14361448
}
14371449

0 commit comments

Comments
 (0)