From a2589c9dc035ca8bb7f3a48d4a55a840c96ecef6 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 22 Apr 2016 15:37:12 -0400
Subject: [PATCH 111/117] FIXME: get ira working (requires adding cgraph node)

---
 gcc/rtl.c                    |  2 ++
 gcc/rtl.h                    |  1 +
 gcc/rtl/notes.rst            |  3 ++
 gcc/rtl/rtl-frontend.c       | 22 ++++--------
 gcc/testsuite/rtl.dg/ira.rtl | 84 ++++++++++++++++++++++++++++++++++++++++++++
 gcc/toplev.c                 |  7 ++++
 6 files changed, 104 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/rtl.dg/ira.rtl

diff --git a/gcc/rtl.c b/gcc/rtl.c
index a445cdc..9729c82 100644
--- a/gcc/rtl.c
+++ b/gcc/rtl.c
@@ -878,3 +878,5 @@ rtl_check_failed_flag (const char *name, const_rtx r, const char *file,
      name, GET_RTX_NAME (GET_CODE (r)), func, trim_filename (file), line);
 }
 #endif /* ENABLE_RTL_FLAG_CHECKING */
+
+bool in_rtl_frontend_p = false;
diff --git a/gcc/rtl.h b/gcc/rtl.h
index d60d043..bd5419f 100644
--- a/gcc/rtl.h
+++ b/gcc/rtl.h
@@ -3744,5 +3744,6 @@ struct GTY(()) cgraph_rtl_info {
   unsigned function_used_regs_valid: 1;
 };
 
+extern bool in_rtl_frontend_p;
 
 #endif /* ! GCC_RTL_H */
diff --git a/gcc/rtl/notes.rst b/gcc/rtl/notes.rst
index 5cfceed..f91ad0d 100644
--- a/gcc/rtl/notes.rst
+++ b/gcc/rtl/notes.rst
@@ -83,6 +83,9 @@ Limitations
   a ``FUNCTION_DECL`` tree when building a function object (by calling
   ``allocate_struct_function``).
 
+* There's no support for running more than one pass; fixing this would
+  require being able to run passes from a certain point onwards.
+
 * Roundtripping of recognized instructions may be an issue (i.e. those
   with ``INSN_CODE`` != -1), such as the ``667 {jump}`` in the following::
 
diff --git a/gcc/rtl/rtl-frontend.c b/gcc/rtl/rtl-frontend.c
index a6a7d05..44227df 100644
--- a/gcc/rtl/rtl-frontend.c
+++ b/gcc/rtl/rtl-frontend.c
@@ -98,6 +98,8 @@ struct GTY(()) language_function
 static bool
 rtl_langhook_init (void)
 {
+  in_rtl_frontend_p = true;
+
   build_common_tree_nodes (false);
 
   /* I don't know why this has to be done explicitly.  */
@@ -640,24 +642,12 @@ function_reader::create_function ()
       BLOCK_VARS (m_inner_block) = BIND_EXPR_VARS (m_inner_bind_expr);
 
       //debug_tree (m_inner_fndecl);
-
-      /* Convert to gimple: */
-      //printf("about to gimplify_function_tree\n");
-      gimplify_function_tree (m_inner_fndecl);
-      //printf("finished gimplify_function_tree\n");
 #endif
 
-#if 0
-      current_function_decl = fndecl;
-
-     /* Add to cgraph: */
-      cgraph_node::finalize_function (fndecl, false);
-      /* This can trigger a collection, so we need to have the function
-	 visible via a GC root.  current_function_decl is a GC-root,
-	 so we should be OK.  */
+  /* Add to cgraph.  */
+  cgraph_node::add_new_function (fndecl, true /*lowered*/);
 
-      current_function_decl = NULL;
-#endif
+  current_function_decl = NULL;
 }
 
 void
@@ -915,6 +905,8 @@ run_one_pass_by_name (const char *pass_name)
 static void
 rtl_langhook_parse_file (void)
 {
+  in_rtl_frontend_p = true;
+
   initialize_rtl ();
   crtl->emit.init_emit ();
 
diff --git a/gcc/testsuite/rtl.dg/ira.rtl b/gcc/testsuite/rtl.dg/ira.rtl
new file mode 100644
index 0000000..6bd3fa6
--- /dev/null
+++ b/gcc/testsuite/rtl.dg/ira.rtl
@@ -0,0 +1,84 @@
+/* { dg-options "-fsingle-pass=rtl-ira -fdump-rtl-ira" } */
+
+/* Lightly-modified dump of test-loop.c.251r.asmcons.  */
+
+;; Function test_1 (test_1, funcdef_no=0, decl_uid=1758, cgraph_uid=0, symbol_order=0)
+
+(note 1 0 6 (nil) NOTE_INSN_DELETED)
+(note 6 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
+(insn 2 6 3 2 (set (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                (const_int -4 [0xfffffffffffffffc])) [1 i+0 S4 A32])
+        (reg:SI 5 di [ i ])) ../../src/test-loop.c:2 -1
+     (nil))
+(insn 3 2 4 2 (set (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                (const_int -8 [0xfffffffffffffff8])) [1 j+0 S4 A32])
+        (reg:SI 4 si [ j ])) ../../src/test-loop.c:2 -1
+     (nil))
+(insn 4 3 5 2 (set (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                (const_int -12 [0xfffffffffffffff4])) [1 k+0 S4 A32])
+        (reg:SI 1 dx [ k ])) ../../src/test-loop.c:2 -1
+     (nil))
+(note 5 4 8 2 NOTE_INSN_FUNCTION_BEG)
+(insn 8 5 9 2 (set (reg:SI 89)
+        (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                (const_int -4 [0xfffffffffffffffc])) [1 i+0 S4 A32])) ../../src/test-loop.c:3 -1
+     (nil))
+(insn 9 8 10 2 (set (reg:CCGC 17 flags)
+        (compare:CCGC (reg:SI 89)
+            (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                    (const_int -8 [0xfffffffffffffff8])) [1 j+0 S4 A32]))) ../../src/test-loop.c:3 -1
+     (nil))
+(jump_insn 10 9 11 2 (set (pc)
+        (if_then_else (ge (reg:CCGC 17 flags)
+                (const_int 0 [0]))
+            (label_ref 16)
+            (pc))) ../../src/test-loop.c:3 -1
+     (nil)
+ -> 16)
+(note 11 10 12 3 [bb 3] NOTE_INSN_BASIC_BLOCK)
+(insn 12 11 13 3 (set (reg:SI 90)
+        (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                (const_int -12 [0xfffffffffffffff4])) [1 k+0 S4 A32])) ../../src/test-loop.c:4 -1
+     (nil))
+(insn 13 12 29 3 (parallel [
+            (set (reg:SI 87 [ _1 ])
+                (plus:SI (reg:SI 90)
+                    (const_int 4 [0x4])))
+            (clobber (reg:CC 17 flags))
+        ]) ../../src/test-loop.c:4 -1
+     (expr_list:REG_EQUAL (plus:SI (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                    (const_int -12 [0xfffffffffffffff4])) [1 k+0 S4 A32])
+            (const_int 4 [0x4]))
+        (nil)))
+(jump_insn 29 13 30 3 (set (pc)
+        (label_ref 20)) ../../src/test-loop.c:4 -1
+     (nil)
+ -> 20)
+(barrier 30 29 16)
+(code_label 16 30 17 4 2 (nil) [1 uses])
+(note 17 16 18 4 [bb 4] NOTE_INSN_BASIC_BLOCK)
+(insn 18 17 19 4 (set (reg:SI 91)
+        (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                (const_int -12 [0xfffffffffffffff4])) [1 k+0 S4 A32])) ../../src/test-loop.c:6 -1
+     (nil))
+(insn 19 18 20 4 (parallel [
+            (set (reg:SI 87 [ _1 ])
+                (neg:SI (reg:SI 91)))
+            (clobber (reg:CC 17 flags))
+        ]) ../../src/test-loop.c:6 -1
+     (expr_list:REG_EQUAL (neg:SI (mem/c:SI (plus:DI (reg/f:DI 20 frame)
+                    (const_int -12 [0xfffffffffffffff4])) [1 k+0 S4 A32]))
+        (nil)))
+(code_label 20 19 21 5 3 (nil) [1 uses])
+(note 21 20 22 5 [bb 5] NOTE_INSN_BASIC_BLOCK)
+(insn 22 21 26 5 (set (reg:SI 88 [ <retval> ])
+        (reg:SI 87 [ _1 ])) -1
+     (nil))
+(insn 26 22 27 5 (set (reg/i:SI 0 ax)
+        (reg:SI 88 [ <retval> ])) ../../src/test-loop.c:7 -1
+     (nil))
+(insn 27 26 0 5 (use (reg/i:SI 0 ax)) ../../src/test-loop.c:7 -1
+     (nil))
+
+/* Verify that IRA was run.  */
+/* { dg-final { scan-rtl-dump "Building IRA IR" "ira" } } */
diff --git a/gcc/toplev.c b/gcc/toplev.c
index c480bfc..2e2632b 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -475,6 +475,13 @@ compile_file (void)
 
   if (flag_syntax_only || flag_wpa)
     return;
+
+  /* The RTL frontend is currently only capable of running one pass; this
+     is done from within the parse_file langhook.
+     Hence we must return early here.  Fixing this would require
+     being able to run passes from a certain point onwards.  */
+  if (in_rtl_frontend_p)
+    return;
  
   /* Reset maximum_field_alignment, it can be adjusted by #pragma pack
      and this shouldn't influence any types built by the middle-end
-- 
1.8.5.3

