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