From da4687923b8c2d42c23f61fa3db9f4d3ce0c5f54 Mon Sep 17 00:00:00 2001 From: Yi Yang Date: Sun, 19 Mar 2023 07:24:04 +0000 Subject: 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: 1aad82e62e61e89789932c2070851a023f130dd8 GitHub-Pull-Request: golang/go#59119 Reviewed-on: https://go-review.googlesource.com/c/go/+/477555 Reviewed-by: Keith Randall Reviewed-by: Heschi Kreinick Auto-Submit: Keith Randall Reviewed-by: Keith Randall TryBot-Result: Gopher Robot Run-TryBot: Keith Randall --- test/codegen/arithmetic.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test/codegen/arithmetic.go') diff --git a/test/codegen/arithmetic.go b/test/codegen/arithmetic.go index 2f06ac58d6..f381b34ade 100644 --- a/test/codegen/arithmetic.go +++ b/test/codegen/arithmetic.go @@ -92,6 +92,22 @@ func SubAddSimplify(a, b int) int { return r } +func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) { + // amd64:-"ADDQ" + r := (a + b) - (a + c) + // amd64:-"ADDQ" + r1 := (a + b) - (c + a) + // amd64:-"ADDQ" + r2 := (b + a) - (a + c) + // amd64:-"ADDQ" + r3 := (b + a) - (c + a) + // amd64:-"SUBQ" + r4 := (a - c) + (c + b) + // amd64:-"SUBQ" + r5 := (a - c) + (b + c) + return r, r1, r2, r3, r4, r5 +} + func SubAddNegSimplify(a, b int) int { // amd64:"NEGQ",-"ADDQ",-"SUBQ" // ppc64x:"NEG",-"ADD",-"SUB" -- cgit v1.3-5-g9baa