From 834734993e8b6bda40207fc1e42c5915d0646875 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 13 Apr 2017 21:05:57 -0400
Subject: [PATCH 09/10] FIXME: various cleanups

---
 gcc/heap-optimizations.c | 158 +++++++++++++++++++++--------------------------
 1 file changed, 72 insertions(+), 86 deletions(-)

diff --git a/gcc/heap-optimizations.c b/gcc/heap-optimizations.c
index 6025a54..be1f721 100644
--- a/gcc/heap-optimizations.c
+++ b/gcc/heap-optimizations.c
@@ -63,6 +63,17 @@ struct alloc_pair
   void maybe_convert_to_alloca ();
 };
 
+/* FIXME.  */
+
+static void
+remark (location_t loc, const char *msg)
+{
+  if (1)
+    warning_at (loc, 0, "%s", msg);
+}
+
+/* FIXME.  */
+
 void
 alloc_pair::maybe_convert_to_alloca ()
 {
@@ -71,16 +82,12 @@ alloc_pair::maybe_convert_to_alloca ()
   // FIXME: must not be within a loop
   // FIXME: no realloc calls
 
-#if 1
   /* Convert the acquire stmt into an alloca call.  */
+  remark (m_acquire->location, "converting to alloca");
   tree alloca_decl = builtin_decl_explicit (BUILT_IN_ALLOCA);
   // FIXME: should this be builtin_decl_implicit?
   gimple_call_set_fndecl (m_acquire, alloca_decl);
-#endif
 
-#if 0
-  warning_at ("converting to alloca");
-#endif
   /* FIXME: */
   // do we want to capture inlining information in locations?
   // maybe only when rich dumping is turned on?
@@ -89,7 +96,8 @@ alloc_pair::maybe_convert_to_alloca ()
 
   /* Delete the release stmt.  */
 #if 1
-  gimple_stmt_iterator gsi = gsi_for_stmt (m_release);  
+  remark (m_release->location, "removing explicit deallocator");
+  gimple_stmt_iterator gsi = gsi_for_stmt (m_release);
   /* bool require_eh_edge_purge = */
   gsi_remove (&gsi, false /* true*/);
   unlink_stmt_vdef (m_release);
@@ -164,6 +172,9 @@ class allocation_group
 class function_state
 {
  public:
+  void handle_deallocation (gcall *release_call,
+			    enum allocator_kind release_kind);
+
   void add_alloc_pair (gcall *alloc, gcall *release, enum allocator_kind kind);
 
   void combine_calls ();
@@ -192,6 +203,8 @@ const pass_data pass_data_heap_optimizations =
   0, /* todo_flags_finish */
 };
 
+/* FIXME.  */
+
 class pass_heap_optimizations : public gimple_opt_pass
 {
  public:
@@ -200,7 +213,7 @@ class pass_heap_optimizations : public gimple_opt_pass
   {}
 
   /* opt_pass methods: */
-  bool gate (function *fun) OVERRIDE FINAL
+  bool gate (function *) OVERRIDE FINAL
   {
     return flag_heap_optimizations;
   }
@@ -208,18 +221,17 @@ class pass_heap_optimizations : public gimple_opt_pass
   unsigned int execute (function *) OVERRIDE FINAL;
 
  private:
-  //void handle_malloc (gcall *call);
-  void handle_free (function_state &state, gcall *call);
-  void handle_operator_delete (function_state &state, gcall *call);
-  void handle_operator_vec_delete (function_state &state, gcall *call);
   void maybe_optimize_alloca (gcall *call);
 }; // class pass_heap_optimizations
 
+/* FIXME.  */
+
 void
 allocation_group::combine_calls ()
 {
-  warning_at (0, 0, "alloc BB: %i free BB: %i",
-	      m_acquire_bb->index, m_release_bb->index);
+  if (0)
+    warning_at (0, 0, "alloc BB: %i free BB: %i",
+		m_acquire_bb->index, m_release_bb->index);
 
   /* m_alloc_pairs is in order of the frees, but we need to process
      the allocs in the order they occur in.  */
@@ -227,11 +239,13 @@ allocation_group::combine_calls ()
 
   alloc_pair *pair;
   unsigned i;
+#if 0
   FOR_EACH_VEC_ELT (m_alloc_pairs, i, pair)
     {
       warning_at (pair->m_acquire->location, 0, "combining alloc here");
       warning_at (pair->m_release->location, 0, "combining release here");
     }
+#endif
 
   gimple_seq seq = NULL;
 
@@ -318,6 +332,7 @@ allocation_group::sort_by_alloc_order ()
   m_alloc_pairs.qsort (sort_by_alloc_uid);
 }
 
+/* FIXME.  */
 
 void
 function_state::add_alloc_pair (gcall *alloc, gcall *release,
@@ -326,15 +341,18 @@ function_state::add_alloc_pair (gcall *alloc, gcall *release,
   gcc_assert (alloc);
   gcc_assert (release);
 
-#if 0
-  warning_at (alloc->location, 0, "alloc pair: alloc here...");
-  warning_at (release->location, 0, "alloc pair: ...release here");
-#endif
+  if (0)
+    {
+      remark (alloc->location, "alloc pair: alloc here...");
+      remark (release->location, "alloc pair: ...release here");
+    }
 
   allocation_group &group = get_allocation_group (alloc->bb, release->bb);
   group.add_alloc_pair (alloc, release, kind);
 }
 
+/* FIXME.  */
+
 void
 function_state::combine_calls ()
 {
@@ -344,6 +362,8 @@ function_state::combine_calls ()
     group->combine_calls ();
 }
 
+/* FIXME.  */
+
 allocation_group &
 function_state::get_allocation_group (basic_block bb_alloc,
 				      basic_block bb_release)
@@ -357,18 +377,20 @@ function_state::get_allocation_group (basic_block bb_alloc,
     if (group->get_acquire_bb () == bb_alloc
 	&& group->get_release_bb () == bb_release)
       return *group;
-  
+
   /* Not found?  Add one.  */
   m_allocation_groups.safe_push (allocation_group (bb_alloc, bb_release));
   return m_allocation_groups[m_allocation_groups.length () - 1];
 }
 
+/* FIXME.  */
 
 unsigned int
 pass_heap_optimizations::execute (function *fun)
 {
-  warning_at (fun->function_start_locus, 0,
-	      "pass_heap_optimizations::execute: %qD", fun->decl);
+  if (0)
+    warning_at (fun->function_start_locus, 0,
+		"pass_heap_optimizations::execute: %qD", fun->decl);
 
   // FIXME: maybe examine callgraph for calls to malloc and free?
   // does this give us the BB?
@@ -398,10 +420,10 @@ pass_heap_optimizations::execute (function *fun)
 		{
 		  if (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
 				   "operator delete"))
-		    handle_operator_delete (state, call);
+		    state.handle_deallocation (call, ALLOCATOR_KIND_NEW);
 		  if (0 == strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
 				   "operator delete []"))
-		    handle_operator_vec_delete (state, call);
+		    state.handle_deallocation (call, ALLOCATOR_KIND_VEC_NEW);
 		}
 
 	      //debug_tree (gimple_call_arg (stmt, 0));
@@ -424,16 +446,10 @@ pass_heap_optimizations::execute (function *fun)
   (operator_name_info[(int) (CODE)].identifier)
 	      */
 
-
+	      if (gimple_call_builtin_p (call, BUILT_IN_FREE))
+		state.handle_deallocation (call, ALLOCATOR_KIND_MALLOC);
 	    }
 
-#if 0
-	  if (gimple_call_builtin_p (stmt, BUILT_IN_MALLOC))
-	    handle_malloc (as_a <gcall *> (stmt));
-#endif
-
-	  if (gimple_call_builtin_p (stmt, BUILT_IN_FREE))
-	    handle_free (state, as_a <gcall *> (stmt));
 
 	  gsi_next (&si);
 	}
@@ -467,19 +483,8 @@ pass_heap_optimizations::execute (function *fun)
 /* Merging mallocs/frees.  */
 /* If we have a set of malloc in one BB and a set of frees in one BB... */
 
-#if 0
-void
-pass_heap_optimizations::handle_malloc (gcall *call)
-{
-  warning_at (call->location, 0, "found malloc");
-
-  tree size = gimple_call_arg (call, 0);
-  debug_tree (size);
-  //warning_at (call->location, 0, "arg0 size: %qE", size);
-}
-#endif
+/* Determine if CALL is a known allocator function, and return its kind.  */
 
-/* FIXME.  */
 static enum allocator_kind
 get_allocator_kind (gcall *call)
 {
@@ -503,12 +508,14 @@ get_allocator_kind (gcall *call)
   return ALLOCATOR_KIND_UNKNOWN;
 }
 
+/* Given RELEASE_CALL, a call to a known heap-deallocation function of
+   type KIND, attempt to locate the matching allocation call.  If
+   it is found, add it to STATE.
+   Warn about mismatches (e.g. delete() of malloc()-ed memory).  */
 
-/* FIXME.  */
-
-static void
-handle_release (function_state &state, gcall *release_call,
-		enum allocator_kind release_kind)
+void
+function_state::handle_deallocation (gcall *release_call,
+				     enum allocator_kind release_kind)
 {
   gcc_assert (release_kind != ALLOCATOR_KIND_UNKNOWN);
   //warning_at (call->location, 0, "found free");
@@ -541,44 +548,21 @@ handle_release (function_state &state, gcall *release_call,
 	{
 	  inform (allocator_call->location, "...for allocation here");
 	  // FIXME: fix-it hint?
-	  // FIXME: more test cases:
-	  // e.g. where it's unknown
-	  // e.g. for realloc
+	  /* FIXME: more test cases:
+	     - e.g. where it's unknown
+	     - e.g. for realloc.  */
 	}
       return;
     }
 
-  warning_at (def_stmt->location, 0, "found matching pair");
-  state.add_alloc_pair (allocator_call, release_call, acquire_kind);
-}
-
-/* FIXME.  */
-
-void
-pass_heap_optimizations::handle_free (function_state &state, gcall *call)
-{
-  handle_release (state, call, ALLOCATOR_KIND_MALLOC);
-}
-
-void
-pass_heap_optimizations::handle_operator_delete (function_state &state,
-						 gcall *call)
-{
-  warning_at (call->location, 0, "found delete");
-  handle_release (state, call, ALLOCATOR_KIND_NEW);
-}
-
-void
-pass_heap_optimizations::handle_operator_vec_delete (function_state &state,
-						     gcall *call)
-{
-  warning_at (call->location, 0, "found delete[]");
-  handle_release (state, call, ALLOCATOR_KIND_VEC_NEW);
+  if (0)
+    warning_at (def_stmt->location, 0, "found matching pair");
+  add_alloc_pair (allocator_call, release_call, acquire_kind);
 }
 
 /* FIXME.
    Convert alloca calls to something else.
-  
+
    If we have:
 
        ptr = (struct foo *)__builtin_alloca (sizeof (foo));
@@ -595,25 +579,28 @@ pass_heap_optimizations::handle_operator_vec_delete (function_state &state,
 void
 pass_heap_optimizations::maybe_optimize_alloca (gcall *call)
 {
-  warning_at (call->location, 0, "found alloca");
+  //warning_at (call->location, 0, "found alloca");
   // TODO
 
   tree size = gimple_call_arg (call, 0);
-  debug_tree (size);
+  if (0)
+    debug_tree (size);
 
   tree result = gimple_call_lhs (call);
-  debug_tree (result);
+  if (0)
+    debug_tree (result);
 
   /* Must be a fixed-size allocation.  */
   if (TREE_CODE (size) != INTEGER_CST)
-    return;  
+    return;
 
   /* FIXME.  */
   if (TREE_CODE (TREE_TYPE (result)) != POINTER_TYPE)
     return;
 
   tree obj_type = TREE_TYPE (TREE_TYPE (result));
-  debug_tree (obj_type);
+  if (0)
+    debug_tree (obj_type);
 
   /* Verify that size of allocation is sizeof (OBJ_TYPE).  */
   //unsigned HOST_WIDE_INT obj_size = ; //tree_to_uhwi (DECL_SIZE_UNIT (obj_type));
@@ -633,11 +620,10 @@ pass_heap_optimizations::maybe_optimize_alloca (gcall *call)
   // see e.g. in_loop_p within gimple-ssa-warn-alloca.c
 
   /* All checks passed.  */
+  // FIXME: convert to "remark"
   warning_at (call->location, 0, "converting alloca to local: obj_size: %E bits",
 	      TYPE_SIZE (obj_type));
 
-
-
   //tree new_local = make_ssa_name (obj_type);
 
   gimple_seq seq = NULL;
@@ -659,10 +645,10 @@ pass_heap_optimizations::maybe_optimize_alloca (gcall *call)
 
   SSA_NAME_DEF_STMT (result) = new_stmt;
 
-  // FIXME: remove call
+  /* Remove the alloca call.  */
 #if 1
   {
-    gimple_stmt_iterator gsi = gsi_for_stmt (call);  
+    gimple_stmt_iterator gsi = gsi_for_stmt (call);
     /* bool require_eh_edge_purge = */
     gsi_remove (&gsi, false /* true*/);
     unlink_stmt_vdef (call);
-- 
1.8.5.3

