Fix typo in my last change
[official-gcc.git] / gcc / gimple-iterator.c
blobf5a1d26dc276964e413cc92ae3e1ca0cf6f4f643
1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007, 2008, 2010 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldy@quesejoda.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-flow.h"
28 #include "value-prof.h"
31 /* Mark the statement STMT as modified, and update it. */
33 static inline void
34 update_modified_stmt (gimple stmt)
36 if (!ssa_operands_active ())
37 return;
38 update_stmt_if_modified (stmt);
42 /* Mark the statements in SEQ as modified, and update them. */
44 static void
45 update_modified_stmts (gimple_seq seq)
47 gimple_stmt_iterator gsi;
49 if (!ssa_operands_active ())
50 return;
51 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
52 update_stmt_if_modified (gsi_stmt (gsi));
56 /* Set BB to be the basic block for all the statements in the list
57 starting at FIRST and LAST. */
59 static void
60 update_bb_for_stmts (gimple_seq_node first, basic_block bb)
62 gimple_seq_node n;
64 for (n = first; n; n = n->next)
65 gimple_set_bb (n->stmt, bb);
68 /* Set the frequencies for the cgraph_edges for each of the calls
69 starting at FIRST for their new position within BB. */
71 static void
72 update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
74 struct cgraph_node *cfun_node = NULL;
75 int bb_freq = 0;
76 gimple_seq_node n;
78 for (n = first; n ; n = n->next)
79 if (is_gimple_call (n->stmt))
81 struct cgraph_edge *e;
83 /* These function calls are expensive enough that we want
84 to avoid calling them if we never see any calls. */
85 if (cfun_node == NULL)
87 cfun_node = cgraph_get_node (current_function_decl);
88 bb_freq = (compute_call_stmt_bb_frequency
89 (current_function_decl, bb));
92 e = cgraph_edge (cfun_node, n->stmt);
93 if (e != NULL)
94 e->frequency = bb_freq;
98 /* Insert the sequence delimited by nodes FIRST and LAST before
99 iterator I. M specifies how to update iterator I after insertion
100 (see enum gsi_iterator_update).
102 This routine assumes that there is a forward and backward path
103 between FIRST and LAST (i.e., they are linked in a doubly-linked
104 list). Additionally, if FIRST == LAST, this routine will properly
105 insert a single node. */
107 static void
108 gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
109 gimple_seq_node first,
110 gimple_seq_node last,
111 enum gsi_iterator_update mode)
113 basic_block bb;
114 gimple_seq_node cur = i->ptr;
116 if ((bb = gsi_bb (*i)) != NULL)
117 update_bb_for_stmts (first, bb);
119 /* Link SEQ before CUR in the sequence. */
120 if (cur)
122 first->prev = cur->prev;
123 if (first->prev)
124 first->prev->next = first;
125 else
126 gimple_seq_set_first (i->seq, first);
127 last->next = cur;
128 cur->prev = last;
130 else
132 gimple_seq_node itlast = gimple_seq_last (i->seq);
134 /* If CUR is NULL, we link at the end of the sequence (this case happens
135 when gsi_after_labels is called for a basic block that contains only
136 labels, so it returns an iterator after the end of the block, and
137 we need to insert before it; it might be cleaner to add a flag to the
138 iterator saying whether we are at the start or end of the list). */
139 first->prev = itlast;
140 if (itlast)
141 itlast->next = first;
142 else
143 gimple_seq_set_first (i->seq, first);
144 gimple_seq_set_last (i->seq, last);
147 /* Update the iterator, if requested. */
148 switch (mode)
150 case GSI_NEW_STMT:
151 case GSI_CONTINUE_LINKING:
152 i->ptr = first;
153 break;
154 case GSI_SAME_STMT:
155 break;
156 default:
157 gcc_unreachable ();
162 /* Inserts the sequence of statements SEQ before the statement pointed
163 by iterator I. MODE indicates what to do with the iterator after
164 insertion (see enum gsi_iterator_update).
166 This function does not scan for new operands. It is provided for
167 the use of the gimplifier, which manipulates statements for which
168 def/use information has not yet been constructed. Most callers
169 should use gsi_insert_seq_before. */
171 void
172 gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
173 enum gsi_iterator_update mode)
175 gimple_seq_node first, last;
177 if (seq == NULL)
178 return;
180 /* Don't allow inserting a sequence into itself. */
181 gcc_assert (seq != i->seq);
183 first = gimple_seq_first (seq);
184 last = gimple_seq_last (seq);
186 gimple_seq_set_first (seq, NULL);
187 gimple_seq_set_last (seq, NULL);
188 gimple_seq_free (seq);
190 /* Empty sequences need no work. */
191 if (!first || !last)
193 gcc_assert (first == last);
194 return;
197 gsi_insert_seq_nodes_before (i, first, last, mode);
201 /* Inserts the sequence of statements SEQ before the statement pointed
202 by iterator I. MODE indicates what to do with the iterator after
203 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
204 for new operands. */
206 void
207 gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
208 enum gsi_iterator_update mode)
210 update_modified_stmts (seq);
211 gsi_insert_seq_before_without_update (i, seq, mode);
215 /* Insert the sequence delimited by nodes FIRST and LAST after
216 iterator I. M specifies how to update iterator I after insertion
217 (see enum gsi_iterator_update).
219 This routine assumes that there is a forward and backward path
220 between FIRST and LAST (i.e., they are linked in a doubly-linked
221 list). Additionally, if FIRST == LAST, this routine will properly
222 insert a single node. */
224 static void
225 gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
226 gimple_seq_node first,
227 gimple_seq_node last,
228 enum gsi_iterator_update m)
230 basic_block bb;
231 gimple_seq_node cur = i->ptr;
233 /* If the iterator is inside a basic block, we need to update the
234 basic block information for all the nodes between FIRST and LAST. */
235 if ((bb = gsi_bb (*i)) != NULL)
236 update_bb_for_stmts (first, bb);
238 /* Link SEQ after CUR. */
239 if (cur)
241 last->next = cur->next;
242 if (last->next)
243 last->next->prev = last;
244 else
245 gimple_seq_set_last (i->seq, last);
246 first->prev = cur;
247 cur->next = first;
249 else
251 gcc_assert (!gimple_seq_last (i->seq));
252 gimple_seq_set_first (i->seq, first);
253 gimple_seq_set_last (i->seq, last);
256 /* Update the iterator, if requested. */
257 switch (m)
259 case GSI_NEW_STMT:
260 i->ptr = first;
261 break;
262 case GSI_CONTINUE_LINKING:
263 i->ptr = last;
264 break;
265 case GSI_SAME_STMT:
266 gcc_assert (cur);
267 break;
268 default:
269 gcc_unreachable ();
274 /* Links sequence SEQ after the statement pointed-to by iterator I.
275 MODE is as in gsi_insert_after.
277 This function does not scan for new operands. It is provided for
278 the use of the gimplifier, which manipulates statements for which
279 def/use information has not yet been constructed. Most callers
280 should use gsi_insert_seq_after. */
282 void
283 gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
284 enum gsi_iterator_update mode)
286 gimple_seq_node first, last;
288 if (seq == NULL)
289 return;
291 /* Don't allow inserting a sequence into itself. */
292 gcc_assert (seq != i->seq);
294 first = gimple_seq_first (seq);
295 last = gimple_seq_last (seq);
297 gimple_seq_set_first (seq, NULL);
298 gimple_seq_set_last (seq, NULL);
299 gimple_seq_free (seq);
301 /* Empty sequences need no work. */
302 if (!first || !last)
304 gcc_assert (first == last);
305 return;
308 gsi_insert_seq_nodes_after (i, first, last, mode);
312 /* Links sequence SEQ after the statement pointed-to by iterator I.
313 MODE is as in gsi_insert_after. Scan the statements in SEQ
314 for new operands. */
316 void
317 gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
318 enum gsi_iterator_update mode)
320 update_modified_stmts (seq);
321 gsi_insert_seq_after_without_update (i, seq, mode);
325 /* Move all statements in the sequence after I to a new sequence.
326 Return this new sequence. */
328 gimple_seq
329 gsi_split_seq_after (gimple_stmt_iterator i)
331 gimple_seq_node cur, next;
332 gimple_seq old_seq, new_seq;
334 cur = i.ptr;
336 /* How can we possibly split after the end, or before the beginning? */
337 gcc_assert (cur && cur->next);
338 next = cur->next;
340 old_seq = i.seq;
341 new_seq = gimple_seq_alloc ();
343 gimple_seq_set_first (new_seq, next);
344 gimple_seq_set_last (new_seq, gimple_seq_last (old_seq));
345 gimple_seq_set_last (old_seq, cur);
346 cur->next = NULL;
347 next->prev = NULL;
349 return new_seq;
353 /* Move all statements in the sequence before I to a new sequence.
354 Return this new sequence. I is set to the head of the new list. */
356 gimple_seq
357 gsi_split_seq_before (gimple_stmt_iterator *i)
359 gimple_seq_node cur, prev;
360 gimple_seq old_seq, new_seq;
362 cur = i->ptr;
364 /* How can we possibly split after the end? */
365 gcc_assert (cur);
366 prev = cur->prev;
368 old_seq = i->seq;
369 new_seq = gimple_seq_alloc ();
370 i->seq = new_seq;
372 /* Set the limits on NEW_SEQ. */
373 gimple_seq_set_first (new_seq, cur);
374 gimple_seq_set_last (new_seq, gimple_seq_last (old_seq));
376 /* Cut OLD_SEQ before I. */
377 gimple_seq_set_last (old_seq, prev);
378 cur->prev = NULL;
379 if (prev)
380 prev->next = NULL;
381 else
382 gimple_seq_set_first (old_seq, NULL);
384 return new_seq;
388 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
389 is true, the exception handling information of the original
390 statement is moved to the new statement. Assignments must only be
391 replaced with assignments to the same LHS. */
393 void
394 gsi_replace (gimple_stmt_iterator *gsi, gimple stmt, bool update_eh_info)
396 gimple orig_stmt = gsi_stmt (*gsi);
398 if (stmt == orig_stmt)
399 return;
401 gcc_assert (!gimple_has_lhs (orig_stmt)
402 || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
404 gimple_set_location (stmt, gimple_location (orig_stmt));
405 gimple_set_bb (stmt, gsi_bb (*gsi));
407 /* Preserve EH region information from the original statement, if
408 requested by the caller. */
409 if (update_eh_info)
410 maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
412 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
414 /* Free all the data flow information for ORIG_STMT. */
415 gimple_set_bb (orig_stmt, NULL);
416 gimple_remove_stmt_histograms (cfun, orig_stmt);
417 delink_stmt_imm_use (orig_stmt);
419 *gsi_stmt_ptr (gsi) = stmt;
420 gimple_set_modified (stmt, true);
421 update_modified_stmt (stmt);
425 /* Insert statement STMT before the statement pointed-to by iterator I.
426 M specifies how to update iterator I after insertion (see enum
427 gsi_iterator_update).
429 This function does not scan for new operands. It is provided for
430 the use of the gimplifier, which manipulates statements for which
431 def/use information has not yet been constructed. Most callers
432 should use gsi_insert_before. */
434 void
435 gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple stmt,
436 enum gsi_iterator_update m)
438 gimple_seq_node n;
440 n = ggc_alloc_gimple_seq_node_d ();
441 n->prev = n->next = NULL;
442 n->stmt = stmt;
443 gsi_insert_seq_nodes_before (i, n, n, m);
446 /* Insert statement STMT before the statement pointed-to by iterator I.
447 Update STMT's basic block and scan it for new operands. M
448 specifies how to update iterator I after insertion (see enum
449 gsi_iterator_update). */
451 void
452 gsi_insert_before (gimple_stmt_iterator *i, gimple stmt,
453 enum gsi_iterator_update m)
455 update_modified_stmt (stmt);
456 gsi_insert_before_without_update (i, stmt, m);
460 /* Insert statement STMT after the statement pointed-to by iterator I.
461 M specifies how to update iterator I after insertion (see enum
462 gsi_iterator_update).
464 This function does not scan for new operands. It is provided for
465 the use of the gimplifier, which manipulates statements for which
466 def/use information has not yet been constructed. Most callers
467 should use gsi_insert_after. */
469 void
470 gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple stmt,
471 enum gsi_iterator_update m)
473 gimple_seq_node n;
475 n = ggc_alloc_gimple_seq_node_d ();
476 n->prev = n->next = NULL;
477 n->stmt = stmt;
478 gsi_insert_seq_nodes_after (i, n, n, m);
482 /* Insert statement STMT after the statement pointed-to by iterator I.
483 Update STMT's basic block and scan it for new operands. M
484 specifies how to update iterator I after insertion (see enum
485 gsi_iterator_update). */
487 void
488 gsi_insert_after (gimple_stmt_iterator *i, gimple stmt,
489 enum gsi_iterator_update m)
491 update_modified_stmt (stmt);
492 gsi_insert_after_without_update (i, stmt, m);
496 /* Remove the current stmt from the sequence. The iterator is updated
497 to point to the next statement.
499 REMOVE_PERMANENTLY is true when the statement is going to be removed
500 from the IL and not reinserted elsewhere. In that case we remove the
501 statement pointed to by iterator I from the EH tables, and free its
502 operand caches. Otherwise we do not modify this information. */
504 void
505 gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
507 gimple_seq_node cur, next, prev;
508 gimple stmt = gsi_stmt (*i);
510 if (gimple_code (stmt) != GIMPLE_PHI)
511 insert_debug_temps_for_defs (i);
513 /* Free all the data flow information for STMT. */
514 gimple_set_bb (stmt, NULL);
515 delink_stmt_imm_use (stmt);
516 gimple_set_modified (stmt, true);
518 if (remove_permanently)
520 remove_stmt_from_eh_lp (stmt);
521 gimple_remove_stmt_histograms (cfun, stmt);
524 /* Update the iterator and re-wire the links in I->SEQ. */
525 cur = i->ptr;
526 next = cur->next;
527 prev = cur->prev;
529 if (prev)
530 prev->next = next;
531 else
532 gimple_seq_set_first (i->seq, next);
534 if (next)
535 next->prev = prev;
536 else
537 gimple_seq_set_last (i->seq, prev);
539 i->ptr = next;
543 /* Finds iterator for STMT. */
545 gimple_stmt_iterator
546 gsi_for_stmt (gimple stmt)
548 gimple_stmt_iterator i;
549 basic_block bb = gimple_bb (stmt);
551 if (gimple_code (stmt) == GIMPLE_PHI)
552 i = gsi_start_phis (bb);
553 else
554 i = gsi_start_bb (bb);
556 for (; !gsi_end_p (i); gsi_next (&i))
557 if (gsi_stmt (i) == stmt)
558 return i;
560 gcc_unreachable ();
564 /* Move the statement at FROM so it comes right after the statement at TO. */
566 void
567 gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
569 gimple stmt = gsi_stmt (*from);
570 gsi_remove (from, false);
572 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
573 move statements to an empty block. */
574 gsi_insert_after (to, stmt, GSI_NEW_STMT);
578 /* Move the statement at FROM so it comes right before the statement
579 at TO. */
581 void
582 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
584 gimple stmt = gsi_stmt (*from);
585 gsi_remove (from, false);
587 /* For consistency with gsi_move_after, it might be better to have
588 GSI_NEW_STMT here; however, that breaks several places that expect
589 that TO does not change. */
590 gsi_insert_before (to, stmt, GSI_SAME_STMT);
594 /* Move the statement at FROM to the end of basic block BB. */
596 void
597 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
599 gimple_stmt_iterator last = gsi_last_bb (bb);
600 gcc_checking_assert (gsi_bb (last) == bb);
602 /* Have to check gsi_end_p because it could be an empty block. */
603 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
604 gsi_move_before (from, &last);
605 else
606 gsi_move_after (from, &last);
610 /* Add STMT to the pending list of edge E. No actual insertion is
611 made until a call to gsi_commit_edge_inserts () is made. */
613 void
614 gsi_insert_on_edge (edge e, gimple stmt)
616 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
619 /* Add the sequence of statements SEQ to the pending list of edge E.
620 No actual insertion is made until a call to gsi_commit_edge_inserts
621 is made. */
623 void
624 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
626 gimple_seq_add_seq (&PENDING_STMT (e), seq);
630 /* Insert the statement pointed-to by GSI into edge E. Every attempt
631 is made to place the statement in an existing basic block, but
632 sometimes that isn't possible. When it isn't possible, the edge is
633 split and the statement is added to the new block.
635 In all cases, the returned *GSI points to the correct location. The
636 return value is true if insertion should be done after the location,
637 or false if it should be done before the location. If a new basic block
638 has to be created, it is stored in *NEW_BB. */
640 static bool
641 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
642 basic_block *new_bb)
644 basic_block dest, src;
645 gimple tmp;
647 dest = e->dest;
649 /* If the destination has one predecessor which has no PHI nodes,
650 insert there. Except for the exit block.
652 The requirement for no PHI nodes could be relaxed. Basically we
653 would have to examine the PHIs to prove that none of them used
654 the value set by the statement we want to insert on E. That
655 hardly seems worth the effort. */
656 restart:
657 if (single_pred_p (dest)
658 && gimple_seq_empty_p (phi_nodes (dest))
659 && dest != EXIT_BLOCK_PTR)
661 *gsi = gsi_start_bb (dest);
662 if (gsi_end_p (*gsi))
663 return true;
665 /* Make sure we insert after any leading labels. */
666 tmp = gsi_stmt (*gsi);
667 while (gimple_code (tmp) == GIMPLE_LABEL)
669 gsi_next (gsi);
670 if (gsi_end_p (*gsi))
671 break;
672 tmp = gsi_stmt (*gsi);
675 if (gsi_end_p (*gsi))
677 *gsi = gsi_last_bb (dest);
678 return true;
680 else
681 return false;
684 /* If the source has one successor, the edge is not abnormal and
685 the last statement does not end a basic block, insert there.
686 Except for the entry block. */
687 src = e->src;
688 if ((e->flags & EDGE_ABNORMAL) == 0
689 && single_succ_p (src)
690 && src != ENTRY_BLOCK_PTR)
692 *gsi = gsi_last_bb (src);
693 if (gsi_end_p (*gsi))
694 return true;
696 tmp = gsi_stmt (*gsi);
697 if (!stmt_ends_bb_p (tmp))
698 return true;
700 switch (gimple_code (tmp))
702 case GIMPLE_RETURN:
703 case GIMPLE_RESX:
704 return false;
705 default:
706 break;
710 /* Otherwise, create a new basic block, and split this edge. */
711 dest = split_edge (e);
712 if (new_bb)
713 *new_bb = dest;
714 e = single_pred_edge (dest);
715 goto restart;
719 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
720 block has to be created, it is returned. */
722 basic_block
723 gsi_insert_on_edge_immediate (edge e, gimple stmt)
725 gimple_stmt_iterator gsi;
726 struct gimple_seq_node_d node;
727 basic_block new_bb = NULL;
728 bool ins_after;
730 gcc_assert (!PENDING_STMT (e));
732 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
734 node.stmt = stmt;
735 node.prev = node.next = NULL;
736 update_call_edge_frequencies (&node, gsi.bb);
738 if (ins_after)
739 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
740 else
741 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
743 return new_bb;
746 /* Insert STMTS on edge E. If a new block has to be created, it
747 is returned. */
749 basic_block
750 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
752 gimple_stmt_iterator gsi;
753 basic_block new_bb = NULL;
754 bool ins_after;
756 gcc_assert (!PENDING_STMT (e));
758 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
759 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
761 if (ins_after)
762 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
763 else
764 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
766 return new_bb;
769 /* This routine will commit all pending edge insertions, creating any new
770 basic blocks which are necessary. */
772 void
773 gsi_commit_edge_inserts (void)
775 basic_block bb;
776 edge e;
777 edge_iterator ei;
779 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
781 FOR_EACH_BB (bb)
782 FOR_EACH_EDGE (e, ei, bb->succs)
783 gsi_commit_one_edge_insert (e, NULL);
787 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
788 to this block, otherwise set it to NULL. */
790 void
791 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
793 if (new_bb)
794 *new_bb = NULL;
796 if (PENDING_STMT (e))
798 gimple_stmt_iterator gsi;
799 gimple_seq seq = PENDING_STMT (e);
800 bool ins_after;
802 PENDING_STMT (e) = NULL;
804 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
805 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
807 if (ins_after)
808 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
809 else
810 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
814 /* Returns iterator at the start of the list of phi nodes of BB. */
816 gimple_stmt_iterator
817 gsi_start_phis (basic_block bb)
819 return gsi_start (phi_nodes (bb));