public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix memory leaks
@ 2022-03-24 10:16 Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2022-03-24 10:16 UTC (permalink / raw)
  To: gcc-patches

When changing the predcom pass to use auto_vec leaks were introduced by
failing to replace deallocation with C++ delete.  The following does
this.  It also fixes leaks in vectorization and range folding.

Boostrapped and tested on x86_64-unknown-linux-gnu, pushed.

2022-03-24  Richard Biener  <rguenther@suse.de>

	* tree-predcom.cc (chain::chain): Add CTOR.
	(component::component): Likewise.
	(pcom_worker::release_chain): Use delete.
	(release_components): Likewise.
	(pcom_worker::filter_suitable_components): Likewise.
	(pcom_worker::split_data_refs_to_components): Use new.
	(make_invariant_chain): Likewise.
	(make_rooted_chain): Likewise.
	(pcom_worker::combine_chains): Likewise.
	* tree-vect-loop.cc (vect_create_epilog_for_reduction):
	Make sure to release previously constructed scalar_results.
	* tree-vect-stmts.cc (vectorizable_load): Use auto_vec
	for vec_offsets.
	* vr-values.cc (simplify_using_ranges::~simplify_using_ranges):
	Release m_flag_set_edges.
---
 gcc/tree-predcom.cc    | 28 +++++++++++++++-------------
 gcc/tree-vect-loop.cc  |  3 ++-
 gcc/tree-vect-stmts.cc |  2 +-
 gcc/vr-values.cc       |  1 +
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc
index 1d8121c4d80..e4aea7cdcb4 100644
--- a/gcc/tree-predcom.cc
+++ b/gcc/tree-predcom.cc
@@ -297,6 +297,11 @@ enum chain_type
 
 typedef struct chain
 {
+  chain (chain_type t) : type (t), op (ERROR_MARK), rslt_type (NULL_TREE),
+    ch1 (NULL), ch2 (NULL), length (0), init_seq (NULL), fini_seq (NULL),
+    has_max_use_after (false), all_always_accessed (false), combined (false),
+    inv_store_elimination (false) {}
+
   /* Type of the chain.  */
   enum chain_type type;
 
@@ -362,6 +367,8 @@ enum ref_step_type
 
 struct component
 {
+  component (bool es) : eliminate_store_p (es), next (NULL) {}
+
   /* The references in the component.  */
   auto_vec<dref> refs;
 
@@ -698,7 +705,7 @@ pcom_worker::release_chain (chain_p chain)
   if (chain->fini_seq)
     gimple_seq_discard (chain->fini_seq);
 
-  free (chain);
+  delete chain;
 }
 
 /* Frees CHAINS.  */
@@ -723,7 +730,7 @@ release_components (struct component *comps)
   for (act = comps; act; act = next)
     {
       next = act->next;
-      XDELETE (act);
+      delete act;
     }
 }
 
@@ -1023,9 +1030,8 @@ pcom_worker::split_data_refs_to_components ()
       comp = comps[ca];
       if (!comp)
 	{
-	  comp = XCNEW (struct component);
-	  comp->refs.create (comp_size[ca]);
-	  comp->eliminate_store_p = eliminate_store_p;
+	  comp = new component (eliminate_store_p);
+	  comp->refs.reserve_exact (comp_size[ca]);
 	  comps[ca] = comp;
 	}
 
@@ -1142,7 +1148,7 @@ pcom_worker::filter_suitable_components (struct component *comps)
 	  *comp = act->next;
 	  FOR_EACH_VEC_ELT (act->refs, i, ref)
 	    free (ref);
-	  XDELETE (act);
+	  delete act;
 	}
     }
 
@@ -1255,12 +1261,10 @@ add_ref_to_chain (chain_p chain, dref ref)
 static chain_p
 make_invariant_chain (struct component *comp)
 {
-  chain_p chain = XCNEW (struct chain);
+  chain_p chain = new struct chain (CT_INVARIANT);
   unsigned i;
   dref ref;
 
-  chain->type = CT_INVARIANT;
-
   chain->all_always_accessed = true;
 
   FOR_EACH_VEC_ELT (comp->refs, i, ref)
@@ -1280,9 +1284,8 @@ make_invariant_chain (struct component *comp)
 static chain_p
 make_rooted_chain (dref ref, enum chain_type type)
 {
-  chain_p chain = XCNEW (struct chain);
+  chain_p chain = new struct chain (type);
 
-  chain->type = type;
   chain->refs.safe_push (ref);
   chain->all_always_accessed = ref->always_accessed;
   ref->distance = 0;
@@ -2873,8 +2876,7 @@ pcom_worker::combine_chains (chain_p ch1, chain_p ch2)
   if (swap)
     std::swap (ch1, ch2);
 
-  new_chain = XCNEW (struct chain);
-  new_chain->type = CT_COMBINATION;
+  new_chain = new struct chain (CT_COMBINATION);
   new_chain->op = op;
   new_chain->ch1 = ch1;
   new_chain->ch2 = ch2;
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 7fcec12a3e9..7a74633e0b4 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -5466,7 +5466,8 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
   
   scalar_dest = gimple_get_lhs (orig_stmt_info->stmt);
   scalar_type = TREE_TYPE (scalar_dest);
-  scalar_results.create (group_size); 
+  scalar_results.truncate (0);
+  scalar_results.reserve_exact (group_size);
   new_scalar_dest = vect_create_destination_var (scalar_dest, NULL);
   bitsize = TYPE_SIZE (scalar_type);
 
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 5c9e8cfefa5..f7449a79d1c 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -9480,7 +9480,7 @@ vectorizable_load (vec_info *vinfo,
 					  memory_access_type);
     }
 
-  vec<tree> vec_offsets = vNULL;
+  auto_vec<tree> vec_offsets;
   auto_vec<tree> vec_masks;
   if (mask)
     {
diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc
index 0414020876a..f94da3130e3 100644
--- a/gcc/vr-values.cc
+++ b/gcc/vr-values.cc
@@ -4221,6 +4221,7 @@ simplify_using_ranges::simplify_using_ranges (range_query *query,
 simplify_using_ranges::~simplify_using_ranges ()
 {
   cleanup_edges_and_switches ();
+  m_flag_set_edges.release ();
 }
 
 /* Simplify STMT using ranges if possible.  */
-- 
2.34.1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] fix memory leaks
@ 2021-02-02 18:29 Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2021-02-02 18:29 UTC (permalink / raw)
  To: gcc-patches

This fixes various vec<> memory leaks as discovered compiling 521.wrf_r.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

2021-02-02  Richard Biener  <rguenther@suse.de>

	* gimple-loop-interchange.cc (prepare_data_references):
	Release vectors.
	* gimple-loop-jam.c (tree_loop_unroll_and_jam): Likewise.
	* tree-ssa-loop-im.c (hoist_memory_references): Likewise.
	* tree-vect-stmts.c (vectorizable_condition): Do not
	allocate vectors.
	(vectorizable_comparison): Likewise.
---
 gcc/gimple-loop-interchange.cc | 10 ++++++++--
 gcc/gimple-loop-jam.c          |  6 ++----
 gcc/tree-ssa-loop-im.c         |  1 +
 gcc/tree-vect-stmts.c          | 13 -------------
 4 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
index a1dadd8e5d4..f45b9364644 100644
--- a/gcc/gimple-loop-interchange.cc
+++ b/gcc/gimple-loop-interchange.cc
@@ -1940,7 +1940,10 @@ prepare_data_references (class loop *loop, vec<data_reference_p> *datarefs)
           delete bb_refs;
         }
       else if (bb_refs->is_empty ())
-	delete bb_refs;
+	{
+	  bb_refs->release ();
+	  delete bb_refs;
+	}
       else
 	bb->aux = bb_refs;
     }
@@ -1954,7 +1957,10 @@ prepare_data_references (class loop *loop, vec<data_reference_p> *datarefs)
 
       bb_refs = (vec<data_reference_p> *) bb->aux;
       if (loop_nest && flow_bb_inside_loop_p (loop_nest, bb))
-	datarefs->safe_splice (*bb_refs);
+	{
+	  datarefs->safe_splice (*bb_refs);
+	  bb_refs->release ();
+	}
       else
 	free_data_refs (*bb_refs);
 
diff --git a/gcc/gimple-loop-jam.c b/gcc/gimple-loop-jam.c
index 485f5a9bf61..69dbaeb6cb9 100644
--- a/gcc/gimple-loop-jam.c
+++ b/gcc/gimple-loop-jam.c
@@ -505,15 +505,13 @@ tree_loop_unroll_and_jam (void)
       if (!unroll_jam_possible_p (outer, loop))
 	continue;
 
-      vec<data_reference_p> datarefs;
-      vec<ddr_p> dependences;
+      vec<data_reference_p> datarefs = vNULL;
+      vec<ddr_p> dependences = vNULL;
       unsigned unroll_factor, profit_unroll, removed;
       class tree_niter_desc desc;
       bool unroll = false;
 
       auto_vec<loop_p, 3> loop_nest;
-      dependences.create (10);
-      datarefs.create (10);
       if (!compute_data_dependences_for_loop (outer, true, &loop_nest,
 					      &datarefs, &dependences))
 	{
diff --git a/gcc/tree-ssa-loop-im.c b/gcc/tree-ssa-loop-im.c
index 445b93f7979..8034cf68d27 100644
--- a/gcc/tree-ssa-loop-im.c
+++ b/gcc/tree-ssa-loop-im.c
@@ -2508,6 +2508,7 @@ hoist_memory_references (class loop *loop, bitmap mem_refs,
       if (res != 1)
 	{
 	  bitmap_copy (refs_not_supported, mem_refs);
+	  seq.release ();
 	  break;
 	}
       sms.safe_push (std::make_pair (e, seq));
diff --git a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c
index f180ced3124..5eb7b2d1d6e 100644
--- a/gcc/tree-vect-stmts.c
+++ b/gcc/tree-vect-stmts.c
@@ -10085,14 +10085,6 @@ vectorizable_condition (vec_info *vinfo,
 
   /* Transform.  */
 
-  if (!slp_node)
-    {
-      vec_oprnds0.create (1);
-      vec_oprnds1.create (1);
-      vec_oprnds2.create (1);
-      vec_oprnds3.create (1);
-    }
-
   /* Handle def.  */
   scalar_dest = gimple_assign_lhs (stmt);
   if (reduction_type != EXTRACT_LAST_REDUCTION)
@@ -10480,11 +10472,6 @@ vectorizable_comparison (vec_info *vinfo,
     }
 
   /* Transform.  */
-  if (!slp_node)
-    {
-      vec_oprnds0.create (1);
-      vec_oprnds1.create (1);
-    }
 
   /* Handle def.  */
   lhs = gimple_assign_lhs (stmt);
-- 
2.26.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix memory leaks
  2015-11-06 12:18 [PATCH] Fix " Richard Biener
@ 2015-11-06 13:43 ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2015-11-06 13:43 UTC (permalink / raw)
  To: gcc-patches

On Fri, 6 Nov 2015, Richard Biener wrote:

> 
> A few, spotted with valgrind.  One is even mine ;)
> 
> Bootstrap and regtest running on x86_64-unknown-linux-gnu.

And this is what I committed (one extra leak in postreload-gcse).

Richard.

2015-11-06  Richard Biener  <rguenther@suse.de>

	* tree-ssa-sccvn.c (class sccvn_dom_walker): Add destructor.
	* lra.c (init_reg_info): Truncate copy_vec instead of
	re-allocating a new one and leaking the old.
	* ipa-inline-analysis.c (estimate_function_body_sizes): Free
	bb_infos vec.
	* sched-deps.c (sched_deps_finish): Free the dn/dl pools.
	* postreload-gcse.c (free_mem): Free modify_mem_list and
	canon_modify_mem_list.

Index: gcc/tree-ssa-sccvn.c
===================================================================
*** gcc/tree-ssa-sccvn.c	(revision 229842)
--- gcc/tree-ssa-sccvn.c	(working copy)
*************** class sccvn_dom_walker : public dom_walk
*** 4154,4159 ****
--- 4199,4205 ----
  public:
    sccvn_dom_walker ()
      : dom_walker (CDI_DOMINATORS), fail (false), cond_stack (vNULL) {}
+   ~sccvn_dom_walker ();
  
    virtual void before_dom_children (basic_block);
    virtual void after_dom_children (basic_block);
*************** public:
*** 4168,4173 ****
--- 4214,4224 ----
      cond_stack;
  };
  
+ sccvn_dom_walker::~sccvn_dom_walker ()
+ {
+   cond_stack.release ();
+ }
+ 
  /* Record a temporary condition for the BB and its dominated blocks.  */
  
  void
Index: gcc/ipa-inline-analysis.c
===================================================================
*** gcc/ipa-inline-analysis.c	(revision 229842)
--- gcc/ipa-inline-analysis.c	(working copy)
*************** estimate_function_body_sizes (struct cgr
*** 2853,2858 ****
--- 2853,2859 ----
    inline_summaries->get (node)->self_time = time;
    inline_summaries->get (node)->self_size = size;
    nonconstant_names.release ();
+   fbi.bb_infos.release ();
    if (opt_for_fn (node->decl, optimize))
      {
        if (!early)
Index: gcc/sched-deps.c
===================================================================
*** gcc/sched-deps.c	(revision 229842)
--- gcc/sched-deps.c	(working copy)
*************** void
*** 4092,4100 ****
  sched_deps_finish (void)
  {
    gcc_assert (deps_pools_are_empty_p ());
!   dn_pool->release_if_empty ();
    dn_pool = NULL;
-   dl_pool->release_if_empty ();
    dl_pool = NULL;
  
    h_d_i_d.release ();
--- 4092,4100 ----
  sched_deps_finish (void)
  {
    gcc_assert (deps_pools_are_empty_p ());
!   delete dn_pool;
!   delete dl_pool;
    dn_pool = NULL;
    dl_pool = NULL;
  
    h_d_i_d.release ();
Index: gcc/lra.c
===================================================================
--- gcc/lra.c	(revision 229843)
+++ gcc/lra.c	(working copy)
@@ -1293,7 +1293,7 @@ init_reg_info (void)
   lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
   for (i = 0; i < reg_info_size; i++)
     initialize_lra_reg_info_element (i);
-  copy_vec.create (100);
+  copy_vec.truncate (0);
 }
 
 
Index: gcc/postreload-gcse.c
===================================================================
--- gcc/postreload-gcse.c	(revision 229842)
+++ gcc/postreload-gcse.c	(working copy)
@@ -348,6 +348,8 @@ free_mem (void)
   BITMAP_FREE (blocks_with_calls);
   BITMAP_FREE (modify_mem_list_set);
   free (reg_avail_info);
+  free (modify_mem_list);
+  free (canon_modify_mem_list);
 }
 \f
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] Fix memory leaks
@ 2015-11-06 12:18 Richard Biener
  2015-11-06 13:43 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Biener @ 2015-11-06 12:18 UTC (permalink / raw)
  To: gcc-patches


A few, spotted with valgrind.  One is even mine ;)

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

Richard.

2015-11-06  Richard Biener  <rguenther@suse.de>

	* tree-ssa-sccvn.c (class sccvn_dom_walker): Add destructor.
	* lra.c (init_reg_info): Truncate copy_vec instead of
	re-allocating a new one and leaking the old.
	* ipa-inline-analysis.c (estimate_function_body_sizes): Free
	bb_infos vec.
	* sched-deps.c (sched_deps_finish): Free the dn/dl pools.

Index: gcc/tree-ssa-sccvn.c
===================================================================
*** gcc/tree-ssa-sccvn.c	(revision 229842)
--- gcc/tree-ssa-sccvn.c	(working copy)
*************** class sccvn_dom_walker : public dom_walk
*** 4154,4159 ****
--- 4199,4205 ----
  public:
    sccvn_dom_walker ()
      : dom_walker (CDI_DOMINATORS), fail (false), cond_stack (vNULL) {}
+   ~sccvn_dom_walker ();
  
    virtual void before_dom_children (basic_block);
    virtual void after_dom_children (basic_block);
*************** public:
*** 4168,4173 ****
--- 4214,4224 ----
      cond_stack;
  };
  
+ sccvn_dom_walker::~sccvn_dom_walker ()
+ {
+   cond_stack.release ();
+ }
+ 
  /* Record a temporary condition for the BB and its dominated blocks.  */
  
  void
Index: gcc/ipa-inline-analysis.c
===================================================================
*** gcc/ipa-inline-analysis.c	(revision 229842)
--- gcc/ipa-inline-analysis.c	(working copy)
*************** estimate_function_body_sizes (struct cgr
*** 2853,2858 ****
--- 2853,2859 ----
    inline_summaries->get (node)->self_time = time;
    inline_summaries->get (node)->self_size = size;
    nonconstant_names.release ();
+   fbi.bb_infos.release ();
    if (opt_for_fn (node->decl, optimize))
      {
        if (!early)
Index: gcc/sched-deps.c
===================================================================
*** gcc/sched-deps.c	(revision 229842)
--- gcc/sched-deps.c	(working copy)
*************** void
*** 4092,4100 ****
  sched_deps_finish (void)
  {
    gcc_assert (deps_pools_are_empty_p ());
!   dn_pool->release_if_empty ();
    dn_pool = NULL;
-   dl_pool->release_if_empty ();
    dl_pool = NULL;
  
    h_d_i_d.release ();
--- 4092,4100 ----
  sched_deps_finish (void)
  {
    gcc_assert (deps_pools_are_empty_p ());
!   delete dn_pool;
!   delete dl_pool;
    dn_pool = NULL;
    dl_pool = NULL;
  
    h_d_i_d.release ();
Index: gcc/lra.c
===================================================================
--- gcc/lra.c	(revision 229843)
+++ gcc/lra.c	(working copy)
@@ -1293,7 +1293,7 @@ init_reg_info (void)
   lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
   for (i = 0; i < reg_info_size; i++)
     initialize_lra_reg_info_element (i);
-  copy_vec.create (100);
+  copy_vec.truncate (0);
 }
 
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-24 10:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 10:16 [PATCH] Fix memory leaks Richard Biener
  -- strict thread matches above, loose matches on Subject: below --
2021-02-02 18:29 [PATCH] fix " Richard Biener
2015-11-06 12:18 [PATCH] Fix " Richard Biener
2015-11-06 13:43 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).