From 5f541b11aaa345b4cf0fb37a80c32b704a6854ea Mon Sep 17 00:00:00 2001
From: Alberto Donizetti <alb.donizetti@gmail.com>
Date: Fri, 9 Mar 2018 14:51:30 +0100
Subject: [PATCH] test/codegen: port MULs merging tests to codegen

And delete them from asm_go.

Change-Id: I0057cbd90ca55fa51c596e32406e190f3866f93e
Reviewed-on: https://go-review.googlesource.com/99815
Reviewed-by: Keith Randall <khr@golang.org>
---
 src/cmd/compile/internal/gc/asm_test.go | 66 -------------------------
 test/codegen/arithmetic.go              | 34 +++++++++++++
 2 files changed, 34 insertions(+), 66 deletions(-)

diff --git a/src/cmd/compile/internal/gc/asm_test.go b/src/cmd/compile/internal/gc/asm_test.go
index 96d0bc0d084..bed9ba9a41b 100644
--- a/src/cmd/compile/internal/gc/asm_test.go
+++ b/src/cmd/compile/internal/gc/asm_test.go
@@ -499,43 +499,6 @@ var linuxAMD64Tests = []*asmTest{
 		`,
 		pos: []string{"\tBTQ\t\\$60"},
 	},
-	// multiplication merging tests
-	{
-		fn: `
-		func mul1(n int) int {
-			return 15*n + 31*n
-		}`,
-		pos: []string{"\tIMULQ\t[$]46"}, // 46*n
-	},
-	{
-		fn: `
-		func mul2(n int) int {
-			return 5*n + 7*(n+1) + 11*(n+2)
-		}`,
-		pos: []string{"\tIMULQ\t[$]23", "\tADDQ\t[$]29"}, // 23*n + 29
-	},
-	{
-		fn: `
-		func mul3(a, n int) int {
-			return a*n + 19*n
-		}`,
-		pos: []string{"\tADDQ\t[$]19", "\tIMULQ"}, // (a+19)*n
-	},
-	{
-		fn: `
-		func mul4(n int) int {
-			return 23*n - 9*n
-		}`,
-		pos: []string{"\tIMULQ\t[$]14"}, // 14*n
-	},
-	{
-		fn: `
-		func mul5(a, n int) int {
-			return a*n - 19*n
-		}`,
-		pos: []string{"\tADDQ\t[$]-19", "\tIMULQ"}, // (a-19)*n
-	},
-
 	// see issue 19595.
 	// We want to merge load+op in f58, but not in f59.
 	{
@@ -906,21 +869,6 @@ var linuxAMD64Tests = []*asmTest{
 }
 
 var linux386Tests = []*asmTest{
-	// multiplication merging tests
-	{
-		fn: `
-		func $(n int) int {
-			return 9*n + 14*n
-		}`,
-		pos: []string{"\tIMULL\t[$]23"}, // 23*n
-	},
-	{
-		fn: `
-		func $(a, n int) int {
-			return 19*a + a*n
-		}`,
-		pos: []string{"\tADDL\t[$]19", "\tIMULL"}, // (n+19)*a
-	},
 	{
 		// check that stack store is optimized away
 		fn: `
@@ -931,20 +879,6 @@ var linux386Tests = []*asmTest{
 		`,
 		pos: []string{"TEXT\t.*, [$]0-4"},
 	},
-	{
-		fn: `
-		func mul3(n int) int {
-			return 23*n - 9*n
-		}`,
-		pos: []string{"\tIMULL\t[$]14"}, // 14*n
-	},
-	{
-		fn: `
-		func mul4(a, n int) int {
-			return n*a - a*19
-		}`,
-		pos: []string{"\tADDL\t[$]-19", "\tIMULL"}, // (n-19)*a
-	},
 	// Check that len() and cap() div by a constant power of two
 	// are compiled into SHRL.
 	{
diff --git a/test/codegen/arithmetic.go b/test/codegen/arithmetic.go
index c09fad60c81..eecb1013957 100644
--- a/test/codegen/arithmetic.go
+++ b/test/codegen/arithmetic.go
@@ -24,3 +24,37 @@ func Pow2Muls(n1, n2 int) (int, int) {
 
 	return a, b
 }
+
+// ------------------ //
+//    MULs merging    //
+// ------------------ //
+
+func MergeMuls1(n int) int {
+	// amd64:"IMULQ\t[$]46"
+	// 386:"IMULL\t[$]46"
+	return 15*n + 31*n // 46n
+}
+
+func MergeMuls2(n int) int {
+	// amd64:"IMULQ\t[$]23","ADDQ\t[$]29"
+	// 386:"IMULL\t[$]23","ADDL\t[$]29"
+	return 5*n + 7*(n+1) + 11*(n+2) // 23n + 29
+}
+
+func MergeMuls3(a, n int) int {
+	// amd64:"ADDQ\t[$]19",-"IMULQ\t[$]19"
+	// 386:"ADDL\t[$]19",-"IMULL\t[$]19"
+	return a*n + 19*n // (a+19)n
+}
+
+func MergeMuls4(n int) int {
+	// amd64:"IMULQ\t[$]14"
+	// 386:"IMULL\t[$]14"
+	return 23*n - 9*n // 14n
+}
+
+func MergeMuls5(a, n int) int {
+	// amd64:"ADDQ\t[$]-19",-"IMULQ\t[$]19"
+	// 386:"ADDL\t[$]-19",-"IMULL\t[$]19"
+	return a*n - 19*n // (a-19)n
+}
-- 
GitLab