From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by sourceware.org (Postfix) with ESMTPS id DBFE8397303E for ; Fri, 8 Jan 2021 14:33:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DBFE8397303E Received: by mail-ed1-x529.google.com with SMTP id y24so11356764edt.10 for ; Fri, 08 Jan 2021 06:33:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=tBHFq+0rm8lAxggDpU5JqIubJJmlQuI6iV/GBu5layU=; b=cxHgi5mD8oSlrUMBrKdeL7beMSa3fJhpPGfVaof/ylkU8+IOxOzOZYtsOV4TPmpBuF +Z8AJwhEBlrwSI9P8cZ9h8SuJuVjVpsIcL+X0sr/jQQYzhwGQwYf2p7diG2uMqZrx0O7 5Qlzz9MLdHFaLIHz3su+S6lOMQV+oKIg1HplzLvaE5DeT6WP3uZUOBkwiMLGQY36Krvj ShUuaCAeoyh3MwiyhsS1V7THCA2xJlvngNyBflR2pZBiQwMhHuSs2D6RAz7PlkoS/Qz5 DL2j1sdfiB1Yd/zvGHEm4BmJJVL1wh2+Sx5AtNWJuungHmkMfEPo1WVwQy2Ya0zVK3Q2 G7OQ== X-Gm-Message-State: AOAM533NQ1YWmqAVfj1WBrNzNY3wxz0THHrpEAZZCfjVc9uuGVUsCi38 o5zrVoQyWMNEaH3zA8kyieG7g2rZS4KhO6nTXZk= X-Google-Smtp-Source: ABdhPJyeLwX3+siCi7HaP01KiwZAFI3YbRVbLs+r46plm7lF53dNQwCqfwyl1L5qbMtkruQcuKyLsHAXD3t44mPjbVs= X-Received: by 2002:aa7:ce94:: with SMTP id y20mr5369026edv.361.1610116420825; Fri, 08 Jan 2021 06:33:40 -0800 (PST) MIME-Version: 1.0 References: <1786fedc-b48b-091a-bdf1-e8afa31767ba@suse.cz> In-Reply-To: <1786fedc-b48b-091a-bdf1-e8afa31767ba@suse.cz> From: Richard Biener Date: Fri, 8 Jan 2021 15:33:29 +0100 Message-ID: Subject: Re: [PATCH] if-to-switch: remove memory leaks To: =?UTF-8?Q?Martin_Li=C5=A1ka?= Cc: GCC Patches Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jan 2021 14:33:44 -0000 On Fri, Jan 8, 2021 at 3:27 PM Martin Li=C5=A1ka wrote: > > The patch removes some memory leaks spotted by valgrind. > > Patch can bootstrap on x86_64-linux-gnu and survives regression tests. > > Ready to be installed? OK. Richard. > Thanks, > Martin > > > gcc/ChangeLog: > > * gimple-if-to-switch.cc (struct condition_info): Use auto_var. > (if_chain::is_beneficial): Delete clusters > (find_conditions): Make second argument of conditions_in_bbs a > pointer so that we control over it's lifetime. > (pass_if_to_switch::execute): Delete them. > --- > gcc/gimple-if-to-switch.cc | 97 ++++++++++++++++++++++---------------- > 1 file changed, 56 insertions(+), 41 deletions(-) > > diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc > index 6dba4e2c39c..560753d0311 100644 > --- a/gcc/gimple-if-to-switch.cc > +++ b/gcc/gimple-if-to-switch.cc > @@ -59,7 +59,7 @@ using namespace tree_switch_conversion; > > struct condition_info > { > - typedef vec> mapping_vec; > + typedef auto_vec> mapping_vec; > > condition_info (gcond *cond): m_cond (cond), m_bb (gimple_bb (cond)), > m_forwarder_bb (NULL), m_ranges (), m_true_edge (NULL), m_false_edg= e (NULL), > @@ -75,7 +75,7 @@ struct condition_info > gcond *m_cond; > basic_block m_bb; > basic_block m_forwarder_bb; > - vec m_ranges; > + auto_vec m_ranges; > edge m_true_edge; > edge m_false_edge; > mapping_vec m_true_edge_phi_mapping; > @@ -253,6 +253,10 @@ if_chain::is_beneficial () > r =3D output.length () < filtered_clusters.length (); > if (r) > dump_clusters (&output, "BT can be built"); > + > + for (unsigned i =3D 0; i < output.length (); i++) > + delete output[i]; > + > output.release (); > return r; > } > @@ -377,7 +381,7 @@ convert_if_conditions_to_switch (if_chain *chain) > > static void > find_conditions (basic_block bb, > - hash_map *conditions_in_bbs= ) > + hash_map *conditions_in_b= bs) > { > gimple_stmt_iterator gsi =3D gsi_last_nondebug_bb (bb); > if (gsi_end_p (gsi)) > @@ -394,7 +398,7 @@ find_conditions (basic_block bb, > tree rhs =3D gimple_cond_rhs (cond); > tree_code code =3D gimple_cond_code (cond); > > - condition_info info (cond); > + condition_info *info =3D new condition_info (cond); > > gassign *def; > if (code =3D=3D NE_EXPR > @@ -405,49 +409,53 @@ find_conditions (basic_block bb, > enum tree_code rhs_code =3D gimple_assign_rhs_code (def); > if (rhs_code =3D=3D BIT_IOR_EXPR) > { > - info.m_ranges.safe_grow (2, true); > - init_range_entry (&info.m_ranges[0], gimple_assign_rhs1 (def), = NULL); > - init_range_entry (&info.m_ranges[1], gimple_assign_rhs2 (def), = NULL); > + info->m_ranges.safe_grow (2, true); > + init_range_entry (&info->m_ranges[0], gimple_assign_rhs1 (def),= NULL); > + init_range_entry (&info->m_ranges[1], gimple_assign_rhs2 (def),= NULL); > } > } > else > { > - info.m_ranges.safe_grow (1, true); > - init_range_entry (&info.m_ranges[0], NULL_TREE, cond); > + info->m_ranges.safe_grow (1, true); > + init_range_entry (&info->m_ranges[0], NULL_TREE, cond); > } > > /* All identified ranges must have equal expression and IN_P flag. *= / > - if (!info.m_ranges.is_empty ()) > + if (!info->m_ranges.is_empty ()) > { > edge true_edge, false_edge; > - tree expr =3D info.m_ranges[0].exp; > - bool in_p =3D info.m_ranges[0].in_p; > + tree expr =3D info->m_ranges[0].exp; > + bool in_p =3D info->m_ranges[0].in_p; > > extract_true_false_edges_from_block (bb, &true_edge, &false_edge)= ; > - info.m_true_edge =3D in_p ? true_edge : false_edge; > - info.m_false_edge =3D in_p ? false_edge : true_edge; > - > - for (unsigned i =3D 0; i < info.m_ranges.length (); ++i) > - if (info.m_ranges[i].exp =3D=3D NULL_TREE > - || !INTEGRAL_TYPE_P (TREE_TYPE (info.m_ranges[i].exp)) > - || info.m_ranges[i].low =3D=3D NULL_TREE > - || info.m_ranges[i].high =3D=3D NULL_TREE > - || (TYPE_PRECISION (TREE_TYPE (info.m_ranges[i].low)) > - !=3D TYPE_PRECISION (TREE_TYPE (info.m_ranges[i].high)))) > - return; > - > - for (unsigned i =3D 1; i < info.m_ranges.length (); ++i) > - if (info.m_ranges[i].exp !=3D expr > - || info.m_ranges[i].in_p !=3D in_p) > - return; > - > - info.record_phi_mapping (info.m_true_edge, > - &info.m_true_edge_phi_mapping); > - info.record_phi_mapping (info.m_false_edge, > - &info.m_false_edge_phi_mapping); > + info->m_true_edge =3D in_p ? true_edge : false_edge; > + info->m_false_edge =3D in_p ? false_edge : true_edge; > + > + for (unsigned i =3D 0; i < info->m_ranges.length (); ++i) > + if (info->m_ranges[i].exp =3D=3D NULL_TREE > + || !INTEGRAL_TYPE_P (TREE_TYPE (info->m_ranges[i].exp)) > + || info->m_ranges[i].low =3D=3D NULL_TREE > + || info->m_ranges[i].high =3D=3D NULL_TREE > + || (TYPE_PRECISION (TREE_TYPE (info->m_ranges[i].low)) > + !=3D TYPE_PRECISION (TREE_TYPE (info->m_ranges[i].high)))= ) > + goto exit; > + > + for (unsigned i =3D 1; i < info->m_ranges.length (); ++i) > + if (info->m_ranges[i].exp !=3D expr > + || info->m_ranges[i].in_p !=3D in_p) > + goto exit; > + > + info->record_phi_mapping (info->m_true_edge, > + &info->m_true_edge_phi_mapping); > + info->record_phi_mapping (info->m_false_edge, > + &info->m_false_edge_phi_mapping); > conditions_in_bbs->put (bb, info); > } > > + return; > + > +exit: > + delete info; > } > > namespace { > @@ -487,7 +495,7 @@ unsigned int > pass_if_to_switch::execute (function *fun) > { > auto_vec all_candidates; > - hash_map conditions_in_bbs; > + hash_map conditions_in_bbs; > > basic_block bb; > FOR_EACH_BB_FN (bb, fun) > @@ -507,9 +515,10 @@ pass_if_to_switch::execute (function *fun) > continue; > > bitmap_set_bit (seen_bbs, bb->index); > - condition_info *info =3D conditions_in_bbs.get (bb); > - if (info) > + condition_info **slot =3D conditions_in_bbs.get (bb); > + if (slot) > { > + condition_info *info =3D *slot; > if_chain *chain =3D new if_chain (); > chain->m_entries.safe_push (info); > /* Try to find a chain starting in this BB. */ > @@ -518,19 +527,19 @@ pass_if_to_switch::execute (function *fun) > if (!single_pred_p (gimple_bb (info->m_cond))) > break; > edge e =3D single_pred_edge (gimple_bb (info->m_cond)); > - condition_info *info2 =3D conditions_in_bbs.get (e->src); > - if (!info2 || info->m_ranges[0].exp !=3D info2->m_ranges[0]= .exp) > + condition_info **info2 =3D conditions_in_bbs.get (e->src); > + if (!info2 || info->m_ranges[0].exp !=3D (*info2)->m_ranges= [0].exp) > break; > > /* It is important that the blocks are linked through FALSE= _EDGE. > For an expression of index !=3D VALUE, true and false ed= ges > are flipped. */ > - if (info2->m_false_edge !=3D e) > + if ((*info2)->m_false_edge !=3D e) > break; > > - chain->m_entries.safe_push (info2); > + chain->m_entries.safe_push (*info2); > bitmap_set_bit (seen_bbs, e->src->index); > - info =3D info2; > + info =3D *info2; > } > > chain->m_entries.reverse (); > @@ -546,6 +555,8 @@ pass_if_to_switch::execute (function *fun) > chain->m_entries.length ()); > all_candidates.safe_push (chain); > } > + else > + delete chain; > } > } > > @@ -557,6 +568,10 @@ pass_if_to_switch::execute (function *fun) > > free (rpo); > > + for (hash_map::iterator it > + =3D conditions_in_bbs.begin (); it !=3D conditions_in_bbs.end ();= ++it) > + delete (*it).second; > + > if (!all_candidates.is_empty ()) > { > free_dominance_info (CDI_DOMINATORS); > -- > 2.29.2 >