Daily bump.
[official-gcc.git] / gcc / gimple-iterator.cc
blob93646262eac5f47fc3634806ce7665c4f148e40d
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"
35 #include "gimplify.h"
38 /* Mark the statement STMT as modified, and update it. */
40 static inline void
41 update_modified_stmt (gimple *stmt)
43 if (!ssa_operands_active (cfun))
44 return;
45 update_stmt_if_modified (stmt);
49 /* Mark the statements in SEQ as modified, and update them. */
51 void
52 update_modified_stmts (gimple_seq seq)
54 gimple_stmt_iterator gsi;
56 if (!ssa_operands_active (cfun))
57 return;
58 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
59 update_stmt_if_modified (gsi_stmt (gsi));
63 /* Set BB to be the basic block for all the statements in the list
64 starting at FIRST and LAST. */
66 static void
67 update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last,
68 basic_block bb)
70 gimple_seq_node n;
72 for (n = first; n; n = n->next)
74 gimple_set_bb (n, bb);
75 if (n == last)
76 break;
80 /* Set the frequencies for the cgraph_edges for each of the calls
81 starting at FIRST for their new position within BB. */
83 static void
84 update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
86 struct cgraph_node *cfun_node = NULL;
87 gimple_seq_node n;
89 for (n = first; n ; n = n->next)
90 if (is_gimple_call (n))
92 struct cgraph_edge *e;
94 /* These function calls are expensive enough that we want
95 to avoid calling them if we never see any calls. */
96 if (cfun_node == NULL)
97 cfun_node = cgraph_node::get (current_function_decl);
99 e = cfun_node->get_edge (n);
100 if (e != NULL)
101 e->count = bb->count;
105 /* Insert the sequence delimited by nodes FIRST and LAST before
106 iterator I. M specifies how to update iterator I after insertion
107 (see enum gsi_iterator_update).
109 This routine assumes that there is a forward and backward path
110 between FIRST and LAST (i.e., they are linked in a doubly-linked
111 list). Additionally, if FIRST == LAST, this routine will properly
112 insert a single node. */
114 static void
115 gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
116 gimple_seq_node first,
117 gimple_seq_node last,
118 enum gsi_iterator_update mode)
120 basic_block bb;
121 gimple_seq_node cur = i->ptr;
123 gcc_assert (!cur || cur->prev);
125 if ((bb = gsi_bb (*i)) != NULL)
126 update_bb_for_stmts (first, last, bb);
128 /* Link SEQ before CUR in the sequence. */
129 if (cur)
131 first->prev = cur->prev;
132 if (first->prev->next)
133 first->prev->next = first;
134 else
135 gimple_seq_set_first (i->seq, first);
136 last->next = cur;
137 cur->prev = last;
139 else
141 gimple_seq_node itlast = gimple_seq_last (*i->seq);
143 /* If CUR is NULL, we link at the end of the sequence (this case happens
144 when gsi_after_labels is called for a basic block that contains only
145 labels, so it returns an iterator after the end of the block, and
146 we need to insert before it; it might be cleaner to add a flag to the
147 iterator saying whether we are at the start or end of the list). */
148 last->next = NULL;
149 if (itlast)
151 first->prev = itlast;
152 itlast->next = first;
154 else
155 gimple_seq_set_first (i->seq, first);
156 gimple_seq_set_last (i->seq, last);
159 /* Update the iterator, if requested. */
160 switch (mode)
162 case GSI_NEW_STMT:
163 case GSI_CONTINUE_LINKING:
164 i->ptr = first;
165 break;
166 case GSI_LAST_NEW_STMT:
167 i->ptr = last;
168 break;
169 case GSI_SAME_STMT:
170 break;
171 default:
172 gcc_unreachable ();
177 /* Inserts the sequence of statements SEQ before the statement pointed
178 by iterator I. MODE indicates what to do with the iterator after
179 insertion (see enum gsi_iterator_update).
181 This function does not scan for new operands. It is provided for
182 the use of the gimplifier, which manipulates statements for which
183 def/use information has not yet been constructed. Most callers
184 should use gsi_insert_seq_before. */
186 void
187 gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
188 enum gsi_iterator_update mode)
190 gimple_seq_node first, last;
192 if (seq == NULL)
193 return;
195 /* Don't allow inserting a sequence into itself. */
196 gcc_assert (seq != *i->seq);
198 first = gimple_seq_first (seq);
199 last = gimple_seq_last (seq);
201 /* Empty sequences need no work. */
202 if (!first || !last)
204 gcc_assert (first == last);
205 return;
208 gsi_insert_seq_nodes_before (i, first, last, mode);
212 /* Inserts the sequence of statements SEQ before the statement pointed
213 by iterator I. MODE indicates what to do with the iterator after
214 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
215 for new operands. */
217 void
218 gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
219 enum gsi_iterator_update mode)
221 update_modified_stmts (seq);
222 gsi_insert_seq_before_without_update (i, seq, mode);
226 /* Insert the sequence delimited by nodes FIRST and LAST after
227 iterator I. M specifies how to update iterator I after insertion
228 (see enum gsi_iterator_update).
230 This routine assumes that there is a forward and backward path
231 between FIRST and LAST (i.e., they are linked in a doubly-linked
232 list). Additionally, if FIRST == LAST, this routine will properly
233 insert a single node. */
235 static void
236 gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
237 gimple_seq_node first,
238 gimple_seq_node last,
239 enum gsi_iterator_update m)
241 basic_block bb;
242 gimple_seq_node cur = i->ptr;
244 gcc_assert (!cur || cur->prev);
246 /* If the iterator is inside a basic block, we need to update the
247 basic block information for all the nodes between FIRST and LAST. */
248 if ((bb = gsi_bb (*i)) != NULL)
249 update_bb_for_stmts (first, last, bb);
251 /* Link SEQ after CUR. */
252 if (cur)
254 last->next = cur->next;
255 if (last->next)
257 last->next->prev = last;
259 else
260 gimple_seq_set_last (i->seq, last);
261 first->prev = cur;
262 cur->next = first;
264 else
266 gcc_assert (!gimple_seq_last (*i->seq));
267 last->next = NULL;
268 gimple_seq_set_first (i->seq, first);
269 gimple_seq_set_last (i->seq, last);
272 /* Update the iterator, if requested. */
273 switch (m)
275 case GSI_NEW_STMT:
276 i->ptr = first;
277 break;
278 case GSI_LAST_NEW_STMT:
279 case GSI_CONTINUE_LINKING:
280 i->ptr = last;
281 break;
282 case GSI_SAME_STMT:
283 gcc_assert (cur);
284 break;
285 default:
286 gcc_unreachable ();
291 /* Links sequence SEQ after the statement pointed-to by iterator I.
292 MODE is as in gsi_insert_after.
294 This function does not scan for new operands. It is provided for
295 the use of the gimplifier, which manipulates statements for which
296 def/use information has not yet been constructed. Most callers
297 should use gsi_insert_seq_after. */
299 void
300 gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
301 enum gsi_iterator_update mode)
303 gimple_seq_node first, last;
305 if (seq == NULL)
306 return;
308 /* Don't allow inserting a sequence into itself. */
309 gcc_assert (seq != *i->seq);
311 first = gimple_seq_first (seq);
312 last = gimple_seq_last (seq);
314 /* Empty sequences need no work. */
315 if (!first || !last)
317 gcc_assert (first == last);
318 return;
321 gsi_insert_seq_nodes_after (i, first, last, mode);
325 /* Links sequence SEQ after the statement pointed-to by iterator I.
326 MODE is as in gsi_insert_after. Scan the statements in SEQ
327 for new operands. */
329 void
330 gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
331 enum gsi_iterator_update mode)
333 update_modified_stmts (seq);
334 gsi_insert_seq_after_without_update (i, seq, mode);
338 /* Move all statements in the sequence after I to a new sequence.
339 Return this new sequence. */
341 gimple_seq
342 gsi_split_seq_after (gimple_stmt_iterator i)
344 gimple_seq_node cur, next;
345 gimple_seq *pold_seq, new_seq;
347 cur = i.ptr;
349 /* How can we possibly split after the end, or before the beginning? */
350 gcc_assert (cur && cur->next);
351 next = cur->next;
353 pold_seq = i.seq;
355 gimple_seq_set_first (&new_seq, next);
356 gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
357 gimple_seq_set_last (pold_seq, cur);
358 cur->next = NULL;
360 return new_seq;
364 /* Set the statement to which GSI points to STMT. This only updates
365 the iterator and the gimple sequence, it doesn't do the bookkeeping
366 of gsi_replace. */
368 void
369 gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt)
371 gimple *orig_stmt = gsi_stmt (*gsi);
372 gimple *prev, *next;
374 stmt->next = next = orig_stmt->next;
375 stmt->prev = prev = orig_stmt->prev;
376 /* Note how we don't clear next/prev of orig_stmt. This is so that
377 copies of *GSI our callers might still hold (to orig_stmt)
378 can be advanced as if they too were replaced. */
379 if (prev->next)
380 prev->next = stmt;
381 else
382 gimple_seq_set_first (gsi->seq, stmt);
383 if (next)
384 next->prev = stmt;
385 else
386 gimple_seq_set_last (gsi->seq, stmt);
388 gsi->ptr = stmt;
392 /* Move all statements in the sequence before I to a new sequence.
393 Return this new sequence. I is set to the head of the new list. */
395 void
396 gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq)
398 gimple_seq_node cur, prev;
399 gimple_seq old_seq;
401 cur = i->ptr;
403 /* How can we possibly split after the end? */
404 gcc_assert (cur);
405 prev = cur->prev;
407 old_seq = *i->seq;
408 if (!prev->next)
409 *i->seq = NULL;
410 i->seq = pnew_seq;
412 /* Set the limits on NEW_SEQ. */
413 gimple_seq_set_first (pnew_seq, cur);
414 gimple_seq_set_last (pnew_seq, gimple_seq_last (old_seq));
416 /* Cut OLD_SEQ before I. */
417 gimple_seq_set_last (&old_seq, prev);
418 if (prev->next)
419 prev->next = NULL;
423 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
424 is true, the exception handling information of the original
425 statement is moved to the new statement. Assignments must only be
426 replaced with assignments to the same LHS. Returns whether EH edge
427 cleanup is required. */
429 bool
430 gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info)
432 gimple *orig_stmt = gsi_stmt (*gsi);
433 bool require_eh_edge_purge = false;
435 if (stmt == orig_stmt)
436 return false;
438 gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
439 || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
441 gimple_set_location (stmt, gimple_location (orig_stmt));
442 gimple_set_bb (stmt, gsi_bb (*gsi));
444 /* Preserve EH region information from the original statement, if
445 requested by the caller. */
446 if (update_eh_info)
447 require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
449 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
451 /* Free all the data flow information for ORIG_STMT. */
452 gimple_set_bb (orig_stmt, NULL);
453 gimple_remove_stmt_histograms (cfun, orig_stmt);
454 delink_stmt_imm_use (orig_stmt);
456 gsi_set_stmt (gsi, stmt);
457 gimple_set_modified (stmt, true);
458 update_modified_stmt (stmt);
459 return require_eh_edge_purge;
463 /* Replace the statement pointed-to by GSI with the sequence SEQ.
464 If UPDATE_EH_INFO is true, the exception handling information of
465 the original statement is moved to the last statement of the new
466 sequence. If the old statement is an assignment, then so must
467 be the last statement of the new sequence, and they must have the
468 same LHS. */
470 void
471 gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq,
472 bool update_eh_info)
474 gimple_stmt_iterator seqi;
475 gimple *last;
476 if (gimple_seq_empty_p (seq))
478 gsi_remove (gsi, true);
479 return;
481 seqi = gsi_last (seq);
482 last = gsi_stmt (seqi);
483 gsi_remove (&seqi, false);
484 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
485 gsi_replace (gsi, last, update_eh_info);
489 /* Insert statement STMT before the statement pointed-to by iterator I.
490 M specifies how to update iterator I after insertion (see enum
491 gsi_iterator_update).
493 This function does not scan for new operands. It is provided for
494 the use of the gimplifier, which manipulates statements for which
495 def/use information has not yet been constructed. Most callers
496 should use gsi_insert_before. */
498 void
499 gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt,
500 enum gsi_iterator_update m)
502 gsi_insert_seq_nodes_before (i, stmt, stmt, m);
505 /* Insert statement STMT before the statement pointed-to by iterator I.
506 Update STMT's basic block and scan it for new operands. M
507 specifies how to update iterator I after insertion (see enum
508 gsi_iterator_update). */
510 void
511 gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt,
512 enum gsi_iterator_update m)
514 update_modified_stmt (stmt);
515 gsi_insert_before_without_update (i, stmt, m);
519 /* Insert statement STMT after the statement pointed-to by iterator I.
520 M specifies how to update iterator I after insertion (see enum
521 gsi_iterator_update).
523 This function does not scan for new operands. It is provided for
524 the use of the gimplifier, which manipulates statements for which
525 def/use information has not yet been constructed. Most callers
526 should use gsi_insert_after. */
528 void
529 gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt,
530 enum gsi_iterator_update m)
532 gsi_insert_seq_nodes_after (i, stmt, stmt, m);
536 /* Insert statement STMT after the statement pointed-to by iterator I.
537 Update STMT's basic block and scan it for new operands. M
538 specifies how to update iterator I after insertion (see enum
539 gsi_iterator_update). */
541 void
542 gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt,
543 enum gsi_iterator_update m)
545 update_modified_stmt (stmt);
546 gsi_insert_after_without_update (i, stmt, m);
550 /* Remove the current stmt from the sequence. The iterator is updated
551 to point to the next statement.
553 REMOVE_PERMANENTLY is true when the statement is going to be removed
554 from the IL and not reinserted elsewhere. In that case we remove the
555 statement pointed to by iterator I from the EH tables, and free its
556 operand caches. Otherwise we do not modify this information. Returns
557 true whether EH edge cleanup is required. */
559 bool
560 gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
562 gimple_seq_node cur, next, prev;
563 gimple *stmt = gsi_stmt (*i);
564 bool require_eh_edge_purge = false;
566 /* ??? Do we want to do this for non-permanent operation? */
567 if (gimple_code (stmt) != GIMPLE_PHI)
568 insert_debug_temps_for_defs (i);
570 gimple_set_bb (stmt, NULL);
572 if (remove_permanently)
574 /* Free all the data flow information for STMT. */
575 delink_stmt_imm_use (stmt);
576 gimple_set_modified (stmt, true);
578 if (gimple_debug_nonbind_marker_p (stmt))
579 /* We don't need this to be exact, but try to keep it at least
580 close. */
581 cfun->debug_marker_count--;
582 require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
583 gimple_remove_stmt_histograms (cfun, stmt);
586 /* Update the iterator and re-wire the links in I->SEQ. */
587 cur = i->ptr;
588 next = cur->next;
589 prev = cur->prev;
590 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
592 if (next)
593 /* Cur is not last. */
594 next->prev = prev;
595 else if (prev->next)
596 /* Cur is last but not first. */
597 gimple_seq_set_last (i->seq, prev);
599 if (prev->next)
600 /* Cur is not first. */
601 prev->next = next;
602 else
603 /* Cur is first. */
604 *i->seq = next;
606 i->ptr = next;
608 return require_eh_edge_purge;
612 /* Finds iterator for STMT. */
614 gimple_stmt_iterator
615 gsi_for_stmt (gimple *stmt)
617 gimple_stmt_iterator i;
618 basic_block bb = gimple_bb (stmt);
620 if (gimple_code (stmt) == GIMPLE_PHI)
621 i = gsi_start_phis (bb);
622 else
623 i = gsi_start_bb (bb);
625 i.ptr = stmt;
626 return i;
629 /* Get an iterator for STMT, which is known to belong to SEQ. This is
630 equivalent to starting at the beginning of SEQ and searching forward
631 until STMT is found. */
633 gimple_stmt_iterator
634 gsi_for_stmt (gimple *stmt, gimple_seq *seq)
636 gimple_stmt_iterator i = gsi_start (*seq);
637 i.ptr = stmt;
638 return i;
641 /* Finds iterator for PHI. */
643 gphi_iterator
644 gsi_for_phi (gphi *phi)
646 gphi_iterator i;
647 basic_block bb = gimple_bb (phi);
649 i = gsi_start_phis (bb);
650 i.ptr = phi;
652 return i;
655 /* Move the statement at FROM so it comes right after the statement at TO. */
657 void
658 gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
660 gimple *stmt = gsi_stmt (*from);
661 gsi_remove (from, false);
663 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
664 move statements to an empty block. */
665 gsi_insert_after (to, stmt, GSI_NEW_STMT);
669 /* Move the statement at FROM so it comes right before the statement
670 at TO using method M. M defaults to GSI_SAME_STMT. */
672 void
673 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to,
674 gsi_iterator_update m)
676 gimple *stmt = gsi_stmt (*from);
677 gsi_remove (from, false);
679 /* For consistency with gsi_move_after, it might be better to have
680 GSI_NEW_STMT here; however, that breaks several places that expect
681 that TO does not change. */
682 gsi_insert_before (to, stmt, m);
686 /* Move the statement at FROM to the end of basic block BB. */
688 void
689 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
691 gimple_stmt_iterator last = gsi_last_bb (bb);
692 gcc_checking_assert (gsi_bb (last) == bb);
694 /* Have to check gsi_end_p because it could be an empty block. */
695 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
696 gsi_move_before (from, &last);
697 else
698 gsi_move_after (from, &last);
702 /* Add STMT to the pending list of edge E. No actual insertion is
703 made until a call to gsi_commit_edge_inserts () is made. */
705 void
706 gsi_insert_on_edge (edge e, gimple *stmt)
708 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
711 /* Add the sequence of statements SEQ to the pending list of edge E.
712 No actual insertion is made until a call to gsi_commit_edge_inserts
713 is made. */
715 void
716 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
718 gimple_seq_add_seq (&PENDING_STMT (e), seq);
721 /* Return a new iterator pointing to the first statement in sequence of
722 statements on edge E. Such statements need to be subsequently moved into a
723 basic block by calling gsi_commit_edge_inserts. */
725 gimple_stmt_iterator
726 gsi_start_edge (edge e)
728 return gsi_start (PENDING_STMT (e));
731 /* Insert the statement pointed-to by GSI into edge E. Every attempt
732 is made to place the statement in an existing basic block, but
733 sometimes that isn't possible. When it isn't possible, the edge is
734 split and the statement is added to the new block.
736 In all cases, the returned *GSI points to the correct location. The
737 return value is true if insertion should be done after the location,
738 or false if it should be done before the location. If a new basic block
739 has to be created, it is stored in *NEW_BB. */
741 static bool
742 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
743 basic_block *new_bb)
745 basic_block dest, src;
746 gimple *tmp;
748 dest = e->dest;
750 /* If the destination has one predecessor which has no PHI nodes,
751 insert there. Except for the exit block.
753 The requirement for no PHI nodes could be relaxed. Basically we
754 would have to examine the PHIs to prove that none of them used
755 the value set by the statement we want to insert on E. That
756 hardly seems worth the effort. */
757 restart:
758 if (single_pred_p (dest)
759 && gimple_seq_empty_p (phi_nodes (dest))
760 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
762 *gsi = gsi_start_bb (dest);
763 if (gsi_end_p (*gsi))
764 return true;
766 /* Make sure we insert after any leading labels. */
767 tmp = gsi_stmt (*gsi);
768 while (gimple_code (tmp) == GIMPLE_LABEL)
770 gsi_next (gsi);
771 if (gsi_end_p (*gsi))
772 break;
773 tmp = gsi_stmt (*gsi);
776 if (gsi_end_p (*gsi))
778 *gsi = gsi_last_bb (dest);
779 return true;
781 else
782 return false;
785 /* If the source has one successor, the edge is not abnormal and
786 the last statement does not end a basic block, insert there.
787 Except for the entry block. */
788 src = e->src;
789 if ((e->flags & EDGE_ABNORMAL) == 0
790 && (single_succ_p (src)
791 /* Do not count a fake edge as successor as added to infinite
792 loops by connect_infinite_loops_to_exit. */
793 || (EDGE_COUNT (src->succs) == 2
794 && (EDGE_SUCC (src, 0)->flags & EDGE_FAKE
795 || EDGE_SUCC (src, 1)->flags & EDGE_FAKE)))
796 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
798 *gsi = gsi_last_bb (src);
799 if (gsi_end_p (*gsi))
800 return true;
802 tmp = gsi_stmt (*gsi);
803 if (is_gimple_debug (tmp))
805 gimple_stmt_iterator si = *gsi;
806 gsi_prev_nondebug (&si);
807 if (!gsi_end_p (si))
808 tmp = gsi_stmt (si);
809 /* If we don't have a BB-ending nondebug stmt, we want to
810 insert after the trailing debug stmts. Otherwise, we may
811 insert before the BB-ending nondebug stmt, or split the
812 edge. */
813 if (!stmt_ends_bb_p (tmp))
814 return true;
815 *gsi = si;
817 else if (!stmt_ends_bb_p (tmp))
818 return true;
820 switch (gimple_code (tmp))
822 case GIMPLE_RETURN:
823 case GIMPLE_RESX:
824 return false;
825 default:
826 break;
830 /* Otherwise, create a new basic block, and split this edge. */
831 dest = split_edge (e);
832 if (new_bb)
833 *new_bb = dest;
834 e = single_pred_edge (dest);
835 goto restart;
839 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
840 block has to be created, it is returned. */
842 basic_block
843 gsi_insert_on_edge_immediate (edge e, gimple *stmt)
845 gimple_stmt_iterator gsi;
846 basic_block new_bb = NULL;
847 bool ins_after;
849 gcc_assert (!PENDING_STMT (e));
851 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
853 update_call_edge_frequencies (stmt, gsi.bb);
855 if (ins_after)
856 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
857 else
858 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
860 return new_bb;
863 /* Insert STMTS on edge E. If a new block has to be created, it
864 is returned. */
866 basic_block
867 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
869 gimple_stmt_iterator gsi;
870 basic_block new_bb = NULL;
871 bool ins_after;
873 gcc_assert (!PENDING_STMT (e));
875 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
876 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
878 if (ins_after)
879 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
880 else
881 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
883 return new_bb;
886 /* This routine will commit all pending edge insertions, creating any new
887 basic blocks which are necessary. */
889 void
890 gsi_commit_edge_inserts (void)
892 basic_block bb;
893 edge e;
894 edge_iterator ei;
896 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
897 NULL);
899 FOR_EACH_BB_FN (bb, cfun)
900 FOR_EACH_EDGE (e, ei, bb->succs)
901 gsi_commit_one_edge_insert (e, NULL);
905 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
906 to this block, otherwise set it to NULL. */
908 void
909 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
911 if (new_bb)
912 *new_bb = NULL;
914 if (PENDING_STMT (e))
916 gimple_stmt_iterator gsi;
917 gimple_seq seq = PENDING_STMT (e);
918 bool ins_after;
920 PENDING_STMT (e) = NULL;
922 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
923 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
925 if (ins_after)
926 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
927 else
928 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
932 /* Returns iterator at the start of the list of phi nodes of BB. */
934 gphi_iterator
935 gsi_start_phis (basic_block bb)
937 gimple_seq *pseq = phi_nodes_ptr (bb);
939 /* Adapted from gsi_start. */
940 gphi_iterator i;
942 i.ptr = gimple_seq_first (*pseq);
943 i.seq = pseq;
944 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
946 return i;
949 /* Helper function for gsi_safe_insert_before and gsi_safe_insert_seq_before.
950 Find edge to insert statements before returns_twice call at the start of BB,
951 if there isn't just one, split the bb and adjust PHIs to ensure that. */
953 static edge
954 edge_before_returns_twice_call (basic_block bb)
956 gimple_stmt_iterator gsi = gsi_start_nondebug_bb (bb);
957 gcc_checking_assert (is_gimple_call (gsi_stmt (gsi))
958 && (gimple_call_flags (gsi_stmt (gsi))
959 & ECF_RETURNS_TWICE) != 0);
960 edge_iterator ei;
961 edge e, ad_edge = NULL, other_edge = NULL;
962 bool split = false;
963 FOR_EACH_EDGE (e, ei, bb->preds)
965 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
967 gimple_stmt_iterator gsi
968 = gsi_start_nondebug_after_labels_bb (e->src);
969 gimple *ad = gsi_stmt (gsi);
970 if (ad && gimple_call_internal_p (ad, IFN_ABNORMAL_DISPATCHER))
972 gcc_checking_assert (ad_edge == NULL);
973 ad_edge = e;
974 continue;
977 if (other_edge || e->flags & (EDGE_ABNORMAL | EDGE_EH))
978 split = true;
979 other_edge = e;
981 gcc_checking_assert (ad_edge);
982 if (other_edge == NULL)
983 split = true;
984 if (split)
986 other_edge = split_block_after_labels (bb);
987 e = make_edge (ad_edge->src, other_edge->dest, EDGE_ABNORMAL);
988 for (gphi_iterator gsi = gsi_start_phis (other_edge->src);
989 !gsi_end_p (gsi); gsi_next (&gsi))
991 gphi *phi = gsi.phi ();
992 tree lhs = gimple_phi_result (phi);
993 tree new_lhs = copy_ssa_name (lhs);
994 gimple_phi_set_result (phi, new_lhs);
995 gphi *new_phi = create_phi_node (lhs, other_edge->dest);
996 add_phi_arg (new_phi, new_lhs, other_edge, UNKNOWN_LOCATION);
997 add_phi_arg (new_phi, gimple_phi_arg_def_from_edge (phi, ad_edge),
998 e, gimple_phi_arg_location_from_edge (phi, ad_edge));
1000 e->flags = ad_edge->flags;
1001 e->probability = ad_edge->probability;
1002 remove_edge (ad_edge);
1003 if (dom_info_available_p (CDI_DOMINATORS))
1005 set_immediate_dominator (CDI_DOMINATORS, other_edge->src,
1006 recompute_dominator (CDI_DOMINATORS,
1007 other_edge->src));
1008 set_immediate_dominator (CDI_DOMINATORS, other_edge->dest,
1009 recompute_dominator (CDI_DOMINATORS,
1010 other_edge->dest));
1013 return other_edge;
1016 /* Helper function for gsi_safe_insert_before and gsi_safe_insert_seq_before.
1017 Replace SSA_NAME uses in G if they are PHI results of PHIs on E->dest
1018 bb with the corresponding PHI argument from E edge. */
1020 static void
1021 adjust_before_returns_twice_call (edge e, gimple *g)
1023 use_operand_p use_p;
1024 ssa_op_iter iter;
1025 bool m = false;
1026 FOR_EACH_SSA_USE_OPERAND (use_p, g, iter, SSA_OP_USE)
1028 tree s = USE_FROM_PTR (use_p);
1029 if (SSA_NAME_DEF_STMT (s)
1030 && gimple_code (SSA_NAME_DEF_STMT (s)) == GIMPLE_PHI
1031 && gimple_bb (SSA_NAME_DEF_STMT (s)) == e->dest)
1033 tree r = gimple_phi_arg_def_from_edge (SSA_NAME_DEF_STMT (s), e);
1034 SET_USE (use_p, unshare_expr (r));
1035 m = true;
1038 if (m)
1039 update_stmt (g);
1042 /* Insert G stmt before ITER and keep ITER pointing to the same statement
1043 as before. If ITER is a returns_twice call, insert it on an appropriate
1044 edge instead. */
1046 void
1047 gsi_safe_insert_before (gimple_stmt_iterator *iter, gimple *g)
1049 gimple *stmt = gsi_stmt (*iter);
1050 if (stmt
1051 && is_gimple_call (stmt)
1052 && (gimple_call_flags (stmt) & ECF_RETURNS_TWICE) != 0
1053 && bb_has_abnormal_pred (gsi_bb (*iter)))
1055 edge e = edge_before_returns_twice_call (gsi_bb (*iter));
1056 basic_block new_bb = gsi_insert_on_edge_immediate (e, g);
1057 if (new_bb)
1058 e = single_succ_edge (new_bb);
1059 adjust_before_returns_twice_call (e, g);
1060 *iter = gsi_for_stmt (stmt);
1062 else
1063 gsi_insert_before (iter, g, GSI_SAME_STMT);
1066 /* Similarly for sequence SEQ. */
1068 void
1069 gsi_safe_insert_seq_before (gimple_stmt_iterator *iter, gimple_seq seq)
1071 if (gimple_seq_empty_p (seq))
1072 return;
1073 gimple *stmt = gsi_stmt (*iter);
1074 if (stmt
1075 && is_gimple_call (stmt)
1076 && (gimple_call_flags (stmt) & ECF_RETURNS_TWICE) != 0
1077 && bb_has_abnormal_pred (gsi_bb (*iter)))
1079 edge e = edge_before_returns_twice_call (gsi_bb (*iter));
1080 gimple *f = gimple_seq_first_stmt (seq);
1081 gimple *l = gimple_seq_last_stmt (seq);
1082 basic_block new_bb = gsi_insert_seq_on_edge_immediate (e, seq);
1083 if (new_bb)
1084 e = single_succ_edge (new_bb);
1085 for (gimple_stmt_iterator gsi = gsi_for_stmt (f); ; gsi_next (&gsi))
1087 gimple *g = gsi_stmt (gsi);
1088 adjust_before_returns_twice_call (e, g);
1089 if (g == l)
1090 break;
1092 *iter = gsi_for_stmt (stmt);
1094 else
1095 gsi_insert_seq_before (iter, seq, GSI_SAME_STMT);