Daily bump.
[official-gcc.git] / gcc / gimple-iterator.cc
blob55ef3198c52b01c8d3902f92f7395dc1d98dea92
1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2024 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 using method M. M defaults to GSI_SAME_STMT. */
671 void
672 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to,
673 gsi_iterator_update m)
675 gimple *stmt = gsi_stmt (*from);
676 gsi_remove (from, false);
678 /* For consistency with gsi_move_after, it might be better to have
679 GSI_NEW_STMT here; however, that breaks several places that expect
680 that TO does not change. */
681 gsi_insert_before (to, stmt, m);
685 /* Move the statement at FROM to the end of basic block BB. */
687 void
688 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
690 gimple_stmt_iterator last = gsi_last_bb (bb);
691 gcc_checking_assert (gsi_bb (last) == bb);
693 /* Have to check gsi_end_p because it could be an empty block. */
694 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
695 gsi_move_before (from, &last);
696 else
697 gsi_move_after (from, &last);
701 /* Add STMT to the pending list of edge E. No actual insertion is
702 made until a call to gsi_commit_edge_inserts () is made. */
704 void
705 gsi_insert_on_edge (edge e, gimple *stmt)
707 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
710 /* Add the sequence of statements SEQ to the pending list of edge E.
711 No actual insertion is made until a call to gsi_commit_edge_inserts
712 is made. */
714 void
715 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
717 gimple_seq_add_seq (&PENDING_STMT (e), seq);
720 /* Return a new iterator pointing to the first statement in sequence of
721 statements on edge E. Such statements need to be subsequently moved into a
722 basic block by calling gsi_commit_edge_inserts. */
724 gimple_stmt_iterator
725 gsi_start_edge (edge e)
727 return gsi_start (PENDING_STMT (e));
730 /* Insert the statement pointed-to by GSI into edge E. Every attempt
731 is made to place the statement in an existing basic block, but
732 sometimes that isn't possible. When it isn't possible, the edge is
733 split and the statement is added to the new block.
735 In all cases, the returned *GSI points to the correct location. The
736 return value is true if insertion should be done after the location,
737 or false if it should be done before the location. If a new basic block
738 has to be created, it is stored in *NEW_BB. */
740 static bool
741 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
742 basic_block *new_bb)
744 basic_block dest, src;
745 gimple *tmp;
747 dest = e->dest;
749 /* If the destination has one predecessor which has no PHI nodes,
750 insert there. Except for the exit block.
752 The requirement for no PHI nodes could be relaxed. Basically we
753 would have to examine the PHIs to prove that none of them used
754 the value set by the statement we want to insert on E. That
755 hardly seems worth the effort. */
756 restart:
757 if (single_pred_p (dest)
758 && gimple_seq_empty_p (phi_nodes (dest))
759 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
761 *gsi = gsi_start_bb (dest);
762 if (gsi_end_p (*gsi))
763 return true;
765 /* Make sure we insert after any leading labels. */
766 tmp = gsi_stmt (*gsi);
767 while (gimple_code (tmp) == GIMPLE_LABEL)
769 gsi_next (gsi);
770 if (gsi_end_p (*gsi))
771 break;
772 tmp = gsi_stmt (*gsi);
775 if (gsi_end_p (*gsi))
777 *gsi = gsi_last_bb (dest);
778 return true;
780 else
781 return false;
784 /* If the source has one successor, the edge is not abnormal and
785 the last statement does not end a basic block, insert there.
786 Except for the entry block. */
787 src = e->src;
788 if ((e->flags & EDGE_ABNORMAL) == 0
789 && (single_succ_p (src)
790 /* Do not count a fake edge as successor as added to infinite
791 loops by connect_infinite_loops_to_exit. */
792 || (EDGE_COUNT (src->succs) == 2
793 && (EDGE_SUCC (src, 0)->flags & EDGE_FAKE
794 || EDGE_SUCC (src, 1)->flags & EDGE_FAKE)))
795 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
797 *gsi = gsi_last_bb (src);
798 if (gsi_end_p (*gsi))
799 return true;
801 tmp = gsi_stmt (*gsi);
802 if (is_gimple_debug (tmp))
804 gimple_stmt_iterator si = *gsi;
805 gsi_prev_nondebug (&si);
806 if (!gsi_end_p (si))
807 tmp = gsi_stmt (si);
808 /* If we don't have a BB-ending nondebug stmt, we want to
809 insert after the trailing debug stmts. Otherwise, we may
810 insert before the BB-ending nondebug stmt, or split the
811 edge. */
812 if (!stmt_ends_bb_p (tmp))
813 return true;
814 *gsi = si;
816 else if (!stmt_ends_bb_p (tmp))
817 return true;
819 switch (gimple_code (tmp))
821 case GIMPLE_RETURN:
822 case GIMPLE_RESX:
823 return false;
824 default:
825 break;
829 /* Otherwise, create a new basic block, and split this edge. */
830 dest = split_edge (e);
831 if (new_bb)
832 *new_bb = dest;
833 e = single_pred_edge (dest);
834 goto restart;
838 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
839 block has to be created, it is returned. */
841 basic_block
842 gsi_insert_on_edge_immediate (edge e, gimple *stmt)
844 gimple_stmt_iterator gsi;
845 basic_block new_bb = NULL;
846 bool ins_after;
848 gcc_assert (!PENDING_STMT (e));
850 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
852 update_call_edge_frequencies (stmt, gsi.bb);
854 if (ins_after)
855 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
856 else
857 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
859 return new_bb;
862 /* Insert STMTS on edge E. If a new block has to be created, it
863 is returned. */
865 basic_block
866 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
868 gimple_stmt_iterator gsi;
869 basic_block new_bb = NULL;
870 bool ins_after;
872 gcc_assert (!PENDING_STMT (e));
874 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
875 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
877 if (ins_after)
878 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
879 else
880 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
882 return new_bb;
885 /* This routine will commit all pending edge insertions, creating any new
886 basic blocks which are necessary. */
888 void
889 gsi_commit_edge_inserts (void)
891 basic_block bb;
892 edge e;
893 edge_iterator ei;
895 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
896 NULL);
898 FOR_EACH_BB_FN (bb, cfun)
899 FOR_EACH_EDGE (e, ei, bb->succs)
900 gsi_commit_one_edge_insert (e, NULL);
904 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
905 to this block, otherwise set it to NULL. */
907 void
908 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
910 if (new_bb)
911 *new_bb = NULL;
913 if (PENDING_STMT (e))
915 gimple_stmt_iterator gsi;
916 gimple_seq seq = PENDING_STMT (e);
917 bool ins_after;
919 PENDING_STMT (e) = NULL;
921 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
922 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
924 if (ins_after)
925 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
926 else
927 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
931 /* Returns iterator at the start of the list of phi nodes of BB. */
933 gphi_iterator
934 gsi_start_phis (basic_block bb)
936 gimple_seq *pseq = phi_nodes_ptr (bb);
938 /* Adapted from gsi_start. */
939 gphi_iterator i;
941 i.ptr = gimple_seq_first (*pseq);
942 i.seq = pseq;
943 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
945 return i;