xref: /dflybsd-src/contrib/gcc-8.0/gcc/gimple-iterator.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Iterator routines for GIMPLE statements.
2*38fd1498Szrj    Copyright (C) 2007-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Aldy Hernandez  <aldy@quesejoda.com>
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj 
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj 
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
20*38fd1498Szrj 
21*38fd1498Szrj #include "config.h"
22*38fd1498Szrj #include "system.h"
23*38fd1498Szrj #include "coretypes.h"
24*38fd1498Szrj #include "backend.h"
25*38fd1498Szrj #include "tree.h"
26*38fd1498Szrj #include "gimple.h"
27*38fd1498Szrj #include "cfghooks.h"
28*38fd1498Szrj #include "ssa.h"
29*38fd1498Szrj #include "cgraph.h"
30*38fd1498Szrj #include "tree-eh.h"
31*38fd1498Szrj #include "gimple-iterator.h"
32*38fd1498Szrj #include "tree-cfg.h"
33*38fd1498Szrj #include "tree-ssa.h"
34*38fd1498Szrj #include "value-prof.h"
35*38fd1498Szrj 
36*38fd1498Szrj 
37*38fd1498Szrj /* Mark the statement STMT as modified, and update it.  */
38*38fd1498Szrj 
39*38fd1498Szrj static inline void
update_modified_stmt(gimple * stmt)40*38fd1498Szrj update_modified_stmt (gimple *stmt)
41*38fd1498Szrj {
42*38fd1498Szrj   if (!ssa_operands_active (cfun))
43*38fd1498Szrj     return;
44*38fd1498Szrj   update_stmt_if_modified (stmt);
45*38fd1498Szrj }
46*38fd1498Szrj 
47*38fd1498Szrj 
48*38fd1498Szrj /* Mark the statements in SEQ as modified, and update them.  */
49*38fd1498Szrj 
50*38fd1498Szrj void
update_modified_stmts(gimple_seq seq)51*38fd1498Szrj update_modified_stmts (gimple_seq seq)
52*38fd1498Szrj {
53*38fd1498Szrj   gimple_stmt_iterator gsi;
54*38fd1498Szrj 
55*38fd1498Szrj   if (!ssa_operands_active (cfun))
56*38fd1498Szrj     return;
57*38fd1498Szrj   for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
58*38fd1498Szrj     update_stmt_if_modified (gsi_stmt (gsi));
59*38fd1498Szrj }
60*38fd1498Szrj 
61*38fd1498Szrj 
62*38fd1498Szrj /* Set BB to be the basic block for all the statements in the list
63*38fd1498Szrj    starting at FIRST and LAST.  */
64*38fd1498Szrj 
65*38fd1498Szrj static void
update_bb_for_stmts(gimple_seq_node first,gimple_seq_node last,basic_block bb)66*38fd1498Szrj update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last,
67*38fd1498Szrj 		     basic_block bb)
68*38fd1498Szrj {
69*38fd1498Szrj   gimple_seq_node n;
70*38fd1498Szrj 
71*38fd1498Szrj   for (n = first; n; n = n->next)
72*38fd1498Szrj     {
73*38fd1498Szrj       gimple_set_bb (n, bb);
74*38fd1498Szrj       if (n == last)
75*38fd1498Szrj 	break;
76*38fd1498Szrj     }
77*38fd1498Szrj }
78*38fd1498Szrj 
79*38fd1498Szrj /* Set the frequencies for the cgraph_edges for each of the calls
80*38fd1498Szrj    starting at FIRST for their new position within BB.  */
81*38fd1498Szrj 
82*38fd1498Szrj static void
update_call_edge_frequencies(gimple_seq_node first,basic_block bb)83*38fd1498Szrj update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
84*38fd1498Szrj {
85*38fd1498Szrj   struct cgraph_node *cfun_node = NULL;
86*38fd1498Szrj   gimple_seq_node n;
87*38fd1498Szrj 
88*38fd1498Szrj   for (n = first; n ; n = n->next)
89*38fd1498Szrj     if (is_gimple_call (n))
90*38fd1498Szrj       {
91*38fd1498Szrj 	struct cgraph_edge *e;
92*38fd1498Szrj 
93*38fd1498Szrj 	/* These function calls are expensive enough that we want
94*38fd1498Szrj 	   to avoid calling them if we never see any calls.  */
95*38fd1498Szrj 	if (cfun_node == NULL)
96*38fd1498Szrj 	  cfun_node = cgraph_node::get (current_function_decl);
97*38fd1498Szrj 
98*38fd1498Szrj 	e = cfun_node->get_edge (n);
99*38fd1498Szrj 	if (e != NULL)
100*38fd1498Szrj 	  e->count = bb->count;
101*38fd1498Szrj       }
102*38fd1498Szrj }
103*38fd1498Szrj 
104*38fd1498Szrj /* Insert the sequence delimited by nodes FIRST and LAST before
105*38fd1498Szrj    iterator I.  M specifies how to update iterator I after insertion
106*38fd1498Szrj    (see enum gsi_iterator_update).
107*38fd1498Szrj 
108*38fd1498Szrj    This routine assumes that there is a forward and backward path
109*38fd1498Szrj    between FIRST and LAST (i.e., they are linked in a doubly-linked
110*38fd1498Szrj    list).  Additionally, if FIRST == LAST, this routine will properly
111*38fd1498Szrj    insert a single node.  */
112*38fd1498Szrj 
113*38fd1498Szrj static void
gsi_insert_seq_nodes_before(gimple_stmt_iterator * i,gimple_seq_node first,gimple_seq_node last,enum gsi_iterator_update mode)114*38fd1498Szrj gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
115*38fd1498Szrj 			     gimple_seq_node first,
116*38fd1498Szrj 			     gimple_seq_node last,
117*38fd1498Szrj 			     enum gsi_iterator_update mode)
118*38fd1498Szrj {
119*38fd1498Szrj   basic_block bb;
120*38fd1498Szrj   gimple_seq_node cur = i->ptr;
121*38fd1498Szrj 
122*38fd1498Szrj   gcc_assert (!cur || cur->prev);
123*38fd1498Szrj 
124*38fd1498Szrj   if ((bb = gsi_bb (*i)) != NULL)
125*38fd1498Szrj     update_bb_for_stmts (first, last, bb);
126*38fd1498Szrj 
127*38fd1498Szrj   /* Link SEQ before CUR in the sequence.  */
128*38fd1498Szrj   if (cur)
129*38fd1498Szrj     {
130*38fd1498Szrj       first->prev = cur->prev;
131*38fd1498Szrj       if (first->prev->next)
132*38fd1498Szrj 	first->prev->next = first;
133*38fd1498Szrj       else
134*38fd1498Szrj 	gimple_seq_set_first (i->seq, first);
135*38fd1498Szrj       last->next = cur;
136*38fd1498Szrj       cur->prev = last;
137*38fd1498Szrj     }
138*38fd1498Szrj   else
139*38fd1498Szrj     {
140*38fd1498Szrj       gimple_seq_node itlast = gimple_seq_last (*i->seq);
141*38fd1498Szrj 
142*38fd1498Szrj       /* If CUR is NULL, we link at the end of the sequence (this case happens
143*38fd1498Szrj 	 when gsi_after_labels is called for a basic block that contains only
144*38fd1498Szrj 	 labels, so it returns an iterator after the end of the block, and
145*38fd1498Szrj 	 we need to insert before it; it might be cleaner to add a flag to the
146*38fd1498Szrj 	 iterator saying whether we are at the start or end of the list).  */
147*38fd1498Szrj       last->next = NULL;
148*38fd1498Szrj       if (itlast)
149*38fd1498Szrj 	{
150*38fd1498Szrj 	  first->prev = itlast;
151*38fd1498Szrj 	  itlast->next = first;
152*38fd1498Szrj 	}
153*38fd1498Szrj       else
154*38fd1498Szrj 	gimple_seq_set_first (i->seq, first);
155*38fd1498Szrj       gimple_seq_set_last (i->seq, last);
156*38fd1498Szrj     }
157*38fd1498Szrj 
158*38fd1498Szrj   /* Update the iterator, if requested.  */
159*38fd1498Szrj   switch (mode)
160*38fd1498Szrj     {
161*38fd1498Szrj     case GSI_NEW_STMT:
162*38fd1498Szrj     case GSI_CONTINUE_LINKING:
163*38fd1498Szrj       i->ptr = first;
164*38fd1498Szrj       break;
165*38fd1498Szrj     case GSI_SAME_STMT:
166*38fd1498Szrj       break;
167*38fd1498Szrj     default:
168*38fd1498Szrj       gcc_unreachable ();
169*38fd1498Szrj     }
170*38fd1498Szrj }
171*38fd1498Szrj 
172*38fd1498Szrj 
173*38fd1498Szrj /* Inserts the sequence of statements SEQ before the statement pointed
174*38fd1498Szrj    by iterator I.  MODE indicates what to do with the iterator after
175*38fd1498Szrj    insertion (see enum gsi_iterator_update).
176*38fd1498Szrj 
177*38fd1498Szrj    This function does not scan for new operands.  It is provided for
178*38fd1498Szrj    the use of the gimplifier, which manipulates statements for which
179*38fd1498Szrj    def/use information has not yet been constructed.  Most callers
180*38fd1498Szrj    should use gsi_insert_seq_before.  */
181*38fd1498Szrj 
182*38fd1498Szrj void
gsi_insert_seq_before_without_update(gimple_stmt_iterator * i,gimple_seq seq,enum gsi_iterator_update mode)183*38fd1498Szrj gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
184*38fd1498Szrj                                       enum gsi_iterator_update mode)
185*38fd1498Szrj {
186*38fd1498Szrj   gimple_seq_node first, last;
187*38fd1498Szrj 
188*38fd1498Szrj   if (seq == NULL)
189*38fd1498Szrj     return;
190*38fd1498Szrj 
191*38fd1498Szrj   /* Don't allow inserting a sequence into itself.  */
192*38fd1498Szrj   gcc_assert (seq != *i->seq);
193*38fd1498Szrj 
194*38fd1498Szrj   first = gimple_seq_first (seq);
195*38fd1498Szrj   last = gimple_seq_last (seq);
196*38fd1498Szrj 
197*38fd1498Szrj   /* Empty sequences need no work.  */
198*38fd1498Szrj   if (!first || !last)
199*38fd1498Szrj     {
200*38fd1498Szrj       gcc_assert (first == last);
201*38fd1498Szrj       return;
202*38fd1498Szrj     }
203*38fd1498Szrj 
204*38fd1498Szrj   gsi_insert_seq_nodes_before (i, first, last, mode);
205*38fd1498Szrj }
206*38fd1498Szrj 
207*38fd1498Szrj 
208*38fd1498Szrj /* Inserts the sequence of statements SEQ before the statement pointed
209*38fd1498Szrj    by iterator I.  MODE indicates what to do with the iterator after
210*38fd1498Szrj    insertion (see enum gsi_iterator_update). Scan the statements in SEQ
211*38fd1498Szrj    for new operands.  */
212*38fd1498Szrj 
213*38fd1498Szrj void
gsi_insert_seq_before(gimple_stmt_iterator * i,gimple_seq seq,enum gsi_iterator_update mode)214*38fd1498Szrj gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
215*38fd1498Szrj 		       enum gsi_iterator_update mode)
216*38fd1498Szrj {
217*38fd1498Szrj   update_modified_stmts (seq);
218*38fd1498Szrj   gsi_insert_seq_before_without_update (i, seq, mode);
219*38fd1498Szrj }
220*38fd1498Szrj 
221*38fd1498Szrj 
222*38fd1498Szrj /* Insert the sequence delimited by nodes FIRST and LAST after
223*38fd1498Szrj    iterator I.  M specifies how to update iterator I after insertion
224*38fd1498Szrj    (see enum gsi_iterator_update).
225*38fd1498Szrj 
226*38fd1498Szrj    This routine assumes that there is a forward and backward path
227*38fd1498Szrj    between FIRST and LAST (i.e., they are linked in a doubly-linked
228*38fd1498Szrj    list).  Additionally, if FIRST == LAST, this routine will properly
229*38fd1498Szrj    insert a single node.  */
230*38fd1498Szrj 
231*38fd1498Szrj static void
gsi_insert_seq_nodes_after(gimple_stmt_iterator * i,gimple_seq_node first,gimple_seq_node last,enum gsi_iterator_update m)232*38fd1498Szrj gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
233*38fd1498Szrj 			    gimple_seq_node first,
234*38fd1498Szrj 			    gimple_seq_node last,
235*38fd1498Szrj 			    enum gsi_iterator_update m)
236*38fd1498Szrj {
237*38fd1498Szrj   basic_block bb;
238*38fd1498Szrj   gimple_seq_node cur = i->ptr;
239*38fd1498Szrj 
240*38fd1498Szrj   gcc_assert (!cur || cur->prev);
241*38fd1498Szrj 
242*38fd1498Szrj   /* If the iterator is inside a basic block, we need to update the
243*38fd1498Szrj      basic block information for all the nodes between FIRST and LAST.  */
244*38fd1498Szrj   if ((bb = gsi_bb (*i)) != NULL)
245*38fd1498Szrj     update_bb_for_stmts (first, last, bb);
246*38fd1498Szrj 
247*38fd1498Szrj   /* Link SEQ after CUR.  */
248*38fd1498Szrj   if (cur)
249*38fd1498Szrj     {
250*38fd1498Szrj       last->next = cur->next;
251*38fd1498Szrj       if (last->next)
252*38fd1498Szrj 	{
253*38fd1498Szrj 	  last->next->prev = last;
254*38fd1498Szrj 	}
255*38fd1498Szrj       else
256*38fd1498Szrj 	gimple_seq_set_last (i->seq, last);
257*38fd1498Szrj       first->prev = cur;
258*38fd1498Szrj       cur->next = first;
259*38fd1498Szrj     }
260*38fd1498Szrj   else
261*38fd1498Szrj     {
262*38fd1498Szrj       gcc_assert (!gimple_seq_last (*i->seq));
263*38fd1498Szrj       last->next = NULL;
264*38fd1498Szrj       gimple_seq_set_first (i->seq, first);
265*38fd1498Szrj       gimple_seq_set_last (i->seq, last);
266*38fd1498Szrj     }
267*38fd1498Szrj 
268*38fd1498Szrj   /* Update the iterator, if requested.  */
269*38fd1498Szrj   switch (m)
270*38fd1498Szrj     {
271*38fd1498Szrj     case GSI_NEW_STMT:
272*38fd1498Szrj       i->ptr = first;
273*38fd1498Szrj       break;
274*38fd1498Szrj     case GSI_CONTINUE_LINKING:
275*38fd1498Szrj       i->ptr = last;
276*38fd1498Szrj       break;
277*38fd1498Szrj     case GSI_SAME_STMT:
278*38fd1498Szrj       gcc_assert (cur);
279*38fd1498Szrj       break;
280*38fd1498Szrj     default:
281*38fd1498Szrj       gcc_unreachable ();
282*38fd1498Szrj     }
283*38fd1498Szrj }
284*38fd1498Szrj 
285*38fd1498Szrj 
286*38fd1498Szrj /* Links sequence SEQ after the statement pointed-to by iterator I.
287*38fd1498Szrj    MODE is as in gsi_insert_after.
288*38fd1498Szrj 
289*38fd1498Szrj    This function does not scan for new operands.  It is provided for
290*38fd1498Szrj    the use of the gimplifier, which manipulates statements for which
291*38fd1498Szrj    def/use information has not yet been constructed.  Most callers
292*38fd1498Szrj    should use gsi_insert_seq_after.  */
293*38fd1498Szrj 
294*38fd1498Szrj void
gsi_insert_seq_after_without_update(gimple_stmt_iterator * i,gimple_seq seq,enum gsi_iterator_update mode)295*38fd1498Szrj gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
296*38fd1498Szrj                                      enum gsi_iterator_update mode)
297*38fd1498Szrj {
298*38fd1498Szrj   gimple_seq_node first, last;
299*38fd1498Szrj 
300*38fd1498Szrj   if (seq == NULL)
301*38fd1498Szrj     return;
302*38fd1498Szrj 
303*38fd1498Szrj   /* Don't allow inserting a sequence into itself.  */
304*38fd1498Szrj   gcc_assert (seq != *i->seq);
305*38fd1498Szrj 
306*38fd1498Szrj   first = gimple_seq_first (seq);
307*38fd1498Szrj   last = gimple_seq_last (seq);
308*38fd1498Szrj 
309*38fd1498Szrj   /* Empty sequences need no work.  */
310*38fd1498Szrj   if (!first || !last)
311*38fd1498Szrj     {
312*38fd1498Szrj       gcc_assert (first == last);
313*38fd1498Szrj       return;
314*38fd1498Szrj     }
315*38fd1498Szrj 
316*38fd1498Szrj   gsi_insert_seq_nodes_after (i, first, last, mode);
317*38fd1498Szrj }
318*38fd1498Szrj 
319*38fd1498Szrj 
320*38fd1498Szrj /* Links sequence SEQ after the statement pointed-to by iterator I.
321*38fd1498Szrj    MODE is as in gsi_insert_after.  Scan the statements in SEQ
322*38fd1498Szrj    for new operands.  */
323*38fd1498Szrj 
324*38fd1498Szrj void
gsi_insert_seq_after(gimple_stmt_iterator * i,gimple_seq seq,enum gsi_iterator_update mode)325*38fd1498Szrj gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
326*38fd1498Szrj 		      enum gsi_iterator_update mode)
327*38fd1498Szrj {
328*38fd1498Szrj   update_modified_stmts (seq);
329*38fd1498Szrj   gsi_insert_seq_after_without_update (i, seq, mode);
330*38fd1498Szrj }
331*38fd1498Szrj 
332*38fd1498Szrj 
333*38fd1498Szrj /* Move all statements in the sequence after I to a new sequence.
334*38fd1498Szrj    Return this new sequence.  */
335*38fd1498Szrj 
336*38fd1498Szrj gimple_seq
gsi_split_seq_after(gimple_stmt_iterator i)337*38fd1498Szrj gsi_split_seq_after (gimple_stmt_iterator i)
338*38fd1498Szrj {
339*38fd1498Szrj   gimple_seq_node cur, next;
340*38fd1498Szrj   gimple_seq *pold_seq, new_seq;
341*38fd1498Szrj 
342*38fd1498Szrj   cur = i.ptr;
343*38fd1498Szrj 
344*38fd1498Szrj   /* How can we possibly split after the end, or before the beginning?  */
345*38fd1498Szrj   gcc_assert (cur && cur->next);
346*38fd1498Szrj   next = cur->next;
347*38fd1498Szrj 
348*38fd1498Szrj   pold_seq = i.seq;
349*38fd1498Szrj 
350*38fd1498Szrj   gimple_seq_set_first (&new_seq, next);
351*38fd1498Szrj   gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
352*38fd1498Szrj   gimple_seq_set_last (pold_seq, cur);
353*38fd1498Szrj   cur->next = NULL;
354*38fd1498Szrj 
355*38fd1498Szrj   return new_seq;
356*38fd1498Szrj }
357*38fd1498Szrj 
358*38fd1498Szrj 
359*38fd1498Szrj /* Set the statement to which GSI points to STMT.  This only updates
360*38fd1498Szrj    the iterator and the gimple sequence, it doesn't do the bookkeeping
361*38fd1498Szrj    of gsi_replace.  */
362*38fd1498Szrj 
363*38fd1498Szrj void
gsi_set_stmt(gimple_stmt_iterator * gsi,gimple * stmt)364*38fd1498Szrj gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt)
365*38fd1498Szrj {
366*38fd1498Szrj   gimple *orig_stmt = gsi_stmt (*gsi);
367*38fd1498Szrj   gimple *prev, *next;
368*38fd1498Szrj 
369*38fd1498Szrj   stmt->next = next = orig_stmt->next;
370*38fd1498Szrj   stmt->prev = prev = orig_stmt->prev;
371*38fd1498Szrj   /* Note how we don't clear next/prev of orig_stmt.  This is so that
372*38fd1498Szrj      copies of *GSI our callers might still hold (to orig_stmt)
373*38fd1498Szrj      can be advanced as if they too were replaced.  */
374*38fd1498Szrj   if (prev->next)
375*38fd1498Szrj     prev->next = stmt;
376*38fd1498Szrj   else
377*38fd1498Szrj     gimple_seq_set_first (gsi->seq, stmt);
378*38fd1498Szrj   if (next)
379*38fd1498Szrj     next->prev = stmt;
380*38fd1498Szrj   else
381*38fd1498Szrj     gimple_seq_set_last (gsi->seq, stmt);
382*38fd1498Szrj 
383*38fd1498Szrj   gsi->ptr = stmt;
384*38fd1498Szrj }
385*38fd1498Szrj 
386*38fd1498Szrj 
387*38fd1498Szrj /* Move all statements in the sequence before I to a new sequence.
388*38fd1498Szrj    Return this new sequence.  I is set to the head of the new list.  */
389*38fd1498Szrj 
390*38fd1498Szrj void
gsi_split_seq_before(gimple_stmt_iterator * i,gimple_seq * pnew_seq)391*38fd1498Szrj gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq)
392*38fd1498Szrj {
393*38fd1498Szrj   gimple_seq_node cur, prev;
394*38fd1498Szrj   gimple_seq old_seq;
395*38fd1498Szrj 
396*38fd1498Szrj   cur = i->ptr;
397*38fd1498Szrj 
398*38fd1498Szrj   /* How can we possibly split after the end?  */
399*38fd1498Szrj   gcc_assert (cur);
400*38fd1498Szrj   prev = cur->prev;
401*38fd1498Szrj 
402*38fd1498Szrj   old_seq = *i->seq;
403*38fd1498Szrj   if (!prev->next)
404*38fd1498Szrj     *i->seq = NULL;
405*38fd1498Szrj   i->seq = pnew_seq;
406*38fd1498Szrj 
407*38fd1498Szrj   /* Set the limits on NEW_SEQ.  */
408*38fd1498Szrj   gimple_seq_set_first (pnew_seq, cur);
409*38fd1498Szrj   gimple_seq_set_last (pnew_seq, gimple_seq_last (old_seq));
410*38fd1498Szrj 
411*38fd1498Szrj   /* Cut OLD_SEQ before I.  */
412*38fd1498Szrj   gimple_seq_set_last (&old_seq, prev);
413*38fd1498Szrj   if (prev->next)
414*38fd1498Szrj     prev->next = NULL;
415*38fd1498Szrj }
416*38fd1498Szrj 
417*38fd1498Szrj 
418*38fd1498Szrj /* Replace the statement pointed-to by GSI to STMT.  If UPDATE_EH_INFO
419*38fd1498Szrj    is true, the exception handling information of the original
420*38fd1498Szrj    statement is moved to the new statement.  Assignments must only be
421*38fd1498Szrj    replaced with assignments to the same LHS.  Returns whether EH edge
422*38fd1498Szrj    cleanup is required.  */
423*38fd1498Szrj 
424*38fd1498Szrj bool
gsi_replace(gimple_stmt_iterator * gsi,gimple * stmt,bool update_eh_info)425*38fd1498Szrj gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info)
426*38fd1498Szrj {
427*38fd1498Szrj   gimple *orig_stmt = gsi_stmt (*gsi);
428*38fd1498Szrj   bool require_eh_edge_purge = false;
429*38fd1498Szrj 
430*38fd1498Szrj   if (stmt == orig_stmt)
431*38fd1498Szrj     return false;
432*38fd1498Szrj 
433*38fd1498Szrj   gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
434*38fd1498Szrj 	      || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
435*38fd1498Szrj 
436*38fd1498Szrj   gimple_set_location (stmt, gimple_location (orig_stmt));
437*38fd1498Szrj   gimple_set_bb (stmt, gsi_bb (*gsi));
438*38fd1498Szrj 
439*38fd1498Szrj   /* Preserve EH region information from the original statement, if
440*38fd1498Szrj      requested by the caller.  */
441*38fd1498Szrj   if (update_eh_info)
442*38fd1498Szrj     require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
443*38fd1498Szrj 
444*38fd1498Szrj   gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
445*38fd1498Szrj 
446*38fd1498Szrj   /* Free all the data flow information for ORIG_STMT.  */
447*38fd1498Szrj   gimple_set_bb (orig_stmt, NULL);
448*38fd1498Szrj   gimple_remove_stmt_histograms (cfun, orig_stmt);
449*38fd1498Szrj   delink_stmt_imm_use (orig_stmt);
450*38fd1498Szrj 
451*38fd1498Szrj   gsi_set_stmt (gsi, stmt);
452*38fd1498Szrj   gimple_set_modified (stmt, true);
453*38fd1498Szrj   update_modified_stmt (stmt);
454*38fd1498Szrj   return require_eh_edge_purge;
455*38fd1498Szrj }
456*38fd1498Szrj 
457*38fd1498Szrj 
458*38fd1498Szrj /* Replace the statement pointed-to by GSI with the sequence SEQ.
459*38fd1498Szrj    If UPDATE_EH_INFO is true, the exception handling information of
460*38fd1498Szrj    the original statement is moved to the last statement of the new
461*38fd1498Szrj    sequence.  If the old statement is an assignment, then so must
462*38fd1498Szrj    be the last statement of the new sequence, and they must have the
463*38fd1498Szrj    same LHS.  */
464*38fd1498Szrj 
465*38fd1498Szrj void
gsi_replace_with_seq(gimple_stmt_iterator * gsi,gimple_seq seq,bool update_eh_info)466*38fd1498Szrj gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq,
467*38fd1498Szrj 		      bool update_eh_info)
468*38fd1498Szrj {
469*38fd1498Szrj   gimple_stmt_iterator seqi;
470*38fd1498Szrj   gimple *last;
471*38fd1498Szrj   if (gimple_seq_empty_p (seq))
472*38fd1498Szrj     {
473*38fd1498Szrj       gsi_remove (gsi, true);
474*38fd1498Szrj       return;
475*38fd1498Szrj     }
476*38fd1498Szrj   seqi = gsi_last (seq);
477*38fd1498Szrj   last = gsi_stmt (seqi);
478*38fd1498Szrj   gsi_remove (&seqi, false);
479*38fd1498Szrj   gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
480*38fd1498Szrj   gsi_replace (gsi, last, update_eh_info);
481*38fd1498Szrj }
482*38fd1498Szrj 
483*38fd1498Szrj 
484*38fd1498Szrj /* Insert statement STMT before the statement pointed-to by iterator I.
485*38fd1498Szrj    M specifies how to update iterator I after insertion (see enum
486*38fd1498Szrj    gsi_iterator_update).
487*38fd1498Szrj 
488*38fd1498Szrj    This function does not scan for new operands.  It is provided for
489*38fd1498Szrj    the use of the gimplifier, which manipulates statements for which
490*38fd1498Szrj    def/use information has not yet been constructed.  Most callers
491*38fd1498Szrj    should use gsi_insert_before.  */
492*38fd1498Szrj 
493*38fd1498Szrj void
gsi_insert_before_without_update(gimple_stmt_iterator * i,gimple * stmt,enum gsi_iterator_update m)494*38fd1498Szrj gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt,
495*38fd1498Szrj                                   enum gsi_iterator_update m)
496*38fd1498Szrj {
497*38fd1498Szrj   gsi_insert_seq_nodes_before (i, stmt, stmt, m);
498*38fd1498Szrj }
499*38fd1498Szrj 
500*38fd1498Szrj /* Insert statement STMT before the statement pointed-to by iterator I.
501*38fd1498Szrj    Update STMT's basic block and scan it for new operands.  M
502*38fd1498Szrj    specifies how to update iterator I after insertion (see enum
503*38fd1498Szrj    gsi_iterator_update).  */
504*38fd1498Szrj 
505*38fd1498Szrj void
gsi_insert_before(gimple_stmt_iterator * i,gimple * stmt,enum gsi_iterator_update m)506*38fd1498Szrj gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt,
507*38fd1498Szrj                    enum gsi_iterator_update m)
508*38fd1498Szrj {
509*38fd1498Szrj   update_modified_stmt (stmt);
510*38fd1498Szrj   gsi_insert_before_without_update (i, stmt, m);
511*38fd1498Szrj }
512*38fd1498Szrj 
513*38fd1498Szrj 
514*38fd1498Szrj /* Insert statement STMT after the statement pointed-to by iterator I.
515*38fd1498Szrj    M specifies how to update iterator I after insertion (see enum
516*38fd1498Szrj    gsi_iterator_update).
517*38fd1498Szrj 
518*38fd1498Szrj    This function does not scan for new operands.  It is provided for
519*38fd1498Szrj    the use of the gimplifier, which manipulates statements for which
520*38fd1498Szrj    def/use information has not yet been constructed.  Most callers
521*38fd1498Szrj    should use gsi_insert_after.  */
522*38fd1498Szrj 
523*38fd1498Szrj void
gsi_insert_after_without_update(gimple_stmt_iterator * i,gimple * stmt,enum gsi_iterator_update m)524*38fd1498Szrj gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt,
525*38fd1498Szrj                                  enum gsi_iterator_update m)
526*38fd1498Szrj {
527*38fd1498Szrj   gsi_insert_seq_nodes_after (i, stmt, stmt, m);
528*38fd1498Szrj }
529*38fd1498Szrj 
530*38fd1498Szrj 
531*38fd1498Szrj /* Insert statement STMT after the statement pointed-to by iterator I.
532*38fd1498Szrj    Update STMT's basic block and scan it for new operands.  M
533*38fd1498Szrj    specifies how to update iterator I after insertion (see enum
534*38fd1498Szrj    gsi_iterator_update).  */
535*38fd1498Szrj 
536*38fd1498Szrj void
gsi_insert_after(gimple_stmt_iterator * i,gimple * stmt,enum gsi_iterator_update m)537*38fd1498Szrj gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt,
538*38fd1498Szrj 		  enum gsi_iterator_update m)
539*38fd1498Szrj {
540*38fd1498Szrj   update_modified_stmt (stmt);
541*38fd1498Szrj   gsi_insert_after_without_update (i, stmt, m);
542*38fd1498Szrj }
543*38fd1498Szrj 
544*38fd1498Szrj 
545*38fd1498Szrj /* Remove the current stmt from the sequence.  The iterator is updated
546*38fd1498Szrj    to point to the next statement.
547*38fd1498Szrj 
548*38fd1498Szrj    REMOVE_PERMANENTLY is true when the statement is going to be removed
549*38fd1498Szrj    from the IL and not reinserted elsewhere.  In that case we remove the
550*38fd1498Szrj    statement pointed to by iterator I from the EH tables, and free its
551*38fd1498Szrj    operand caches.  Otherwise we do not modify this information.  Returns
552*38fd1498Szrj    true whether EH edge cleanup is required.  */
553*38fd1498Szrj 
554*38fd1498Szrj bool
gsi_remove(gimple_stmt_iterator * i,bool remove_permanently)555*38fd1498Szrj gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
556*38fd1498Szrj {
557*38fd1498Szrj   gimple_seq_node cur, next, prev;
558*38fd1498Szrj   gimple *stmt = gsi_stmt (*i);
559*38fd1498Szrj   bool require_eh_edge_purge = false;
560*38fd1498Szrj 
561*38fd1498Szrj   if (gimple_code (stmt) != GIMPLE_PHI)
562*38fd1498Szrj     insert_debug_temps_for_defs (i);
563*38fd1498Szrj 
564*38fd1498Szrj   /* Free all the data flow information for STMT.  */
565*38fd1498Szrj   gimple_set_bb (stmt, NULL);
566*38fd1498Szrj   delink_stmt_imm_use (stmt);
567*38fd1498Szrj   gimple_set_modified (stmt, true);
568*38fd1498Szrj 
569*38fd1498Szrj   if (remove_permanently)
570*38fd1498Szrj     {
571*38fd1498Szrj       if (gimple_debug_nonbind_marker_p (stmt))
572*38fd1498Szrj 	/* We don't need this to be exact, but try to keep it at least
573*38fd1498Szrj 	   close.  */
574*38fd1498Szrj 	cfun->debug_marker_count--;
575*38fd1498Szrj       require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
576*38fd1498Szrj       gimple_remove_stmt_histograms (cfun, stmt);
577*38fd1498Szrj     }
578*38fd1498Szrj 
579*38fd1498Szrj   /* Update the iterator and re-wire the links in I->SEQ.  */
580*38fd1498Szrj   cur = i->ptr;
581*38fd1498Szrj   next = cur->next;
582*38fd1498Szrj   prev = cur->prev;
583*38fd1498Szrj   /* See gsi_set_stmt for why we don't reset prev/next of STMT.  */
584*38fd1498Szrj 
585*38fd1498Szrj   if (next)
586*38fd1498Szrj     /* Cur is not last.  */
587*38fd1498Szrj     next->prev = prev;
588*38fd1498Szrj   else if (prev->next)
589*38fd1498Szrj     /* Cur is last but not first.  */
590*38fd1498Szrj     gimple_seq_set_last (i->seq, prev);
591*38fd1498Szrj 
592*38fd1498Szrj   if (prev->next)
593*38fd1498Szrj     /* Cur is not first.  */
594*38fd1498Szrj     prev->next = next;
595*38fd1498Szrj   else
596*38fd1498Szrj     /* Cur is first.  */
597*38fd1498Szrj     *i->seq = next;
598*38fd1498Szrj 
599*38fd1498Szrj   i->ptr = next;
600*38fd1498Szrj 
601*38fd1498Szrj   return require_eh_edge_purge;
602*38fd1498Szrj }
603*38fd1498Szrj 
604*38fd1498Szrj 
605*38fd1498Szrj /* Finds iterator for STMT.  */
606*38fd1498Szrj 
607*38fd1498Szrj gimple_stmt_iterator
gsi_for_stmt(gimple * stmt)608*38fd1498Szrj gsi_for_stmt (gimple *stmt)
609*38fd1498Szrj {
610*38fd1498Szrj   gimple_stmt_iterator i;
611*38fd1498Szrj   basic_block bb = gimple_bb (stmt);
612*38fd1498Szrj 
613*38fd1498Szrj   if (gimple_code (stmt) == GIMPLE_PHI)
614*38fd1498Szrj     i = gsi_start_phis (bb);
615*38fd1498Szrj   else
616*38fd1498Szrj     i = gsi_start_bb (bb);
617*38fd1498Szrj 
618*38fd1498Szrj   i.ptr = stmt;
619*38fd1498Szrj   return i;
620*38fd1498Szrj }
621*38fd1498Szrj 
622*38fd1498Szrj /* Finds iterator for PHI.  */
623*38fd1498Szrj 
624*38fd1498Szrj gphi_iterator
gsi_for_phi(gphi * phi)625*38fd1498Szrj gsi_for_phi (gphi *phi)
626*38fd1498Szrj {
627*38fd1498Szrj   gphi_iterator i;
628*38fd1498Szrj   basic_block bb = gimple_bb (phi);
629*38fd1498Szrj 
630*38fd1498Szrj   i = gsi_start_phis (bb);
631*38fd1498Szrj   i.ptr = phi;
632*38fd1498Szrj 
633*38fd1498Szrj   return i;
634*38fd1498Szrj }
635*38fd1498Szrj 
636*38fd1498Szrj /* Move the statement at FROM so it comes right after the statement at TO.  */
637*38fd1498Szrj 
638*38fd1498Szrj void
gsi_move_after(gimple_stmt_iterator * from,gimple_stmt_iterator * to)639*38fd1498Szrj gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
640*38fd1498Szrj {
641*38fd1498Szrj   gimple *stmt = gsi_stmt (*from);
642*38fd1498Szrj   gsi_remove (from, false);
643*38fd1498Szrj 
644*38fd1498Szrj   /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
645*38fd1498Szrj      move statements to an empty block.  */
646*38fd1498Szrj   gsi_insert_after (to, stmt, GSI_NEW_STMT);
647*38fd1498Szrj }
648*38fd1498Szrj 
649*38fd1498Szrj 
650*38fd1498Szrj /* Move the statement at FROM so it comes right before the statement
651*38fd1498Szrj    at TO.  */
652*38fd1498Szrj 
653*38fd1498Szrj void
gsi_move_before(gimple_stmt_iterator * from,gimple_stmt_iterator * to)654*38fd1498Szrj gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
655*38fd1498Szrj {
656*38fd1498Szrj   gimple *stmt = gsi_stmt (*from);
657*38fd1498Szrj   gsi_remove (from, false);
658*38fd1498Szrj 
659*38fd1498Szrj   /* For consistency with gsi_move_after, it might be better to have
660*38fd1498Szrj      GSI_NEW_STMT here; however, that breaks several places that expect
661*38fd1498Szrj      that TO does not change.  */
662*38fd1498Szrj   gsi_insert_before (to, stmt, GSI_SAME_STMT);
663*38fd1498Szrj }
664*38fd1498Szrj 
665*38fd1498Szrj 
666*38fd1498Szrj /* Move the statement at FROM to the end of basic block BB.  */
667*38fd1498Szrj 
668*38fd1498Szrj void
gsi_move_to_bb_end(gimple_stmt_iterator * from,basic_block bb)669*38fd1498Szrj gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
670*38fd1498Szrj {
671*38fd1498Szrj   gimple_stmt_iterator last = gsi_last_bb (bb);
672*38fd1498Szrj   gcc_checking_assert (gsi_bb (last) == bb);
673*38fd1498Szrj 
674*38fd1498Szrj   /* Have to check gsi_end_p because it could be an empty block.  */
675*38fd1498Szrj   if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
676*38fd1498Szrj     gsi_move_before (from, &last);
677*38fd1498Szrj   else
678*38fd1498Szrj     gsi_move_after (from, &last);
679*38fd1498Szrj }
680*38fd1498Szrj 
681*38fd1498Szrj 
682*38fd1498Szrj /* Add STMT to the pending list of edge E.  No actual insertion is
683*38fd1498Szrj    made until a call to gsi_commit_edge_inserts () is made.  */
684*38fd1498Szrj 
685*38fd1498Szrj void
gsi_insert_on_edge(edge e,gimple * stmt)686*38fd1498Szrj gsi_insert_on_edge (edge e, gimple *stmt)
687*38fd1498Szrj {
688*38fd1498Szrj   gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
689*38fd1498Szrj }
690*38fd1498Szrj 
691*38fd1498Szrj /* Add the sequence of statements SEQ to the pending list of edge E.
692*38fd1498Szrj    No actual insertion is made until a call to gsi_commit_edge_inserts
693*38fd1498Szrj    is made.  */
694*38fd1498Szrj 
695*38fd1498Szrj void
gsi_insert_seq_on_edge(edge e,gimple_seq seq)696*38fd1498Szrj gsi_insert_seq_on_edge (edge e, gimple_seq seq)
697*38fd1498Szrj {
698*38fd1498Szrj   gimple_seq_add_seq (&PENDING_STMT (e), seq);
699*38fd1498Szrj }
700*38fd1498Szrj 
701*38fd1498Szrj /* Return a new iterator pointing to the first statement in sequence of
702*38fd1498Szrj    statements on edge E.  Such statements need to be subsequently moved into a
703*38fd1498Szrj    basic block by calling gsi_commit_edge_inserts.  */
704*38fd1498Szrj 
705*38fd1498Szrj gimple_stmt_iterator
gsi_start_edge(edge e)706*38fd1498Szrj gsi_start_edge (edge e)
707*38fd1498Szrj {
708*38fd1498Szrj   return gsi_start (PENDING_STMT (e));
709*38fd1498Szrj }
710*38fd1498Szrj 
711*38fd1498Szrj /* Insert the statement pointed-to by GSI into edge E.  Every attempt
712*38fd1498Szrj    is made to place the statement in an existing basic block, but
713*38fd1498Szrj    sometimes that isn't possible.  When it isn't possible, the edge is
714*38fd1498Szrj    split and the statement is added to the new block.
715*38fd1498Szrj 
716*38fd1498Szrj    In all cases, the returned *GSI points to the correct location.  The
717*38fd1498Szrj    return value is true if insertion should be done after the location,
718*38fd1498Szrj    or false if it should be done before the location.  If a new basic block
719*38fd1498Szrj    has to be created, it is stored in *NEW_BB.  */
720*38fd1498Szrj 
721*38fd1498Szrj static bool
gimple_find_edge_insert_loc(edge e,gimple_stmt_iterator * gsi,basic_block * new_bb)722*38fd1498Szrj gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
723*38fd1498Szrj 			     basic_block *new_bb)
724*38fd1498Szrj {
725*38fd1498Szrj   basic_block dest, src;
726*38fd1498Szrj   gimple *tmp;
727*38fd1498Szrj 
728*38fd1498Szrj   dest = e->dest;
729*38fd1498Szrj 
730*38fd1498Szrj   /* If the destination has one predecessor which has no PHI nodes,
731*38fd1498Szrj      insert there.  Except for the exit block.
732*38fd1498Szrj 
733*38fd1498Szrj      The requirement for no PHI nodes could be relaxed.  Basically we
734*38fd1498Szrj      would have to examine the PHIs to prove that none of them used
735*38fd1498Szrj      the value set by the statement we want to insert on E.  That
736*38fd1498Szrj      hardly seems worth the effort.  */
737*38fd1498Szrj  restart:
738*38fd1498Szrj   if (single_pred_p (dest)
739*38fd1498Szrj       && gimple_seq_empty_p (phi_nodes (dest))
740*38fd1498Szrj       && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
741*38fd1498Szrj     {
742*38fd1498Szrj       *gsi = gsi_start_bb (dest);
743*38fd1498Szrj       if (gsi_end_p (*gsi))
744*38fd1498Szrj 	return true;
745*38fd1498Szrj 
746*38fd1498Szrj       /* Make sure we insert after any leading labels.  */
747*38fd1498Szrj       tmp = gsi_stmt (*gsi);
748*38fd1498Szrj       while (gimple_code (tmp) == GIMPLE_LABEL)
749*38fd1498Szrj 	{
750*38fd1498Szrj 	  gsi_next (gsi);
751*38fd1498Szrj 	  if (gsi_end_p (*gsi))
752*38fd1498Szrj 	    break;
753*38fd1498Szrj 	  tmp = gsi_stmt (*gsi);
754*38fd1498Szrj 	}
755*38fd1498Szrj 
756*38fd1498Szrj       if (gsi_end_p (*gsi))
757*38fd1498Szrj 	{
758*38fd1498Szrj 	  *gsi = gsi_last_bb (dest);
759*38fd1498Szrj 	  return true;
760*38fd1498Szrj 	}
761*38fd1498Szrj       else
762*38fd1498Szrj 	return false;
763*38fd1498Szrj     }
764*38fd1498Szrj 
765*38fd1498Szrj   /* If the source has one successor, the edge is not abnormal and
766*38fd1498Szrj      the last statement does not end a basic block, insert there.
767*38fd1498Szrj      Except for the entry block.  */
768*38fd1498Szrj   src = e->src;
769*38fd1498Szrj   if ((e->flags & EDGE_ABNORMAL) == 0
770*38fd1498Szrj       && (single_succ_p (src)
771*38fd1498Szrj 	  /* Do not count a fake edge as successor as added to infinite
772*38fd1498Szrj 	     loops by connect_infinite_loops_to_exit.  */
773*38fd1498Szrj 	  || (EDGE_COUNT (src->succs) == 2
774*38fd1498Szrj 	      && (EDGE_SUCC (src, 0)->flags & EDGE_FAKE
775*38fd1498Szrj 		  || EDGE_SUCC (src, 1)->flags & EDGE_FAKE)))
776*38fd1498Szrj       && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
777*38fd1498Szrj     {
778*38fd1498Szrj       *gsi = gsi_last_bb (src);
779*38fd1498Szrj       if (gsi_end_p (*gsi))
780*38fd1498Szrj 	return true;
781*38fd1498Szrj 
782*38fd1498Szrj       tmp = gsi_stmt (*gsi);
783*38fd1498Szrj       if (is_gimple_debug (tmp))
784*38fd1498Szrj 	{
785*38fd1498Szrj 	  gimple_stmt_iterator si = *gsi;
786*38fd1498Szrj 	  gsi_prev_nondebug (&si);
787*38fd1498Szrj 	  if (!gsi_end_p (si))
788*38fd1498Szrj 	    tmp = gsi_stmt (si);
789*38fd1498Szrj 	  /* If we don't have a BB-ending nondebug stmt, we want to
790*38fd1498Szrj 	     insert after the trailing debug stmts.  Otherwise, we may
791*38fd1498Szrj 	     insert before the BB-ending nondebug stmt, or split the
792*38fd1498Szrj 	     edge.  */
793*38fd1498Szrj 	  if (!stmt_ends_bb_p (tmp))
794*38fd1498Szrj 	    return true;
795*38fd1498Szrj 	  *gsi = si;
796*38fd1498Szrj 	}
797*38fd1498Szrj       else if (!stmt_ends_bb_p (tmp))
798*38fd1498Szrj 	return true;
799*38fd1498Szrj 
800*38fd1498Szrj       switch (gimple_code (tmp))
801*38fd1498Szrj 	{
802*38fd1498Szrj 	case GIMPLE_RETURN:
803*38fd1498Szrj 	case GIMPLE_RESX:
804*38fd1498Szrj 	  return false;
805*38fd1498Szrj 	default:
806*38fd1498Szrj 	  break;
807*38fd1498Szrj         }
808*38fd1498Szrj     }
809*38fd1498Szrj 
810*38fd1498Szrj   /* Otherwise, create a new basic block, and split this edge.  */
811*38fd1498Szrj   dest = split_edge (e);
812*38fd1498Szrj   if (new_bb)
813*38fd1498Szrj     *new_bb = dest;
814*38fd1498Szrj   e = single_pred_edge (dest);
815*38fd1498Szrj   goto restart;
816*38fd1498Szrj }
817*38fd1498Szrj 
818*38fd1498Szrj 
819*38fd1498Szrj /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts.  If a new
820*38fd1498Szrj    block has to be created, it is returned.  */
821*38fd1498Szrj 
822*38fd1498Szrj basic_block
gsi_insert_on_edge_immediate(edge e,gimple * stmt)823*38fd1498Szrj gsi_insert_on_edge_immediate (edge e, gimple *stmt)
824*38fd1498Szrj {
825*38fd1498Szrj   gimple_stmt_iterator gsi;
826*38fd1498Szrj   basic_block new_bb = NULL;
827*38fd1498Szrj   bool ins_after;
828*38fd1498Szrj 
829*38fd1498Szrj   gcc_assert (!PENDING_STMT (e));
830*38fd1498Szrj 
831*38fd1498Szrj   ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
832*38fd1498Szrj 
833*38fd1498Szrj   update_call_edge_frequencies (stmt, gsi.bb);
834*38fd1498Szrj 
835*38fd1498Szrj   if (ins_after)
836*38fd1498Szrj     gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
837*38fd1498Szrj   else
838*38fd1498Szrj     gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
839*38fd1498Szrj 
840*38fd1498Szrj   return new_bb;
841*38fd1498Szrj }
842*38fd1498Szrj 
843*38fd1498Szrj /* Insert STMTS on edge E.  If a new block has to be created, it
844*38fd1498Szrj    is returned.  */
845*38fd1498Szrj 
846*38fd1498Szrj basic_block
gsi_insert_seq_on_edge_immediate(edge e,gimple_seq stmts)847*38fd1498Szrj gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
848*38fd1498Szrj {
849*38fd1498Szrj   gimple_stmt_iterator gsi;
850*38fd1498Szrj   basic_block new_bb = NULL;
851*38fd1498Szrj   bool ins_after;
852*38fd1498Szrj 
853*38fd1498Szrj   gcc_assert (!PENDING_STMT (e));
854*38fd1498Szrj 
855*38fd1498Szrj   ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
856*38fd1498Szrj   update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
857*38fd1498Szrj 
858*38fd1498Szrj   if (ins_after)
859*38fd1498Szrj     gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
860*38fd1498Szrj   else
861*38fd1498Szrj     gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
862*38fd1498Szrj 
863*38fd1498Szrj   return new_bb;
864*38fd1498Szrj }
865*38fd1498Szrj 
866*38fd1498Szrj /* This routine will commit all pending edge insertions, creating any new
867*38fd1498Szrj    basic blocks which are necessary.  */
868*38fd1498Szrj 
869*38fd1498Szrj void
gsi_commit_edge_inserts(void)870*38fd1498Szrj gsi_commit_edge_inserts (void)
871*38fd1498Szrj {
872*38fd1498Szrj   basic_block bb;
873*38fd1498Szrj   edge e;
874*38fd1498Szrj   edge_iterator ei;
875*38fd1498Szrj 
876*38fd1498Szrj   gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
877*38fd1498Szrj 			      NULL);
878*38fd1498Szrj 
879*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
880*38fd1498Szrj     FOR_EACH_EDGE (e, ei, bb->succs)
881*38fd1498Szrj       gsi_commit_one_edge_insert (e, NULL);
882*38fd1498Szrj }
883*38fd1498Szrj 
884*38fd1498Szrj 
885*38fd1498Szrj /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
886*38fd1498Szrj    to this block, otherwise set it to NULL.  */
887*38fd1498Szrj 
888*38fd1498Szrj void
gsi_commit_one_edge_insert(edge e,basic_block * new_bb)889*38fd1498Szrj gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
890*38fd1498Szrj {
891*38fd1498Szrj   if (new_bb)
892*38fd1498Szrj     *new_bb = NULL;
893*38fd1498Szrj 
894*38fd1498Szrj   if (PENDING_STMT (e))
895*38fd1498Szrj     {
896*38fd1498Szrj       gimple_stmt_iterator gsi;
897*38fd1498Szrj       gimple_seq seq = PENDING_STMT (e);
898*38fd1498Szrj       bool ins_after;
899*38fd1498Szrj 
900*38fd1498Szrj       PENDING_STMT (e) = NULL;
901*38fd1498Szrj 
902*38fd1498Szrj       ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
903*38fd1498Szrj       update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
904*38fd1498Szrj 
905*38fd1498Szrj       if (ins_after)
906*38fd1498Szrj 	gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
907*38fd1498Szrj       else
908*38fd1498Szrj 	gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
909*38fd1498Szrj     }
910*38fd1498Szrj }
911*38fd1498Szrj 
912*38fd1498Szrj /* Returns iterator at the start of the list of phi nodes of BB.  */
913*38fd1498Szrj 
914*38fd1498Szrj gphi_iterator
gsi_start_phis(basic_block bb)915*38fd1498Szrj gsi_start_phis (basic_block bb)
916*38fd1498Szrj {
917*38fd1498Szrj   gimple_seq *pseq = phi_nodes_ptr (bb);
918*38fd1498Szrj 
919*38fd1498Szrj   /* Adapted from gsi_start_1. */
920*38fd1498Szrj   gphi_iterator i;
921*38fd1498Szrj 
922*38fd1498Szrj   i.ptr = gimple_seq_first (*pseq);
923*38fd1498Szrj   i.seq = pseq;
924*38fd1498Szrj   i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
925*38fd1498Szrj 
926*38fd1498Szrj   return i;
927*38fd1498Szrj }
928