gcov: Remove TARGET_GCOV_TYPE_SIZE target hook
[official-gcc.git] / gcc / gimple-iterator.cc
blob1cc3d581b967929b66a3e692a2faf52e17c6db42
1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2023 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 "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfghooks.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "tree-eh.h"
31 #include "gimple-iterator.h"
32 #include "tree-cfg.h"
33 #include "tree-ssa.h"
34 #include "value-prof.h"
37 /* Mark the statement STMT as modified, and update it. */
39 static inline void
40 update_modified_stmt (gimple *stmt)
42 if (!ssa_operands_active (cfun))
43 return;
44 update_stmt_if_modified (stmt);
48 /* Mark the statements in SEQ as modified, and update them. */
50 void
51 update_modified_stmts (gimple_seq seq)
53 gimple_stmt_iterator gsi;
55 if (!ssa_operands_active (cfun))
56 return;
57 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
58 update_stmt_if_modified (gsi_stmt (gsi));
62 /* Set BB to be the basic block for all the statements in the list
63 starting at FIRST and LAST. */
65 static void
66 update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last,
67 basic_block bb)
69 gimple_seq_node n;
71 for (n = first; n; n = n->next)
73 gimple_set_bb (n, bb);
74 if (n == last)
75 break;
79 /* Set the frequencies for the cgraph_edges for each of the calls
80 starting at FIRST for their new position within BB. */
82 static void
83 update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
85 struct cgraph_node *cfun_node = NULL;
86 gimple_seq_node n;
88 for (n = first; n ; n = n->next)
89 if (is_gimple_call (n))
91 struct cgraph_edge *e;
93 /* These function calls are expensive enough that we want
94 to avoid calling them if we never see any calls. */
95 if (cfun_node == NULL)
96 cfun_node = cgraph_node::get (current_function_decl);
98 e = cfun_node->get_edge (n);
99 if (e != NULL)
100 e->count = bb->count;
104 /* Insert the sequence delimited by nodes FIRST and LAST before
105 iterator I. M specifies how to update iterator I after insertion
106 (see enum gsi_iterator_update).
108 This routine assumes that there is a forward and backward path
109 between FIRST and LAST (i.e., they are linked in a doubly-linked
110 list). Additionally, if FIRST == LAST, this routine will properly
111 insert a single node. */
113 static void
114 gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
115 gimple_seq_node first,
116 gimple_seq_node last,
117 enum gsi_iterator_update mode)
119 basic_block bb;
120 gimple_seq_node cur = i->ptr;
122 gcc_assert (!cur || cur->prev);
124 if ((bb = gsi_bb (*i)) != NULL)
125 update_bb_for_stmts (first, last, bb);
127 /* Link SEQ before CUR in the sequence. */
128 if (cur)
130 first->prev = cur->prev;
131 if (first->prev->next)
132 first->prev->next = first;
133 else
134 gimple_seq_set_first (i->seq, first);
135 last->next = cur;
136 cur->prev = last;
138 else
140 gimple_seq_node itlast = gimple_seq_last (*i->seq);
142 /* If CUR is NULL, we link at the end of the sequence (this case happens
143 when gsi_after_labels is called for a basic block that contains only
144 labels, so it returns an iterator after the end of the block, and
145 we need to insert before it; it might be cleaner to add a flag to the
146 iterator saying whether we are at the start or end of the list). */
147 last->next = NULL;
148 if (itlast)
150 first->prev = itlast;
151 itlast->next = first;
153 else
154 gimple_seq_set_first (i->seq, first);
155 gimple_seq_set_last (i->seq, last);
158 /* Update the iterator, if requested. */
159 switch (mode)
161 case GSI_NEW_STMT:
162 case GSI_CONTINUE_LINKING:
163 i->ptr = first;
164 break;
165 case GSI_LAST_NEW_STMT:
166 i->ptr = last;
167 break;
168 case GSI_SAME_STMT:
169 break;
170 default:
171 gcc_unreachable ();
176 /* Inserts the sequence of statements SEQ before the statement pointed
177 by iterator I. MODE indicates what to do with the iterator after
178 insertion (see enum gsi_iterator_update).
180 This function does not scan for new operands. It is provided for
181 the use of the gimplifier, which manipulates statements for which
182 def/use information has not yet been constructed. Most callers
183 should use gsi_insert_seq_before. */
185 void
186 gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
187 enum gsi_iterator_update mode)
189 gimple_seq_node first, last;
191 if (seq == NULL)
192 return;
194 /* Don't allow inserting a sequence into itself. */
195 gcc_assert (seq != *i->seq);
197 first = gimple_seq_first (seq);
198 last = gimple_seq_last (seq);
200 /* Empty sequences need no work. */
201 if (!first || !last)
203 gcc_assert (first == last);
204 return;
207 gsi_insert_seq_nodes_before (i, first, last, mode);
211 /* Inserts the sequence of statements SEQ before the statement pointed
212 by iterator I. MODE indicates what to do with the iterator after
213 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
214 for new operands. */
216 void
217 gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
218 enum gsi_iterator_update mode)
220 update_modified_stmts (seq);
221 gsi_insert_seq_before_without_update (i, seq, mode);
225 /* Insert the sequence delimited by nodes FIRST and LAST after
226 iterator I. M specifies how to update iterator I after insertion
227 (see enum gsi_iterator_update).
229 This routine assumes that there is a forward and backward path
230 between FIRST and LAST (i.e., they are linked in a doubly-linked
231 list). Additionally, if FIRST == LAST, this routine will properly
232 insert a single node. */
234 static void
235 gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
236 gimple_seq_node first,
237 gimple_seq_node last,
238 enum gsi_iterator_update m)
240 basic_block bb;
241 gimple_seq_node cur = i->ptr;
243 gcc_assert (!cur || cur->prev);
245 /* If the iterator is inside a basic block, we need to update the
246 basic block information for all the nodes between FIRST and LAST. */
247 if ((bb = gsi_bb (*i)) != NULL)
248 update_bb_for_stmts (first, last, bb);
250 /* Link SEQ after CUR. */
251 if (cur)
253 last->next = cur->next;
254 if (last->next)
256 last->next->prev = last;
258 else
259 gimple_seq_set_last (i->seq, last);
260 first->prev = cur;
261 cur->next = first;
263 else
265 gcc_assert (!gimple_seq_last (*i->seq));
266 last->next = NULL;
267 gimple_seq_set_first (i->seq, first);
268 gimple_seq_set_last (i->seq, last);
271 /* Update the iterator, if requested. */
272 switch (m)
274 case GSI_NEW_STMT:
275 i->ptr = first;
276 break;
277 case GSI_LAST_NEW_STMT:
278 case GSI_CONTINUE_LINKING:
279 i->ptr = last;
280 break;
281 case GSI_SAME_STMT:
282 gcc_assert (cur);
283 break;
284 default:
285 gcc_unreachable ();
290 /* Links sequence SEQ after the statement pointed-to by iterator I.
291 MODE is as in gsi_insert_after.
293 This function does not scan for new operands. It is provided for
294 the use of the gimplifier, which manipulates statements for which
295 def/use information has not yet been constructed. Most callers
296 should use gsi_insert_seq_after. */
298 void
299 gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
300 enum gsi_iterator_update mode)
302 gimple_seq_node first, last;
304 if (seq == NULL)
305 return;
307 /* Don't allow inserting a sequence into itself. */
308 gcc_assert (seq != *i->seq);
310 first = gimple_seq_first (seq);
311 last = gimple_seq_last (seq);
313 /* Empty sequences need no work. */
314 if (!first || !last)
316 gcc_assert (first == last);
317 return;
320 gsi_insert_seq_nodes_after (i, first, last, mode);
324 /* Links sequence SEQ after the statement pointed-to by iterator I.
325 MODE is as in gsi_insert_after. Scan the statements in SEQ
326 for new operands. */
328 void
329 gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
330 enum gsi_iterator_update mode)
332 update_modified_stmts (seq);
333 gsi_insert_seq_after_without_update (i, seq, mode);
337 /* Move all statements in the sequence after I to a new sequence.
338 Return this new sequence. */
340 gimple_seq
341 gsi_split_seq_after (gimple_stmt_iterator i)
343 gimple_seq_node cur, next;
344 gimple_seq *pold_seq, new_seq;
346 cur = i.ptr;
348 /* How can we possibly split after the end, or before the beginning? */
349 gcc_assert (cur && cur->next);
350 next = cur->next;
352 pold_seq = i.seq;
354 gimple_seq_set_first (&new_seq, next);
355 gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
356 gimple_seq_set_last (pold_seq, cur);
357 cur->next = NULL;
359 return new_seq;
363 /* Set the statement to which GSI points to STMT. This only updates
364 the iterator and the gimple sequence, it doesn't do the bookkeeping
365 of gsi_replace. */
367 void
368 gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt)
370 gimple *orig_stmt = gsi_stmt (*gsi);
371 gimple *prev, *next;
373 stmt->next = next = orig_stmt->next;
374 stmt->prev = prev = orig_stmt->prev;
375 /* Note how we don't clear next/prev of orig_stmt. This is so that
376 copies of *GSI our callers might still hold (to orig_stmt)
377 can be advanced as if they too were replaced. */
378 if (prev->next)
379 prev->next = stmt;
380 else
381 gimple_seq_set_first (gsi->seq, stmt);
382 if (next)
383 next->prev = stmt;
384 else
385 gimple_seq_set_last (gsi->seq, stmt);
387 gsi->ptr = stmt;
391 /* Move all statements in the sequence before I to a new sequence.
392 Return this new sequence. I is set to the head of the new list. */
394 void
395 gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq)
397 gimple_seq_node cur, prev;
398 gimple_seq old_seq;
400 cur = i->ptr;
402 /* How can we possibly split after the end? */
403 gcc_assert (cur);
404 prev = cur->prev;
406 old_seq = *i->seq;
407 if (!prev->next)
408 *i->seq = NULL;
409 i->seq = pnew_seq;
411 /* Set the limits on NEW_SEQ. */
412 gimple_seq_set_first (pnew_seq, cur);
413 gimple_seq_set_last (pnew_seq, gimple_seq_last (old_seq));
415 /* Cut OLD_SEQ before I. */
416 gimple_seq_set_last (&old_seq, prev);
417 if (prev->next)
418 prev->next = NULL;
422 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
423 is true, the exception handling information of the original
424 statement is moved to the new statement. Assignments must only be
425 replaced with assignments to the same LHS. Returns whether EH edge
426 cleanup is required. */
428 bool
429 gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info)
431 gimple *orig_stmt = gsi_stmt (*gsi);
432 bool require_eh_edge_purge = false;
434 if (stmt == orig_stmt)
435 return false;
437 gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
438 || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
440 gimple_set_location (stmt, gimple_location (orig_stmt));
441 gimple_set_bb (stmt, gsi_bb (*gsi));
443 /* Preserve EH region information from the original statement, if
444 requested by the caller. */
445 if (update_eh_info)
446 require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
448 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
450 /* Free all the data flow information for ORIG_STMT. */
451 gimple_set_bb (orig_stmt, NULL);
452 gimple_remove_stmt_histograms (cfun, orig_stmt);
453 delink_stmt_imm_use (orig_stmt);
455 gsi_set_stmt (gsi, stmt);
456 gimple_set_modified (stmt, true);
457 update_modified_stmt (stmt);
458 return require_eh_edge_purge;
462 /* Replace the statement pointed-to by GSI with the sequence SEQ.
463 If UPDATE_EH_INFO is true, the exception handling information of
464 the original statement is moved to the last statement of the new
465 sequence. If the old statement is an assignment, then so must
466 be the last statement of the new sequence, and they must have the
467 same LHS. */
469 void
470 gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq,
471 bool update_eh_info)
473 gimple_stmt_iterator seqi;
474 gimple *last;
475 if (gimple_seq_empty_p (seq))
477 gsi_remove (gsi, true);
478 return;
480 seqi = gsi_last (seq);
481 last = gsi_stmt (seqi);
482 gsi_remove (&seqi, false);
483 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
484 gsi_replace (gsi, last, update_eh_info);
488 /* Insert statement STMT before the statement pointed-to by iterator I.
489 M specifies how to update iterator I after insertion (see enum
490 gsi_iterator_update).
492 This function does not scan for new operands. It is provided for
493 the use of the gimplifier, which manipulates statements for which
494 def/use information has not yet been constructed. Most callers
495 should use gsi_insert_before. */
497 void
498 gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt,
499 enum gsi_iterator_update m)
501 gsi_insert_seq_nodes_before (i, stmt, stmt, m);
504 /* Insert statement STMT before the statement pointed-to by iterator I.
505 Update STMT's basic block and scan it for new operands. M
506 specifies how to update iterator I after insertion (see enum
507 gsi_iterator_update). */
509 void
510 gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt,
511 enum gsi_iterator_update m)
513 update_modified_stmt (stmt);
514 gsi_insert_before_without_update (i, stmt, m);
518 /* Insert statement STMT after the statement pointed-to by iterator I.
519 M specifies how to update iterator I after insertion (see enum
520 gsi_iterator_update).
522 This function does not scan for new operands. It is provided for
523 the use of the gimplifier, which manipulates statements for which
524 def/use information has not yet been constructed. Most callers
525 should use gsi_insert_after. */
527 void
528 gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt,
529 enum gsi_iterator_update m)
531 gsi_insert_seq_nodes_after (i, stmt, stmt, m);
535 /* Insert statement STMT after the statement pointed-to by iterator I.
536 Update STMT's basic block and scan it for new operands. M
537 specifies how to update iterator I after insertion (see enum
538 gsi_iterator_update). */
540 void
541 gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt,
542 enum gsi_iterator_update m)
544 update_modified_stmt (stmt);
545 gsi_insert_after_without_update (i, stmt, m);
549 /* Remove the current stmt from the sequence. The iterator is updated
550 to point to the next statement.
552 REMOVE_PERMANENTLY is true when the statement is going to be removed
553 from the IL and not reinserted elsewhere. In that case we remove the
554 statement pointed to by iterator I from the EH tables, and free its
555 operand caches. Otherwise we do not modify this information. Returns
556 true whether EH edge cleanup is required. */
558 bool
559 gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
561 gimple_seq_node cur, next, prev;
562 gimple *stmt = gsi_stmt (*i);
563 bool require_eh_edge_purge = false;
565 /* ??? Do we want to do this for non-permanent operation? */
566 if (gimple_code (stmt) != GIMPLE_PHI)
567 insert_debug_temps_for_defs (i);
569 gimple_set_bb (stmt, NULL);
571 if (remove_permanently)
573 /* Free all the data flow information for STMT. */
574 delink_stmt_imm_use (stmt);
575 gimple_set_modified (stmt, true);
577 if (gimple_debug_nonbind_marker_p (stmt))
578 /* We don't need this to be exact, but try to keep it at least
579 close. */
580 cfun->debug_marker_count--;
581 require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
582 gimple_remove_stmt_histograms (cfun, stmt);
585 /* Update the iterator and re-wire the links in I->SEQ. */
586 cur = i->ptr;
587 next = cur->next;
588 prev = cur->prev;
589 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
591 if (next)
592 /* Cur is not last. */
593 next->prev = prev;
594 else if (prev->next)
595 /* Cur is last but not first. */
596 gimple_seq_set_last (i->seq, prev);
598 if (prev->next)
599 /* Cur is not first. */
600 prev->next = next;
601 else
602 /* Cur is first. */
603 *i->seq = next;
605 i->ptr = next;
607 return require_eh_edge_purge;
611 /* Finds iterator for STMT. */
613 gimple_stmt_iterator
614 gsi_for_stmt (gimple *stmt)
616 gimple_stmt_iterator i;
617 basic_block bb = gimple_bb (stmt);
619 if (gimple_code (stmt) == GIMPLE_PHI)
620 i = gsi_start_phis (bb);
621 else
622 i = gsi_start_bb (bb);
624 i.ptr = stmt;
625 return i;
628 /* Get an iterator for STMT, which is known to belong to SEQ. This is
629 equivalent to starting at the beginning of SEQ and searching forward
630 until STMT is found. */
632 gimple_stmt_iterator
633 gsi_for_stmt (gimple *stmt, gimple_seq *seq)
635 gimple_stmt_iterator i = gsi_start (*seq);
636 i.ptr = stmt;
637 return i;
640 /* Finds iterator for PHI. */
642 gphi_iterator
643 gsi_for_phi (gphi *phi)
645 gphi_iterator i;
646 basic_block bb = gimple_bb (phi);
648 i = gsi_start_phis (bb);
649 i.ptr = phi;
651 return i;
654 /* Move the statement at FROM so it comes right after the statement at TO. */
656 void
657 gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
659 gimple *stmt = gsi_stmt (*from);
660 gsi_remove (from, false);
662 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
663 move statements to an empty block. */
664 gsi_insert_after (to, stmt, GSI_NEW_STMT);
668 /* Move the statement at FROM so it comes right before the statement
669 at TO. */
671 void
672 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
674 gimple *stmt = gsi_stmt (*from);
675 gsi_remove (from, false);
677 /* For consistency with gsi_move_after, it might be better to have
678 GSI_NEW_STMT here; however, that breaks several places that expect
679 that TO does not change. */
680 gsi_insert_before (to, stmt, GSI_SAME_STMT);
684 /* Move the statement at FROM to the end of basic block BB. */
686 void
687 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
689 gimple_stmt_iterator last = gsi_last_bb (bb);
690 gcc_checking_assert (gsi_bb (last) == bb);
692 /* Have to check gsi_end_p because it could be an empty block. */
693 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
694 gsi_move_before (from, &last);
695 else
696 gsi_move_after (from, &last);
700 /* Add STMT to the pending list of edge E. No actual insertion is
701 made until a call to gsi_commit_edge_inserts () is made. */
703 void
704 gsi_insert_on_edge (edge e, gimple *stmt)
706 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
709 /* Add the sequence of statements SEQ to the pending list of edge E.
710 No actual insertion is made until a call to gsi_commit_edge_inserts
711 is made. */
713 void
714 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
716 gimple_seq_add_seq (&PENDING_STMT (e), seq);
719 /* Return a new iterator pointing to the first statement in sequence of
720 statements on edge E. Such statements need to be subsequently moved into a
721 basic block by calling gsi_commit_edge_inserts. */
723 gimple_stmt_iterator
724 gsi_start_edge (edge e)
726 return gsi_start (PENDING_STMT (e));
729 /* Insert the statement pointed-to by GSI into edge E. Every attempt
730 is made to place the statement in an existing basic block, but
731 sometimes that isn't possible. When it isn't possible, the edge is
732 split and the statement is added to the new block.
734 In all cases, the returned *GSI points to the correct location. The
735 return value is true if insertion should be done after the location,
736 or false if it should be done before the location. If a new basic block
737 has to be created, it is stored in *NEW_BB. */
739 static bool
740 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
741 basic_block *new_bb)
743 basic_block dest, src;
744 gimple *tmp;
746 dest = e->dest;
748 /* If the destination has one predecessor which has no PHI nodes,
749 insert there. Except for the exit block.
751 The requirement for no PHI nodes could be relaxed. Basically we
752 would have to examine the PHIs to prove that none of them used
753 the value set by the statement we want to insert on E. That
754 hardly seems worth the effort. */
755 restart:
756 if (single_pred_p (dest)
757 && gimple_seq_empty_p (phi_nodes (dest))
758 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
760 *gsi = gsi_start_bb (dest);
761 if (gsi_end_p (*gsi))
762 return true;
764 /* Make sure we insert after any leading labels. */
765 tmp = gsi_stmt (*gsi);
766 while (gimple_code (tmp) == GIMPLE_LABEL)
768 gsi_next (gsi);
769 if (gsi_end_p (*gsi))
770 break;
771 tmp = gsi_stmt (*gsi);
774 if (gsi_end_p (*gsi))
776 *gsi = gsi_last_bb (dest);
777 return true;
779 else
780 return false;
783 /* If the source has one successor, the edge is not abnormal and
784 the last statement does not end a basic block, insert there.
785 Except for the entry block. */
786 src = e->src;
787 if ((e->flags & EDGE_ABNORMAL) == 0
788 && (single_succ_p (src)
789 /* Do not count a fake edge as successor as added to infinite
790 loops by connect_infinite_loops_to_exit. */
791 || (EDGE_COUNT (src->succs) == 2
792 && (EDGE_SUCC (src, 0)->flags & EDGE_FAKE
793 || EDGE_SUCC (src, 1)->flags & EDGE_FAKE)))
794 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
796 *gsi = gsi_last_bb (src);
797 if (gsi_end_p (*gsi))
798 return true;
800 tmp = gsi_stmt (*gsi);
801 if (is_gimple_debug (tmp))
803 gimple_stmt_iterator si = *gsi;
804 gsi_prev_nondebug (&si);
805 if (!gsi_end_p (si))
806 tmp = gsi_stmt (si);
807 /* If we don't have a BB-ending nondebug stmt, we want to
808 insert after the trailing debug stmts. Otherwise, we may
809 insert before the BB-ending nondebug stmt, or split the
810 edge. */
811 if (!stmt_ends_bb_p (tmp))
812 return true;
813 *gsi = si;
815 else if (!stmt_ends_bb_p (tmp))
816 return true;
818 switch (gimple_code (tmp))
820 case GIMPLE_RETURN:
821 case GIMPLE_RESX:
822 return false;
823 default:
824 break;
828 /* Otherwise, create a new basic block, and split this edge. */
829 dest = split_edge (e);
830 if (new_bb)
831 *new_bb = dest;
832 e = single_pred_edge (dest);
833 goto restart;
837 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
838 block has to be created, it is returned. */
840 basic_block
841 gsi_insert_on_edge_immediate (edge e, gimple *stmt)
843 gimple_stmt_iterator gsi;
844 basic_block new_bb = NULL;
845 bool ins_after;
847 gcc_assert (!PENDING_STMT (e));
849 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
851 update_call_edge_frequencies (stmt, gsi.bb);
853 if (ins_after)
854 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
855 else
856 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
858 return new_bb;
861 /* Insert STMTS on edge E. If a new block has to be created, it
862 is returned. */
864 basic_block
865 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
867 gimple_stmt_iterator gsi;
868 basic_block new_bb = NULL;
869 bool ins_after;
871 gcc_assert (!PENDING_STMT (e));
873 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
874 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
876 if (ins_after)
877 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
878 else
879 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
881 return new_bb;
884 /* This routine will commit all pending edge insertions, creating any new
885 basic blocks which are necessary. */
887 void
888 gsi_commit_edge_inserts (void)
890 basic_block bb;
891 edge e;
892 edge_iterator ei;
894 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
895 NULL);
897 FOR_EACH_BB_FN (bb, cfun)
898 FOR_EACH_EDGE (e, ei, bb->succs)
899 gsi_commit_one_edge_insert (e, NULL);
903 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
904 to this block, otherwise set it to NULL. */
906 void
907 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
909 if (new_bb)
910 *new_bb = NULL;
912 if (PENDING_STMT (e))
914 gimple_stmt_iterator gsi;
915 gimple_seq seq = PENDING_STMT (e);
916 bool ins_after;
918 PENDING_STMT (e) = NULL;
920 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
921 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
923 if (ins_after)
924 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
925 else
926 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
930 /* Returns iterator at the start of the list of phi nodes of BB. */
932 gphi_iterator
933 gsi_start_phis (basic_block bb)
935 gimple_seq *pseq = phi_nodes_ptr (bb);
937 /* Adapted from gsi_start. */
938 gphi_iterator i;
940 i.ptr = gimple_seq_first (*pseq);
941 i.seq = pseq;
942 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
944 return i;