[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / gimple-iterator.c
blobc998b65f65926883631bae6646381b2ce2b0fa75
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 "backend.h"
25 #include "cfghooks.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "hard-reg-set.h"
29 #include "ssa.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "internal-fn.h"
33 #include "tree-eh.h"
34 #include "gimple-iterator.h"
35 #include "cgraph.h"
36 #include "tree-cfg.h"
37 #include "tree-ssa.h"
38 #include "value-prof.h"
41 /* Mark the statement STMT as modified, and update it. */
43 static inline void
44 update_modified_stmt (gimple *stmt)
46 if (!ssa_operands_active (cfun))
47 return;
48 update_stmt_if_modified (stmt);
52 /* Mark the statements in SEQ as modified, and update them. */
54 void
55 update_modified_stmts (gimple_seq seq)
57 gimple_stmt_iterator gsi;
59 if (!ssa_operands_active (cfun))
60 return;
61 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
62 update_stmt_if_modified (gsi_stmt (gsi));
66 /* Set BB to be the basic block for all the statements in the list
67 starting at FIRST and LAST. */
69 static void
70 update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last,
71 basic_block bb)
73 gimple_seq_node n;
75 for (n = first; n; n = n->next)
77 gimple_set_bb (n, bb);
78 if (n == last)
79 break;
83 /* Set the frequencies for the cgraph_edges for each of the calls
84 starting at FIRST for their new position within BB. */
86 static void
87 update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
89 struct cgraph_node *cfun_node = NULL;
90 int bb_freq = 0;
91 gimple_seq_node n;
93 for (n = first; n ; n = n->next)
94 if (is_gimple_call (n))
96 struct cgraph_edge *e;
98 /* These function calls are expensive enough that we want
99 to avoid calling them if we never see any calls. */
100 if (cfun_node == NULL)
102 cfun_node = cgraph_node::get (current_function_decl);
103 bb_freq = (compute_call_stmt_bb_frequency
104 (current_function_decl, bb));
107 e = cfun_node->get_edge (n);
108 if (e != NULL)
109 e->frequency = bb_freq;
113 /* Insert the sequence delimited by nodes FIRST and LAST before
114 iterator I. M specifies how to update iterator I after insertion
115 (see enum gsi_iterator_update).
117 This routine assumes that there is a forward and backward path
118 between FIRST and LAST (i.e., they are linked in a doubly-linked
119 list). Additionally, if FIRST == LAST, this routine will properly
120 insert a single node. */
122 static void
123 gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
124 gimple_seq_node first,
125 gimple_seq_node last,
126 enum gsi_iterator_update mode)
128 basic_block bb;
129 gimple_seq_node cur = i->ptr;
131 gcc_assert (!cur || cur->prev);
133 if ((bb = gsi_bb (*i)) != NULL)
134 update_bb_for_stmts (first, last, bb);
136 /* Link SEQ before CUR in the sequence. */
137 if (cur)
139 first->prev = cur->prev;
140 if (first->prev->next)
141 first->prev->next = first;
142 else
143 gimple_seq_set_first (i->seq, first);
144 last->next = cur;
145 cur->prev = last;
147 else
149 gimple_seq_node itlast = gimple_seq_last (*i->seq);
151 /* If CUR is NULL, we link at the end of the sequence (this case happens
152 when gsi_after_labels is called for a basic block that contains only
153 labels, so it returns an iterator after the end of the block, and
154 we need to insert before it; it might be cleaner to add a flag to the
155 iterator saying whether we are at the start or end of the list). */
156 last->next = NULL;
157 if (itlast)
159 first->prev = itlast;
160 itlast->next = first;
162 else
163 gimple_seq_set_first (i->seq, first);
164 gimple_seq_set_last (i->seq, last);
167 /* Update the iterator, if requested. */
168 switch (mode)
170 case GSI_NEW_STMT:
171 case GSI_CONTINUE_LINKING:
172 i->ptr = first;
173 break;
174 case GSI_SAME_STMT:
175 break;
176 default:
177 gcc_unreachable ();
182 /* Inserts the sequence of statements SEQ before the statement pointed
183 by iterator I. MODE indicates what to do with the iterator after
184 insertion (see enum gsi_iterator_update).
186 This function does not scan for new operands. It is provided for
187 the use of the gimplifier, which manipulates statements for which
188 def/use information has not yet been constructed. Most callers
189 should use gsi_insert_seq_before. */
191 void
192 gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
193 enum gsi_iterator_update mode)
195 gimple_seq_node first, last;
197 if (seq == NULL)
198 return;
200 /* Don't allow inserting a sequence into itself. */
201 gcc_assert (seq != *i->seq);
203 first = gimple_seq_first (seq);
204 last = gimple_seq_last (seq);
206 /* Empty sequences need no work. */
207 if (!first || !last)
209 gcc_assert (first == last);
210 return;
213 gsi_insert_seq_nodes_before (i, first, last, mode);
217 /* Inserts the sequence of statements SEQ before the statement pointed
218 by iterator I. MODE indicates what to do with the iterator after
219 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
220 for new operands. */
222 void
223 gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
224 enum gsi_iterator_update mode)
226 update_modified_stmts (seq);
227 gsi_insert_seq_before_without_update (i, seq, mode);
231 /* Insert the sequence delimited by nodes FIRST and LAST after
232 iterator I. M specifies how to update iterator I after insertion
233 (see enum gsi_iterator_update).
235 This routine assumes that there is a forward and backward path
236 between FIRST and LAST (i.e., they are linked in a doubly-linked
237 list). Additionally, if FIRST == LAST, this routine will properly
238 insert a single node. */
240 static void
241 gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
242 gimple_seq_node first,
243 gimple_seq_node last,
244 enum gsi_iterator_update m)
246 basic_block bb;
247 gimple_seq_node cur = i->ptr;
249 gcc_assert (!cur || cur->prev);
251 /* If the iterator is inside a basic block, we need to update the
252 basic block information for all the nodes between FIRST and LAST. */
253 if ((bb = gsi_bb (*i)) != NULL)
254 update_bb_for_stmts (first, last, bb);
256 /* Link SEQ after CUR. */
257 if (cur)
259 last->next = cur->next;
260 if (last->next)
262 last->next->prev = last;
264 else
265 gimple_seq_set_last (i->seq, last);
266 first->prev = cur;
267 cur->next = first;
269 else
271 gcc_assert (!gimple_seq_last (*i->seq));
272 last->next = NULL;
273 gimple_seq_set_first (i->seq, first);
274 gimple_seq_set_last (i->seq, last);
277 /* Update the iterator, if requested. */
278 switch (m)
280 case GSI_NEW_STMT:
281 i->ptr = first;
282 break;
283 case GSI_CONTINUE_LINKING:
284 i->ptr = last;
285 break;
286 case GSI_SAME_STMT:
287 gcc_assert (cur);
288 break;
289 default:
290 gcc_unreachable ();
295 /* Links sequence SEQ after the statement pointed-to by iterator I.
296 MODE is as in gsi_insert_after.
298 This function does not scan for new operands. It is provided for
299 the use of the gimplifier, which manipulates statements for which
300 def/use information has not yet been constructed. Most callers
301 should use gsi_insert_seq_after. */
303 void
304 gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
305 enum gsi_iterator_update mode)
307 gimple_seq_node first, last;
309 if (seq == NULL)
310 return;
312 /* Don't allow inserting a sequence into itself. */
313 gcc_assert (seq != *i->seq);
315 first = gimple_seq_first (seq);
316 last = gimple_seq_last (seq);
318 /* Empty sequences need no work. */
319 if (!first || !last)
321 gcc_assert (first == last);
322 return;
325 gsi_insert_seq_nodes_after (i, first, last, mode);
329 /* Links sequence SEQ after the statement pointed-to by iterator I.
330 MODE is as in gsi_insert_after. Scan the statements in SEQ
331 for new operands. */
333 void
334 gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
335 enum gsi_iterator_update mode)
337 update_modified_stmts (seq);
338 gsi_insert_seq_after_without_update (i, seq, mode);
342 /* Move all statements in the sequence after I to a new sequence.
343 Return this new sequence. */
345 gimple_seq
346 gsi_split_seq_after (gimple_stmt_iterator i)
348 gimple_seq_node cur, next;
349 gimple_seq *pold_seq, new_seq;
351 cur = i.ptr;
353 /* How can we possibly split after the end, or before the beginning? */
354 gcc_assert (cur && cur->next);
355 next = cur->next;
357 pold_seq = i.seq;
359 gimple_seq_set_first (&new_seq, next);
360 gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
361 gimple_seq_set_last (pold_seq, cur);
362 cur->next = NULL;
364 return new_seq;
368 /* Set the statement to which GSI points to STMT. This only updates
369 the iterator and the gimple sequence, it doesn't do the bookkeeping
370 of gsi_replace. */
372 void
373 gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt)
375 gimple *orig_stmt = gsi_stmt (*gsi);
376 gimple *prev, *next;
378 stmt->next = next = orig_stmt->next;
379 stmt->prev = prev = orig_stmt->prev;
380 /* Note how we don't clear next/prev of orig_stmt. This is so that
381 copies of *GSI our callers might still hold (to orig_stmt)
382 can be advanced as if they too were replaced. */
383 if (prev->next)
384 prev->next = stmt;
385 else
386 gimple_seq_set_first (gsi->seq, stmt);
387 if (next)
388 next->prev = stmt;
389 else
390 gimple_seq_set_last (gsi->seq, stmt);
392 gsi->ptr = stmt;
396 /* Move all statements in the sequence before I to a new sequence.
397 Return this new sequence. I is set to the head of the new list. */
399 void
400 gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq)
402 gimple_seq_node cur, prev;
403 gimple_seq old_seq;
405 cur = i->ptr;
407 /* How can we possibly split after the end? */
408 gcc_assert (cur);
409 prev = cur->prev;
411 old_seq = *i->seq;
412 if (!prev->next)
413 *i->seq = NULL;
414 i->seq = pnew_seq;
416 /* Set the limits on NEW_SEQ. */
417 gimple_seq_set_first (pnew_seq, cur);
418 gimple_seq_set_last (pnew_seq, gimple_seq_last (old_seq));
420 /* Cut OLD_SEQ before I. */
421 gimple_seq_set_last (&old_seq, prev);
422 if (prev->next)
423 prev->next = NULL;
427 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
428 is true, the exception handling information of the original
429 statement is moved to the new statement. Assignments must only be
430 replaced with assignments to the same LHS. Returns whether EH edge
431 cleanup is required. */
433 bool
434 gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info)
436 gimple *orig_stmt = gsi_stmt (*gsi);
437 bool require_eh_edge_purge = false;
439 if (stmt == orig_stmt)
440 return false;
442 gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
443 || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
445 gimple_set_location (stmt, gimple_location (orig_stmt));
446 gimple_set_bb (stmt, gsi_bb (*gsi));
448 /* Preserve EH region information from the original statement, if
449 requested by the caller. */
450 if (update_eh_info)
451 require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
453 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
455 /* Free all the data flow information for ORIG_STMT. */
456 gimple_set_bb (orig_stmt, NULL);
457 gimple_remove_stmt_histograms (cfun, orig_stmt);
458 delink_stmt_imm_use (orig_stmt);
460 gsi_set_stmt (gsi, stmt);
461 gimple_set_modified (stmt, true);
462 update_modified_stmt (stmt);
463 return require_eh_edge_purge;
467 /* Replace the statement pointed-to by GSI with the sequence SEQ.
468 If UPDATE_EH_INFO is true, the exception handling information of
469 the original statement is moved to the last statement of the new
470 sequence. If the old statement is an assignment, then so must
471 be the last statement of the new sequence, and they must have the
472 same LHS. */
474 void
475 gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq,
476 bool update_eh_info)
478 gimple_stmt_iterator seqi;
479 gimple *last;
480 if (gimple_seq_empty_p (seq))
482 gsi_remove (gsi, true);
483 return;
485 seqi = gsi_last (seq);
486 last = gsi_stmt (seqi);
487 gsi_remove (&seqi, false);
488 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
489 gsi_replace (gsi, last, update_eh_info);
493 /* Insert statement STMT before the statement pointed-to by iterator I.
494 M specifies how to update iterator I after insertion (see enum
495 gsi_iterator_update).
497 This function does not scan for new operands. It is provided for
498 the use of the gimplifier, which manipulates statements for which
499 def/use information has not yet been constructed. Most callers
500 should use gsi_insert_before. */
502 void
503 gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt,
504 enum gsi_iterator_update m)
506 gsi_insert_seq_nodes_before (i, stmt, stmt, m);
509 /* Insert statement STMT before the statement pointed-to by iterator I.
510 Update STMT's basic block and scan it for new operands. M
511 specifies how to update iterator I after insertion (see enum
512 gsi_iterator_update). */
514 void
515 gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt,
516 enum gsi_iterator_update m)
518 update_modified_stmt (stmt);
519 gsi_insert_before_without_update (i, stmt, m);
523 /* Insert statement STMT after the statement pointed-to by iterator I.
524 M specifies how to update iterator I after insertion (see enum
525 gsi_iterator_update).
527 This function does not scan for new operands. It is provided for
528 the use of the gimplifier, which manipulates statements for which
529 def/use information has not yet been constructed. Most callers
530 should use gsi_insert_after. */
532 void
533 gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt,
534 enum gsi_iterator_update m)
536 gsi_insert_seq_nodes_after (i, stmt, stmt, m);
540 /* Insert statement STMT after the statement pointed-to by iterator I.
541 Update STMT's basic block and scan it for new operands. M
542 specifies how to update iterator I after insertion (see enum
543 gsi_iterator_update). */
545 void
546 gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt,
547 enum gsi_iterator_update m)
549 update_modified_stmt (stmt);
550 gsi_insert_after_without_update (i, stmt, m);
554 /* Remove the current stmt from the sequence. The iterator is updated
555 to point to the next statement.
557 REMOVE_PERMANENTLY is true when the statement is going to be removed
558 from the IL and not reinserted elsewhere. In that case we remove the
559 statement pointed to by iterator I from the EH tables, and free its
560 operand caches. Otherwise we do not modify this information. Returns
561 true whether EH edge cleanup is required. */
563 bool
564 gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
566 gimple_seq_node cur, next, prev;
567 gimple *stmt = gsi_stmt (*i);
568 bool require_eh_edge_purge = false;
570 if (gimple_code (stmt) != GIMPLE_PHI)
571 insert_debug_temps_for_defs (i);
573 /* Free all the data flow information for STMT. */
574 gimple_set_bb (stmt, NULL);
575 delink_stmt_imm_use (stmt);
576 gimple_set_modified (stmt, true);
578 if (remove_permanently)
580 require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
581 gimple_remove_stmt_histograms (cfun, stmt);
584 /* Update the iterator and re-wire the links in I->SEQ. */
585 cur = i->ptr;
586 next = cur->next;
587 prev = cur->prev;
588 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
590 if (next)
591 /* Cur is not last. */
592 next->prev = prev;
593 else if (prev->next)
594 /* Cur is last but not first. */
595 gimple_seq_set_last (i->seq, prev);
597 if (prev->next)
598 /* Cur is not first. */
599 prev->next = next;
600 else
601 /* Cur is first. */
602 *i->seq = next;
604 i->ptr = next;
606 return require_eh_edge_purge;
610 /* Finds iterator for STMT. */
612 gimple_stmt_iterator
613 gsi_for_stmt (gimple *stmt)
615 gimple_stmt_iterator i;
616 basic_block bb = gimple_bb (stmt);
618 if (gimple_code (stmt) == GIMPLE_PHI)
619 i = gsi_start_phis (bb);
620 else
621 i = gsi_start_bb (bb);
623 i.ptr = stmt;
624 return i;
627 /* Finds iterator for PHI. */
629 gphi_iterator
630 gsi_for_phi (gphi *phi)
632 gphi_iterator i;
633 basic_block bb = gimple_bb (phi);
635 i = gsi_start_phis (bb);
636 i.ptr = phi;
638 return i;
641 /* Move the statement at FROM so it comes right after the statement at TO. */
643 void
644 gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
646 gimple *stmt = gsi_stmt (*from);
647 gsi_remove (from, false);
649 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
650 move statements to an empty block. */
651 gsi_insert_after (to, stmt, GSI_NEW_STMT);
655 /* Move the statement at FROM so it comes right before the statement
656 at TO. */
658 void
659 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
661 gimple *stmt = gsi_stmt (*from);
662 gsi_remove (from, false);
664 /* For consistency with gsi_move_after, it might be better to have
665 GSI_NEW_STMT here; however, that breaks several places that expect
666 that TO does not change. */
667 gsi_insert_before (to, stmt, GSI_SAME_STMT);
671 /* Move the statement at FROM to the end of basic block BB. */
673 void
674 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
676 gimple_stmt_iterator last = gsi_last_bb (bb);
677 gcc_checking_assert (gsi_bb (last) == bb);
679 /* Have to check gsi_end_p because it could be an empty block. */
680 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
681 gsi_move_before (from, &last);
682 else
683 gsi_move_after (from, &last);
687 /* Add STMT to the pending list of edge E. No actual insertion is
688 made until a call to gsi_commit_edge_inserts () is made. */
690 void
691 gsi_insert_on_edge (edge e, gimple *stmt)
693 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
696 /* Add the sequence of statements SEQ to the pending list of edge E.
697 No actual insertion is made until a call to gsi_commit_edge_inserts
698 is made. */
700 void
701 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
703 gimple_seq_add_seq (&PENDING_STMT (e), seq);
706 /* Return a new iterator pointing to the first statement in sequence of
707 statements on edge E. Such statements need to be subsequently moved into a
708 basic block by calling gsi_commit_edge_inserts. */
710 gimple_stmt_iterator
711 gsi_start_edge (edge e)
713 return gsi_start (PENDING_STMT (e));
716 /* Insert the statement pointed-to by GSI into edge E. Every attempt
717 is made to place the statement in an existing basic block, but
718 sometimes that isn't possible. When it isn't possible, the edge is
719 split and the statement is added to the new block.
721 In all cases, the returned *GSI points to the correct location. The
722 return value is true if insertion should be done after the location,
723 or false if it should be done before the location. If a new basic block
724 has to be created, it is stored in *NEW_BB. */
726 static bool
727 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
728 basic_block *new_bb)
730 basic_block dest, src;
731 gimple *tmp;
733 dest = e->dest;
735 /* If the destination has one predecessor which has no PHI nodes,
736 insert there. Except for the exit block.
738 The requirement for no PHI nodes could be relaxed. Basically we
739 would have to examine the PHIs to prove that none of them used
740 the value set by the statement we want to insert on E. That
741 hardly seems worth the effort. */
742 restart:
743 if (single_pred_p (dest)
744 && gimple_seq_empty_p (phi_nodes (dest))
745 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
747 *gsi = gsi_start_bb (dest);
748 if (gsi_end_p (*gsi))
749 return true;
751 /* Make sure we insert after any leading labels. */
752 tmp = gsi_stmt (*gsi);
753 while (gimple_code (tmp) == GIMPLE_LABEL)
755 gsi_next (gsi);
756 if (gsi_end_p (*gsi))
757 break;
758 tmp = gsi_stmt (*gsi);
761 if (gsi_end_p (*gsi))
763 *gsi = gsi_last_bb (dest);
764 return true;
766 else
767 return false;
770 /* If the source has one successor, the edge is not abnormal and
771 the last statement does not end a basic block, insert there.
772 Except for the entry block. */
773 src = e->src;
774 if ((e->flags & EDGE_ABNORMAL) == 0
775 && single_succ_p (src)
776 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
778 *gsi = gsi_last_bb (src);
779 if (gsi_end_p (*gsi))
780 return true;
782 tmp = gsi_stmt (*gsi);
783 if (!stmt_ends_bb_p (tmp))
784 return true;
786 switch (gimple_code (tmp))
788 case GIMPLE_RETURN:
789 case GIMPLE_RESX:
790 return false;
791 default:
792 break;
796 /* Otherwise, create a new basic block, and split this edge. */
797 dest = split_edge (e);
798 if (new_bb)
799 *new_bb = dest;
800 e = single_pred_edge (dest);
801 goto restart;
805 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
806 block has to be created, it is returned. */
808 basic_block
809 gsi_insert_on_edge_immediate (edge e, gimple *stmt)
811 gimple_stmt_iterator gsi;
812 basic_block new_bb = NULL;
813 bool ins_after;
815 gcc_assert (!PENDING_STMT (e));
817 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
819 update_call_edge_frequencies (stmt, gsi.bb);
821 if (ins_after)
822 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
823 else
824 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
826 return new_bb;
829 /* Insert STMTS on edge E. If a new block has to be created, it
830 is returned. */
832 basic_block
833 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
835 gimple_stmt_iterator gsi;
836 basic_block new_bb = NULL;
837 bool ins_after;
839 gcc_assert (!PENDING_STMT (e));
841 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
842 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
844 if (ins_after)
845 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
846 else
847 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
849 return new_bb;
852 /* This routine will commit all pending edge insertions, creating any new
853 basic blocks which are necessary. */
855 void
856 gsi_commit_edge_inserts (void)
858 basic_block bb;
859 edge e;
860 edge_iterator ei;
862 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
863 NULL);
865 FOR_EACH_BB_FN (bb, cfun)
866 FOR_EACH_EDGE (e, ei, bb->succs)
867 gsi_commit_one_edge_insert (e, NULL);
871 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
872 to this block, otherwise set it to NULL. */
874 void
875 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
877 if (new_bb)
878 *new_bb = NULL;
880 if (PENDING_STMT (e))
882 gimple_stmt_iterator gsi;
883 gimple_seq seq = PENDING_STMT (e);
884 bool ins_after;
886 PENDING_STMT (e) = NULL;
888 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
889 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
891 if (ins_after)
892 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
893 else
894 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
898 /* Returns iterator at the start of the list of phi nodes of BB. */
900 gphi_iterator
901 gsi_start_phis (basic_block bb)
903 gimple_seq *pseq = phi_nodes_ptr (bb);
905 /* Adapted from gsi_start_1. */
906 gphi_iterator i;
908 i.ptr = gimple_seq_first (*pseq);
909 i.seq = pseq;
910 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
912 return i;