Skip to content

Commit da46879

Browse files
y1yang0gopherbot
authored andcommitted
cmd/compile: add rewrite rules for arithmetic operations
Add the following common local transformations (t + x) - (t + y) == x - y (t + x) - (y + t) == x - y (x + t) - (y + t) == x - y (x + t) - (t + y) == x - y (x - t) + (t + y) == x + y (x - t) + (y + t) == x + y The compiler itself matches such patterns many times. This also aligns with other popular compilers. Fixes #59111 Change-Id: Ibdfdb414782f8fcaa20b84ac5d43d0d9ae2c7b60 GitHub-Last-Rev: 1aad82e GitHub-Pull-Request: #59119 Reviewed-on: https://go-review.googlesource.com/c/go/+/477555 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Keith Randall <[email protected]>
1 parent b414ba4 commit da46879

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed

src/cmd/compile/internal/ssa/_gen/generic.rules

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,16 @@
573573
(Sub(64|32|16|8) (Com(64|32|16|8) x) (Neg(64|32|16|8) x)) => (Const(64|32|16|8) [-1])
574574
(Add(64|32|16|8) (Com(64|32|16|8) x) x) => (Const(64|32|16|8) [-1])
575575

576+
// Simplification when involving common integer
577+
// (t + x) - (t + y) == x - y
578+
// (t + x) - (y + t) == x - y
579+
// (x + t) - (y + t) == x - y
580+
// (x + t) - (t + y) == x - y
581+
// (x - t) + (t + y) == x + y
582+
// (x - t) + (y + t) == x + y
583+
(Sub(64|32|16|8) (Add(64|32|16|8) t x) (Add(64|32|16|8) t y)) => (Sub(64|32|16|8) x y)
584+
(Add(64|32|16|8) (Sub(64|32|16|8) x t) (Add(64|32|16|8) t y)) => (Add(64|32|16|8) x y)
585+
576586
// ^(x-1) == ^x+1 == -x
577587
(Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) => (Neg(64|32|16|8) x)
578588
(Com(64|32|16|8) (Add(64|32|16|8) (Const(64|32|16|8) [-1]) x)) => (Neg(64|32|16|8) x)

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

Lines changed: 228 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codegen/arithmetic.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ func SubAddSimplify(a, b int) int {
9292
return r
9393
}
9494

95+
func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
96+
// amd64:-"ADDQ"
97+
r := (a + b) - (a + c)
98+
// amd64:-"ADDQ"
99+
r1 := (a + b) - (c + a)
100+
// amd64:-"ADDQ"
101+
r2 := (b + a) - (a + c)
102+
// amd64:-"ADDQ"
103+
r3 := (b + a) - (c + a)
104+
// amd64:-"SUBQ"
105+
r4 := (a - c) + (c + b)
106+
// amd64:-"SUBQ"
107+
r5 := (a - c) + (b + c)
108+
return r, r1, r2, r3, r4, r5
109+
}
110+
95111
func SubAddNegSimplify(a, b int) int {
96112
// amd64:"NEGQ",-"ADDQ",-"SUBQ"
97113
// ppc64x:"NEG",-"ADD",-"SUB"

0 commit comments

Comments
 (0)