From 3491b1b8c92221512f21a186cd353a186e7b5e64 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 14 Jul 2016 15:56:54 -0400
Subject: [PATCH 32/36] FIXME: eliminate g_regno_remapper in favor of a policy
 class

---
 gcc/combine.c           |  1 +
 gcc/gensupport.c        |  2 +-
 gcc/read-md.c           |  5 +++--
 gcc/read-md.h           | 33 +++++++++++++++++++++++++++++++--
 gcc/read-rtl-function.c | 15 +++++++++------
 gcc/read-rtl-function.h |  5 ++++-
 gcc/read-rtl.c          | 30 +++++++++++++++++++++++++-----
 gcc/rtl.h               | 23 -----------------------
 gcc/rtl/rtl-frontend.c  |  2 +-
 gcc/selftest-rtl.c      |  8 ++++----
 gcc/selftest-rtl.h      | 45 +++++++++++++++++++++++++++++++++++++++++++++
 gcc/selftest.h          | 12 ------------
 12 files changed, 124 insertions(+), 57 deletions(-)
 create mode 100644 gcc/selftest-rtl.h

diff --git a/gcc/combine.c b/gcc/combine.c
index c092ba1..3982a03 100644
--- a/gcc/combine.c
+++ b/gcc/combine.c
@@ -103,6 +103,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "rtl-iter.h"
 #include "print-rtl.h"
 #include "selftest.h"
+#include "selftest-rtl.h"
 #include "context.h" // for g
 #include "pass_manager.h"
 
diff --git a/gcc/gensupport.c b/gcc/gensupport.c
index 9fdf0ef..889e484 100644
--- a/gcc/gensupport.c
+++ b/gcc/gensupport.c
@@ -2233,7 +2233,7 @@ process_define_subst (void)
 class gen_reader : public rtx_reader
 {
  public:
-  gen_reader (line_maps *line_table) : rtx_reader (line_table) {}
+  gen_reader (line_maps *line_table) : rtx_reader (line_table, NULL) {}
   void handle_unknown_directive (file_location, const char *);
 };
 
diff --git a/gcc/read-md.c b/gcc/read-md.c
index d2df82c..c502d58 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -991,7 +991,7 @@ static line_maps *rtx_reader_line_table;
 
 /* Constructor for rtx_reader.  */
 
-rtx_reader::rtx_reader (line_maps *line_table)
+rtx_reader::rtx_reader (line_maps *line_table, rtl_reader_policy *policy)
 : m_line_table (line_table),
   m_current_linemap (NULL),
   m_toplevel_fname (NULL),
@@ -1001,7 +1001,8 @@ rtx_reader::rtx_reader (line_maps *line_table)
   m_first_dir_md_include (NULL),
   m_last_dir_md_include_ptr (&m_first_dir_md_include),
   m_first_line (0),
-  m_last_line (0)
+  m_last_line (0),
+  m_policy (policy)
 {
   rtx_reader_line_table = m_line_table;
 
diff --git a/gcc/read-md.h b/gcc/read-md.h
index 03d984e..0ae8f45 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -82,10 +82,35 @@ struct enum_type {
   unsigned int num_values;
 };
 
+/* FIXME.  */
+class regno_remapper
+{
+ public:
+  regno_remapper (int dumped_first_pseudo_regno)
+    : m_dumped_first_pseudo_regno (dumped_first_pseudo_regno) {}
+
+  int get_effective_regno (int dumped_regno);
+
+ private:
+  int m_dumped_first_pseudo_regno;
+};
+
+/* FIXME.  */
+
+struct rtl_reader_policy
+{
+  rtl_reader_policy (regno_remapper *regno_remapper_)
+  : m_regno_remapper (regno_remapper_)
+  {
+  }
+
+  regno_remapper *m_regno_remapper;
+};
+
 class rtx_reader
 {
  public:
-  rtx_reader (line_maps *line_table);
+  rtx_reader (line_maps *line_table, rtl_reader_policy *policy);
   virtual ~rtx_reader ();
 
   bool read_md_files (int, const char **, bool (*) (const char *));
@@ -155,6 +180,8 @@ class rtx_reader
   const char *get_filename () const { return m_read_md_filename; }
   int get_lineno () const { return m_read_md_lineno; }
 
+  int remap_regno (int dumped_regno) const;
+
  private:
   /* A singly-linked list of filenames.  */
   struct file_name_list {
@@ -199,6 +226,8 @@ class rtx_reader
 
   int m_first_line;
   int m_last_line;
+
+  rtl_reader_policy *m_policy;
 };
 
 /* Global singleton.  */
@@ -209,7 +238,7 @@ extern rtx_reader *rtx_reader_ptr;
 class noop_reader : public rtx_reader
 {
  public:
-  noop_reader (line_maps *line_table) : rtx_reader (line_table) {}
+  noop_reader (line_maps *line_table) : rtx_reader (line_table, NULL) {}
 
   /* A dummy implementation which skips unknown directives.  */
   void handle_unknown_directive (file_location, const char *);
diff --git a/gcc/read-rtl-function.c b/gcc/read-rtl-function.c
index a2ff96a..0b778f8 100644
--- a/gcc/read-rtl-function.c
+++ b/gcc/read-rtl-function.c
@@ -57,6 +57,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "insn-addr.h"
 #include "read-rtl-function.h"
 #include "selftest.h"
+#include "selftest-rtl.h"
 
 class function_reader;
 
@@ -204,7 +205,7 @@ class fixup_expr : public fixup
 class function_reader : public rtx_reader
 {
  public:
-  function_reader (line_maps *set);
+  function_reader (line_maps *set, rtl_reader_policy *policy);
   ~function_reader ();
 
   void handle_unknown_directive (file_location, const char *);
@@ -406,8 +407,9 @@ fixup_expr::apply (function_reader *reader) const
 
 /* function_reader's constructor.  */
 
-function_reader::function_reader (line_maps *set) : rtx_reader (set),
-    m_first_insn (NULL)
+function_reader::function_reader (line_maps *set, rtl_reader_policy *policy)
+: rtx_reader (set, policy),
+  m_first_insn (NULL)
 {
   bitmap_initialize (&m_bb_indices, NULL);
   bitmap_set_bit (&m_bb_indices, ENTRY_BLOCK);
@@ -836,7 +838,8 @@ postprocess (function_reader *reader)
 
 bool
 read_rtl_function_body (int argc, const char **argv,
-			bool (*parse_opt) (const char *))
+			bool (*parse_opt) (const char *),
+			rtl_reader_policy *policy)
 {
 #if 1
   in_rtl_frontend_p = true;
@@ -846,7 +849,7 @@ read_rtl_function_body (int argc, const char **argv,
   init_varasm_status ();
 #endif
 
-  function_reader reader (line_table);
+  function_reader reader (line_table, policy);
   if (!reader.read_md_files (argc, argv, parse_opt))
     return false;
 
@@ -879,7 +882,7 @@ read_rtl_function_body_from_file_range (location_t start_loc,
   crtl->emit.init_emit ();
   init_varasm_status ();
 
-  function_reader reader (line_table);
+  function_reader reader (line_table, NULL);
   if (!reader.read_file_fragment (exploc_start.file, exploc_start.line,
 				  exploc_end.line - 1))
     return false;
diff --git a/gcc/read-rtl-function.h b/gcc/read-rtl-function.h
index 1d5d4cc..145184f 100644
--- a/gcc/read-rtl-function.h
+++ b/gcc/read-rtl-function.h
@@ -20,8 +20,11 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_READ_RTL_FUNCTION_H
 #define GCC_READ_RTL_FUNCTION_H
 
+struct rtl_reader_policy;
+
 extern bool read_rtl_function_body (int argc, const char **argv,
-				    bool (*parse_opt) (const char *));
+				    bool (*parse_opt) (const char *),
+				    rtl_reader_policy *policy);
 
 extern bool read_rtl_function_body_from_file_range (location_t start_loc,
 						    location_t end_loc);
diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index f7b94e5..2713b21 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.c
@@ -1272,7 +1272,30 @@ read_flags (rtx return_rtx)
     }
 }
 
-regno_remapper *g_regno_remapper;
+/* FIXME.  */
+
+int
+regno_remapper::get_effective_regno (int dumped_regno)
+{
+  gcc_assert (dumped_regno >= m_dumped_first_pseudo_regno);
+
+  /* Offset dumped_regno to the correct range for pseudo regs
+     for this target.  */
+  return (LAST_VIRTUAL_REGISTER + 1
+	  + (dumped_regno - m_dumped_first_pseudo_regno));
+}
+
+int
+rtx_reader::remap_regno (int dumped_regno) const
+{
+  if (!m_policy)
+    return dumped_regno;
+
+  if (!m_policy->m_regno_remapper)
+    return dumped_regno;
+
+  return m_policy->m_regno_remapper->get_effective_regno (dumped_regno);
+}
 
 /* Subroutine of read_rtx and read_nested_rtx.  CODE_NAME is the name of
    either an rtx code or a code iterator.  Parse the rest of the rtx and
@@ -1668,10 +1691,7 @@ read_rtx_code (const char *code_name)
 	validate_const_int (name.string);
 	{
 	  int dumped_regno = atoi (name.string);
-	  int effective_regno = dumped_regno;
-	  if (g_regno_remapper)
-	    effective_regno =
-	      g_regno_remapper->get_effective_regno (dumped_regno);
+	  int effective_regno = rtx_reader_ptr->remap_regno (dumped_regno);
 	  set_regno_raw (return_rtx, effective_regno, 1);
 	}
 	REG_ATTRS (return_rtx) = NULL;
diff --git a/gcc/rtl.h b/gcc/rtl.h
index 4e6cf76..9c9530b 100644
--- a/gcc/rtl.h
+++ b/gcc/rtl.h
@@ -3749,29 +3749,6 @@ struct GTY(()) cgraph_rtl_info {
 
 extern bool in_rtl_frontend_p;
 
-/* FIXME.  */
-class regno_remapper
-{
- public:
-  regno_remapper (int dumped_first_pseudo_regno)
-    : m_dumped_first_pseudo_regno (dumped_first_pseudo_regno) {}
-
-  int get_effective_regno (int dumped_regno)
-  {
-    gcc_assert (dumped_regno >= m_dumped_first_pseudo_regno);
-
-    /* Offset dumped_regno to the correct range for pseudo regs
-       for this target.  */
-    return (LAST_VIRTUAL_REGISTER + 1
-	    + (dumped_regno - m_dumped_first_pseudo_regno));
-  }
-
- private:
-  int m_dumped_first_pseudo_regno;
-};
-
-extern regno_remapper *g_regno_remapper;
-
 extern rtx_insn *get_insn_by_uid (int uid);
 
 #endif /* ! GCC_RTL_H */
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index e449628..efb1c8e 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -241,7 +241,7 @@ rtl_langhook_parse_file (void)
   argv.safe_push (progname);
   for (unsigned i = 0; i < num_in_fnames; i++)
     argv.safe_push (in_fnames[i]);
-  if (!read_rtl_function_body (argv.length (), argv.address (), NULL))
+  if (!read_rtl_function_body (argv.length (), argv.address (), NULL, NULL))
     return;
 
   /* If -froundtrip=FILENAME was provided, immediately dump the
diff --git a/gcc/selftest-rtl.c b/gcc/selftest-rtl.c
index 6499f77..4efac9a 100644
--- a/gcc/selftest-rtl.c
+++ b/gcc/selftest-rtl.c
@@ -25,7 +25,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "target.h"
 #include "rtl.h"
 #include "read-rtl-function.h"
+#include "read-md.h"
 #include "tree-core.h"
+#include "selftest-rtl.h"
 
 #if CHECKING_P
 
@@ -42,17 +44,15 @@ selftest::rtl_dump_test::rtl_dump_test (const char *dump_content,
      current value of LAST_VIRTUAL_REGISTER.  */
   regno_remapper r (dumped_first_pseudo_regno);
 
-  g_regno_remapper = &r;
+  rtl_reader_policy policy (&r);
 
   /* Parse the tempfile.  */
   auto_vec<const char *> argv (2);
   argv.safe_push ("foo"); // progname
   argv.safe_push (tmp.get_filename ());
   bool read_ok
-    = read_rtl_function_body (argv.length (), argv.address (), NULL);
+    = read_rtl_function_body (argv.length (), argv.address (), NULL, &policy);
   ASSERT_TRUE (read_ok);
-
-  g_regno_remapper = NULL;
 }
 
 /* FIXME.  */
diff --git a/gcc/selftest-rtl.h b/gcc/selftest-rtl.h
new file mode 100644
index 0000000..be6a635
--- /dev/null
+++ b/gcc/selftest-rtl.h
@@ -0,0 +1,45 @@
+/* A self-testing framework, for use by -fself-test.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_SELFTEST_RTL_H
+#define GCC_SELFTEST_RTL_H
+
+/* The selftest code should entirely disappear in a production
+   configuration, hence we guard all of it with #if CHECKING_P.  */
+
+#if CHECKING_P
+
+namespace selftest {
+
+/* FIXME.  */
+
+class rtl_dump_test
+{
+ public:
+   rtl_dump_test (const char *dump_content, int dumped_first_pseudo_regno);
+   ~rtl_dump_test ();
+};
+
+extern void verify_three_block_rtl_cfg (function *fun);
+
+} /* end of namespace selftest.  */
+
+#endif /* #if CHECKING_P */
+
+#endif /* GCC_SELFTEST_RTL_H */
diff --git a/gcc/selftest.h b/gcc/selftest.h
index 7bccff5..f9af957 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -85,18 +85,6 @@ class temp_source_file
   char *m_filename;
 };
 
-
-/* FIXME.  */
-
-class rtl_dump_test
-{
- public:
-   rtl_dump_test (const char *dump_content, int dumped_first_pseudo_regno);
-   ~rtl_dump_test ();
-};
-
-extern void verify_three_block_rtl_cfg (function *fun);
-
 /* Declarations for specific families of tests (by source file), in
    alphabetical order.  */
 extern void bitmap_c_tests ();
-- 
1.8.5.3

