compiler: add an option to emit optimization diagnostics

Add a -fgo-debug-optimization option to emit optimization
diagnostics. This can be used for testing optimizations. Apply
this to the range clear optimizations of maps and arrays.

Corresponding change on the GCC backend:

Index: gcc/go/gccgo.texi
===================================================================
--- gcc/go/gccgo.texi	(revision 269825)
+++ gcc/go/gccgo.texi	(working copy)
@@ -246,6 +246,11 @@
 that match the given suffix @var{n}.  This can be used to binary
 search across functions to uncover escape analysis bugs.

+@item -fgo-debug-optimization
+@cindex @option{-fgo-debug-optimization}
+@cindex @option{-fno-go-debug-optimization}
+Output optimization diagnostics.
+
 @item -fgo-c-header=@var{file}
 @cindex @option{-fgo-c-header}
 Write top-level named Go struct definitions to @var{file} as C code.
Index: gcc/go/go-c.h
===================================================================
--- gcc/go/go-c.h	(revision 269825)
+++ gcc/go/go-c.h	(working copy)
@@ -49,6 +49,7 @@
   int debug_escape_level;
   const char* debug_escape_hash;
   int64_t nil_check_size_threshold;
+  bool debug_optimization;
 };

 extern void go_create_gogo (const struct go_create_gogo_args*);
Index: gcc/go/go-lang.c
===================================================================
--- gcc/go/go-lang.c	(revision 269825)
+++ gcc/go/go-lang.c	(working copy)
@@ -118,6 +118,7 @@
   args.debug_escape_level = go_debug_escape_level;
   args.debug_escape_hash = go_debug_escape_hash;
   args.nil_check_size_threshold = TARGET_AIX ? -1 : 4096;
+  args.debug_optimization = go_debug_optimization;
   args.linemap = go_get_linemap();
   args.backend = go_get_backend();
   go_create_gogo (&args);
Index: gcc/go/lang.opt
===================================================================
--- gcc/go/lang.opt	(revision 269825)
+++ gcc/go/lang.opt	(working copy)
@@ -85,6 +85,10 @@
 Go Joined RejectNegative Var(go_debug_escape_hash) Init(0)
 -fgo-debug-escape-hash=<string>	Hash value to debug escape analysis.

+fgo-debug-optimization
+Go Var(go_debug_optimization) Init(0)
+Emit optimization diagnostics.
+
 o
 Go Joined Separate
 ; Documented in common.opt

With this option, we can introduce tests for the range clear
optimizations in the GCC testsuite:

Index: gcc/testsuite/go.dg/arrayclear.go
===================================================================
--- gcc/testsuite/go.dg/arrayclear.go	(nonexistent)
+++ gcc/testsuite/go.dg/arrayclear.go	(working copy)
@@ -0,0 +1,20 @@
+// { dg-do compile }
+// { dg-options "-fgo-debug-optimization" }
+
+package p
+
+var a [10]int
+
+func arrayClear() {
+	for i := range a { // { dg-error "array range clear" }
+		a[i] = 0
+	}
+}
+
+var s []int
+
+func sliceClear() {
+	for i := range s { // { dg-error "array range clear" }
+		s[i] = 0
+	}
+}
Index: gcc/testsuite/go.dg/mapclear.go
===================================================================
--- gcc/testsuite/go.dg/mapclear.go	(nonexistent)
+++ gcc/testsuite/go.dg/mapclear.go	(working copy)
@@ -0,0 +1,10 @@
+// { dg-do compile }
+// { dg-options "-fgo-debug-optimization" }
+
+package p
+
+func clear(m map[int]int) {
+	for k := range m { // { dg-error "map range clear" }
+		delete(m, k)
+	}
+}

Change-Id: I0d4c1bcc330b50332831b43457bd04f43ae5ec02
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/170002
Reviewed-by: Ian Lance Taylor <iant@golang.org>
4 files changed