gcc/
[official-gcc.git] / gcc / gimple-iterator.c
blobb51c3c93be52323d73bc824609aa5d5609b591e8
1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2015 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldy@quesejoda.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "predict.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "tree-eh.h"
38 #include "gimple-expr.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
42 #include "plugin-api.h"
43 #include "ipa-ref.h"
44 #include "cgraph.h"
45 #include "tree-cfg.h"
46 #include "tree-phinodes.h"
47 #include "ssa-iterators.h"
48 #include "tree-ssa.h"
49 #include "value-prof.h"
52 /* Mark the statement STMT as modified, and update it. */
54 static inline void
55 update_modified_stmt (gimple stmt)
57 if (!ssa_operands_active (cfun))
58 return;
59 update_stmt_if_modified (stmt);
63 /* Mark the statements in SEQ as modified, and update them. */
65 void
66 update_modified_stmts (gimple_seq seq)
68 gimple_stmt_iterator gsi;
70 if (!ssa_operands_active (cfun))
71 return;
72 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
73 update_stmt_if_modified (gsi_stmt (gsi));
77 /* Set BB to be the basic block for all the statements in the list
78 starting at FIRST and LAST. */
80 static void
81 update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last,
82 basic_block bb)
84 gimple_seq_node n;
86 for (n = first; n; n = n->next)
88 gimple_set_bb (n, bb);
89 if (n == last)
90 break;
94 /* Set the frequencies for the cgraph_edges for each of the calls
95 starting at FIRST for their new position within BB. */
97 static void
98 update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
100 struct cgraph_node *cfun_node = NULL;
101 int bb_freq = 0;
102 gimple_seq_node n;
104 for (n = first; n ; n = n->next)
105 if (is_gimple_call (n))
107 struct cgraph_edge *e;
109 /* These function calls are expensive enough that we want
110 to avoid calling them if we never see any calls. */
111 if (cfun_node == NULL)
113 cfun_node = cgraph_node::get (current_function_decl);
114 bb_freq = (compute_call_stmt_bb_frequency
115 (current_function_decl, bb));
118 e = cfun_node->get_edge (n);
119 if (e != NULL)
120 e->frequency = bb_freq;
124 /* Insert the sequence delimited by nodes FIRST and LAST before
125 iterator I. M specifies how to update iterator I after insertion
126 (see enum gsi_iterator_update).
128 This routine assumes that there is a forward and backward path
129 between FIRST and LAST (i.e., they are linked in a doubly-linked
130 list). Additionally, if FIRST == LAST, this routine will properly
131 insert a single node. */
133 static void
134 gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
135 gimple_seq_node first,
136 gimple_seq_node last,
137 enum gsi_iterator_update mode)
139 basic_block bb;
140 gimple_seq_node cur = i->ptr;
142 gcc_assert (!cur || cur->prev);
144 if ((bb = gsi_bb (*i)) != NULL)
145 update_bb_for_stmts (first, last, bb);
147 /* Link SEQ before CUR in the sequence. */
148 if (cur)
150 first->prev = cur->prev;
151 if (first->prev->next)
152 first->prev->next = first;
153 else
154 gimple_seq_set_first (i->seq, first);
155 last->next = cur;
156 cur->prev = last;
158 else
160 gimple_seq_node itlast = gimple_seq_last (*i->seq);
162 /* If CUR is NULL, we link at the end of the sequence (this case happens
163 when gsi_after_labels is called for a basic block that contains only
164 labels, so it returns an iterator after the end of the block, and
165 we need to insert before it; it might be cleaner to add a flag to the
166 iterator saying whether we are at the start or end of the list). */
167 last->next = NULL;
168 if (itlast)
170 first->prev = itlast;
171 itlast->next = first;
173 else
174 gimple_seq_set_first (i->seq, first);
175 gimple_seq_set_last (i->seq, last);
178 /* Update the iterator, if requested. */
179 switch (mode)
181 case GSI_NEW_STMT:
182 case GSI_CONTINUE_LINKING:
183 i->ptr = first;
184 break;
185 case GSI_SAME_STMT:
186 break;
187 default:
188 gcc_unreachable ();
193 /* Inserts the sequence of statements SEQ before the statement pointed
194 by iterator I. MODE indicates what to do with the iterator after
195 insertion (see enum gsi_iterator_update).
197 This function does not scan for new operands. It is provided for
198 the use of the gimplifier, which manipulates statements for which
199 def/use information has not yet been constructed. Most callers
200 should use gsi_insert_seq_before. */
202 void
203 gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
204 enum gsi_iterator_update mode)
206 gimple_seq_node first, last;
208 if (seq == NULL)
209 return;
211 /* Don't allow inserting a sequence into itself. */
212 gcc_assert (seq != *i->seq);
214 first = gimple_seq_first (seq);
215 last = gimple_seq_last (seq);
217 /* Empty sequences need no work. */
218 if (!first || !last)
220 gcc_assert (first == last);
221 return;
224 gsi_insert_seq_nodes_before (i, first, last, mode);
228 /* Inserts the sequence of statements SEQ before the statement pointed
229 by iterator I. MODE indicates what to do with the iterator after
230 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
231 for new operands. */
233 void
234 gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
235 enum gsi_iterator_update mode)
237 update_modified_stmts (seq);
238 gsi_insert_seq_before_without_update (i, seq, mode);
242 /* Insert the sequence delimited by nodes FIRST and LAST after
243 iterator I. M specifies how to update iterator I after insertion
244 (see enum gsi_iterator_update).
246 This routine assumes that there is a forward and backward path
247 between FIRST and LAST (i.e., they are linked in a doubly-linked
248 list). Additionally, if FIRST == LAST, this routine will properly
249 insert a single node. */
251 static void
252 gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
253 gimple_seq_node first,
254 gimple_seq_node last,
255 enum gsi_iterator_update m)
257 basic_block bb;
258 gimple_seq_node cur = i->ptr;
260 gcc_assert (!cur || cur->prev);
262 /* If the iterator is inside a basic block, we need to update the
263 basic block information for all the nodes between FIRST and LAST. */
264 if ((bb = gsi_bb (*i)) != NULL)
265 update_bb_for_stmts (first, last, bb);
267 /* Link SEQ after CUR. */
268 if (cur)
270 last->next = cur->next;
271 if (last->next)
273 last->next->prev = last;
275 else
276 gimple_seq_set_last (i->seq, last);
277 first->prev = cur;
278 cur->next = first;
280 else
282 gcc_assert (!gimple_seq_last (*i->seq));
283 last->next = NULL;
284 gimple_seq_set_first (i->seq, first);
285 gimple_seq_set_last (i->seq, last);
288 /* Update the iterator, if requested. */
289 switch (m)
291 case GSI_NEW_STMT:
292 i->ptr = first;
293 break;
294 case GSI_CONTINUE_LINKING:
295 i->ptr = last;
296 break;
297 case GSI_SAME_STMT:
298 gcc_assert (cur);
299 break;
300 default:
301 gcc_unreachable ();
306 /* Links sequence SEQ after the statement pointed-to by iterator I.
307 MODE is as in gsi_insert_after.
309 This function does not scan for new operands. It is provided for
310 the use of the gimplifier, which manipulates statements for which
311 def/use information has not yet been constructed. Most callers
312 should use gsi_insert_seq_after. */
314 void
315 gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
316 enum gsi_iterator_update mode)
318 gimple_seq_node first, last;
320 if (seq == NULL)
321 return;
323 /* Don't allow inserting a sequence into itself. */
324 gcc_assert (seq != *i->seq);
326 first = gimple_seq_first (seq);
327 last = gimple_seq_last (seq);
329 /* Empty sequences need no work. */
330 if (!first || !last)
332 gcc_assert (first == last);
333 return;
336 gsi_insert_seq_nodes_after (i, first, last, mode);
340 /* Links sequence SEQ after the statement pointed-to by iterator I.
341 MODE is as in gsi_insert_after. Scan the statements in SEQ
342 for new operands. */
344 void
345 gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
346 enum gsi_iterator_update mode)
348 update_modified_stmts (seq);
349 gsi_insert_seq_after_without_update (i, seq, mode);
353 /* Move all statements in the sequence after I to a new sequence.
354 Return this new sequence. */
356 gimple_seq
357 gsi_split_seq_after (gimple_stmt_iterator i)
359 gimple_seq_node cur, next;
360 gimple_seq *pold_seq, new_seq;
362 cur = i.ptr;
364 /* How can we possibly split after the end, or before the beginning? */
365 gcc_assert (cur && cur->next);
366 next = cur->next;
368 pold_seq = i.seq;
370 gimple_seq_set_first (&new_seq, next);
371 gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
372 gimple_seq_set_last (pold_seq, cur);
373 cur->next = NULL;
375 return new_seq;
379 /* Set the statement to which GSI points to STMT. This only updates
380 the iterator and the gimple sequence, it doesn't do the bookkeeping
381 of gsi_replace. */
383 void
384 gsi_set_stmt (gimple_stmt_iterator *gsi, gimple stmt)
386 gimple orig_stmt = gsi_stmt (*gsi);
387 gimple prev, next;
389 stmt->next = next = orig_stmt->next;
390 stmt->prev = prev = orig_stmt->prev;
391 /* Note how we don't clear next/prev of orig_stmt. This is so that
392 copies of *GSI our callers might still hold (to orig_stmt)
393 can be advanced as if they too were replaced. */
394 if (prev->next)
395 prev->next = stmt;
396 else
397 gimple_seq_set_first (gsi->seq, stmt);
398 if (next)
399 next->prev = stmt;
400 else
401 gimple_seq_set_last (gsi->seq, stmt);
403 gsi->ptr = stmt;
407 /* Move all statements in the sequence before I to a new sequence.
408 Return this new sequence. I is set to the head of the new list. */
410 void
411 gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq)
413 gimple_seq_node cur, prev;
414 gimple_seq old_seq;
416 cur = i->ptr;
418 /* How can we possibly split after the end? */
419 gcc_assert (cur);
420 prev = cur->prev;
422 old_seq = *i->seq;
423 if (!prev->next)
424 *i->seq = NULL;
425 i->seq = pnew_seq;
427 /* Set the limits on NEW_SEQ. */
428 gimple_seq_set_first (pnew_seq, cur);
429 gimple_seq_set_last (pnew_seq, gimple_seq_last (old_seq));
431 /* Cut OLD_SEQ before I. */
432 gimple_seq_set_last (&old_seq, prev);
433 if (prev->next)
434 prev->next = NULL;
438 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
439 is true, the exception handling information of the original
440 statement is moved to the new statement. Assignments must only be
441 replaced with assignments to the same LHS. Returns whether EH edge
442 cleanup is required. */
444 bool
445 gsi_replace (gimple_stmt_iterator *gsi, gimple stmt, bool update_eh_info)
447 gimple orig_stmt = gsi_stmt (*gsi);
448 bool require_eh_edge_purge = false;
450 if (stmt == orig_stmt)
451 return false;
453 gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
454 || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
456 gimple_set_location (stmt, gimple_location (orig_stmt));
457 gimple_set_bb (stmt, gsi_bb (*gsi));
459 /* Preserve EH region information from the original statement, if
460 requested by the caller. */
461 if (update_eh_info)
462 require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
464 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
466 /* Free all the data flow information for ORIG_STMT. */
467 gimple_set_bb (orig_stmt, NULL);
468 gimple_remove_stmt_histograms (cfun, orig_stmt);
469 delink_stmt_imm_use (orig_stmt);
471 gsi_set_stmt (gsi, stmt);
472 gimple_set_modified (stmt, true);
473 update_modified_stmt (stmt);
474 return require_eh_edge_purge;
478 /* Replace the statement pointed-to by GSI with the sequence SEQ.
479 If UPDATE_EH_INFO is true, the exception handling information of
480 the original statement is moved to the last statement of the new
481 sequence. If the old statement is an assignment, then so must
482 be the last statement of the new sequence, and they must have the
483 same LHS. */
485 void
486 gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq,
487 bool update_eh_info)
489 gimple_stmt_iterator seqi;
490 gimple last;
491 if (gimple_seq_empty_p (seq))
493 gsi_remove (gsi, true);
494 return;
496 seqi = gsi_last (seq);
497 last = gsi_stmt (seqi);
498 gsi_remove (&seqi, false);
499 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
500 gsi_replace (gsi, last, update_eh_info);
504 /* Insert statement STMT before the statement pointed-to by iterator I.
505 M specifies how to update iterator I after insertion (see enum
506 gsi_iterator_update).
508 This function does not scan for new operands. It is provided for
509 the use of the gimplifier, which manipulates statements for which
510 def/use information has not yet been constructed. Most callers
511 should use gsi_insert_before. */
513 void
514 gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple stmt,
515 enum gsi_iterator_update m)
517 gsi_insert_seq_nodes_before (i, stmt, stmt, m);
520 /* Insert statement STMT before the statement pointed-to by iterator I.
521 Update STMT's basic block and scan it for new operands. M
522 specifies how to update iterator I after insertion (see enum
523 gsi_iterator_update). */
525 void
526 gsi_insert_before (gimple_stmt_iterator *i, gimple stmt,
527 enum gsi_iterator_update m)
529 update_modified_stmt (stmt);
530 gsi_insert_before_without_update (i, stmt, m);
534 /* Insert statement STMT after the statement pointed-to by iterator I.
535 M specifies how to update iterator I after insertion (see enum
536 gsi_iterator_update).
538 This function does not scan for new operands. It is provided for
539 the use of the gimplifier, which manipulates statements for which
540 def/use information has not yet been constructed. Most callers
541 should use gsi_insert_after. */
543 void
544 gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple stmt,
545 enum gsi_iterator_update m)
547 gsi_insert_seq_nodes_after (i, stmt, stmt, m);
551 /* Insert statement STMT after the statement pointed-to by iterator I.
552 Update STMT's basic block and scan it for new operands. M
553 specifies how to update iterator I after insertion (see enum
554 gsi_iterator_update). */
556 void
557 gsi_insert_after (gimple_stmt_iterator *i, gimple stmt,
558 enum gsi_iterator_update m)
560 update_modified_stmt (stmt);
561 gsi_insert_after_without_update (i, stmt, m);
565 /* Remove the current stmt from the sequence. The iterator is updated
566 to point to the next statement.
568 REMOVE_PERMANENTLY is true when the statement is going to be removed
569 from the IL and not reinserted elsewhere. In that case we remove the
570 statement pointed to by iterator I from the EH tables, and free its
571 operand caches. Otherwise we do not modify this information. Returns
572 true whether EH edge cleanup is required. */
574 bool
575 gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
577 gimple_seq_node cur, next, prev;
578 gimple stmt = gsi_stmt (*i);
579 bool require_eh_edge_purge = false;
581 if (gimple_code (stmt) != GIMPLE_PHI)
582 insert_debug_temps_for_defs (i);
584 /* Free all the data flow information for STMT. */
585 gimple_set_bb (stmt, NULL);
586 delink_stmt_imm_use (stmt);
587 gimple_set_modified (stmt, true);
589 if (remove_permanently)
591 require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
592 gimple_remove_stmt_histograms (cfun, stmt);
595 /* Update the iterator and re-wire the links in I->SEQ. */
596 cur = i->ptr;
597 next = cur->next;
598 prev = cur->prev;
599 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
601 if (next)
602 /* Cur is not last. */
603 next->prev = prev;
604 else if (prev->next)
605 /* Cur is last but not first. */
606 gimple_seq_set_last (i->seq, prev);
608 if (prev->next)
609 /* Cur is not first. */
610 prev->next = next;
611 else
612 /* Cur is first. */
613 *i->seq = next;
615 i->ptr = next;
617 return require_eh_edge_purge;
621 /* Finds iterator for STMT. */
623 gimple_stmt_iterator
624 gsi_for_stmt (gimple stmt)
626 gimple_stmt_iterator i;
627 basic_block bb = gimple_bb (stmt);
629 if (gimple_code (stmt) == GIMPLE_PHI)
630 i = gsi_start_phis (bb);
631 else
632 i = gsi_start_bb (bb);
634 i.ptr = stmt;
635 return i;
638 /* Finds iterator for PHI. */
640 gphi_iterator
641 gsi_for_phi (gphi *phi)
643 gphi_iterator i;
644 basic_block bb = gimple_bb (phi);
646 i = gsi_start_phis (bb);
647 i.ptr = phi;
649 return i;
652 /* Move the statement at FROM so it comes right after the statement at TO. */
654 void
655 gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
657 gimple stmt = gsi_stmt (*from);
658 gsi_remove (from, false);
660 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
661 move statements to an empty block. */
662 gsi_insert_after (to, stmt, GSI_NEW_STMT);
666 /* Move the statement at FROM so it comes right before the statement
667 at TO. */
669 void
670 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
672 gimple stmt = gsi_stmt (*from);
673 gsi_remove (from, false);
675 /* For consistency with gsi_move_after, it might be better to have
676 GSI_NEW_STMT here; however, that breaks several places that expect
677 that TO does not change. */
678 gsi_insert_before (to, stmt, GSI_SAME_STMT);
682 /* Move the statement at FROM to the end of basic block BB. */
684 void
685 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
687 gimple_stmt_iterator last = gsi_last_bb (bb);
688 gcc_checking_assert (gsi_bb (last) == bb);
690 /* Have to check gsi_end_p because it could be an empty block. */
691 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
692 gsi_move_before (from, &last);
693 else
694 gsi_move_after (from, &last);
698 /* Add STMT to the pending list of edge E. No actual insertion is
699 made until a call to gsi_commit_edge_inserts () is made. */
701 void
702 gsi_insert_on_edge (edge e, gimple stmt)
704 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
707 /* Add the sequence of statements SEQ to the pending list of edge E.
708 No actual insertion is made until a call to gsi_commit_edge_inserts
709 is made. */
711 void
712 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
714 gimple_seq_add_seq (&PENDING_STMT (e), seq);
717 /* Return a new iterator pointing to the first statement in sequence of
718 statements on edge E. Such statements need to be subsequently moved into a
719 basic block by calling gsi_commit_edge_inserts. */
721 gimple_stmt_iterator
722 gsi_start_edge (edge e)
724 return gsi_start (PENDING_STMT (e));
727 /* Insert the statement pointed-to by GSI into edge E. Every attempt
728 is made to place the statement in an existing basic block, but
729 sometimes that isn't possible. When it isn't possible, the edge is
730 split and the statement is added to the new block.
732 In all cases, the returned *GSI points to the correct location. The
733 return value is true if insertion should be done after the location,
734 or false if it should be done before the location. If a new basic block
735 has to be created, it is stored in *NEW_BB. */
737 static bool
738 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
739 basic_block *new_bb)
741 basic_block dest, src;
742 gimple tmp;
744 dest = e->dest;
746 /* If the destination has one predecessor which has no PHI nodes,
747 insert there. Except for the exit block.
749 The requirement for no PHI nodes could be relaxed. Basically we
750 would have to examine the PHIs to prove that none of them used
751 the value set by the statement we want to insert on E. That
752 hardly seems worth the effort. */
753 restart:
754 if (single_pred_p (dest)
755 && gimple_seq_empty_p (phi_nodes (dest))
756 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
758 *gsi = gsi_start_bb (dest);
759 if (gsi_end_p (*gsi))
760 return true;
762 /* Make sure we insert after any leading labels. */
763 tmp = gsi_stmt (*gsi);
764 while (gimple_code (tmp) == GIMPLE_LABEL)
766 gsi_next (gsi);
767 if (gsi_end_p (*gsi))
768 break;
769 tmp = gsi_stmt (*gsi);
772 if (gsi_end_p (*gsi))
774 *gsi = gsi_last_bb (dest);
775 return true;
777 else
778 return false;
781 /* If the source has one successor, the edge is not abnormal and
782 the last statement does not end a basic block, insert there.
783 Except for the entry block. */
784 src = e->src;
785 if ((e->flags & EDGE_ABNORMAL) == 0
786 && single_succ_p (src)
787 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
789 *gsi = gsi_last_bb (src);
790 if (gsi_end_p (*gsi))
791 return true;
793 tmp = gsi_stmt (*gsi);
794 if (!stmt_ends_bb_p (tmp))
795 return true;
797 switch (gimple_code (tmp))
799 case GIMPLE_RETURN:
800 case GIMPLE_RESX:
801 return false;
802 default:
803 break;
807 /* Otherwise, create a new basic block, and split this edge. */
808 dest = split_edge (e);
809 if (new_bb)
810 *new_bb = dest;
811 e = single_pred_edge (dest);
812 goto restart;
816 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
817 block has to be created, it is returned. */
819 basic_block
820 gsi_insert_on_edge_immediate (edge e, gimple stmt)
822 gimple_stmt_iterator gsi;
823 basic_block new_bb = NULL;
824 bool ins_after;
826 gcc_assert (!PENDING_STMT (e));
828 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
830 update_call_edge_frequencies (stmt, gsi.bb);
832 if (ins_after)
833 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
834 else
835 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
837 return new_bb;
840 /* Insert STMTS on edge E. If a new block has to be created, it
841 is returned. */
843 basic_block
844 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
846 gimple_stmt_iterator gsi;
847 basic_block new_bb = NULL;
848 bool ins_after;
850 gcc_assert (!PENDING_STMT (e));
852 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
853 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
855 if (ins_after)
856 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
857 else
858 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
860 return new_bb;
863 /* This routine will commit all pending edge insertions, creating any new
864 basic blocks which are necessary. */
866 void
867 gsi_commit_edge_inserts (void)
869 basic_block bb;
870 edge e;
871 edge_iterator ei;
873 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
874 NULL);
876 FOR_EACH_BB_FN (bb, cfun)
877 FOR_EACH_EDGE (e, ei, bb->succs)
878 gsi_commit_one_edge_insert (e, NULL);
882 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
883 to this block, otherwise set it to NULL. */
885 void
886 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
888 if (new_bb)
889 *new_bb = NULL;
891 if (PENDING_STMT (e))
893 gimple_stmt_iterator gsi;
894 gimple_seq seq = PENDING_STMT (e);
895 bool ins_after;
897 PENDING_STMT (e) = NULL;
899 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
900 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
902 if (ins_after)
903 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
904 else
905 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
909 /* Returns iterator at the start of the list of phi nodes of BB. */
911 gphi_iterator
912 gsi_start_phis (basic_block bb)
914 gimple_seq *pseq = phi_nodes_ptr (bb);
916 /* Adapted from gsi_start_1. */
917 gphi_iterator i;
919 i.ptr = gimple_seq_first (*pseq);
920 i.seq = pseq;
921 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
923 return i;