2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / gimple-iterator.c
blobe387c16e31a451e79fedb908a165ca43d62c0565
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. Returns
503 true whether EH edge cleanup is required. */
505 bool
506 gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
508 gimple_seq_node cur, next, prev;
509 gimple stmt = gsi_stmt (*i);
510 bool require_eh_edge_purge = false;
512 if (gimple_code (stmt) != GIMPLE_PHI)
513 insert_debug_temps_for_defs (i);
515 /* Free all the data flow information for STMT. */
516 gimple_set_bb (stmt, NULL);
517 delink_stmt_imm_use (stmt);
518 gimple_set_modified (stmt, true);
520 if (remove_permanently)
522 require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
523 gimple_remove_stmt_histograms (cfun, stmt);
526 /* Update the iterator and re-wire the links in I->SEQ. */
527 cur = i->ptr;
528 next = cur->next;
529 prev = cur->prev;
531 if (prev)
532 prev->next = next;
533 else
534 gimple_seq_set_first (i->seq, next);
536 if (next)
537 next->prev = prev;
538 else
539 gimple_seq_set_last (i->seq, prev);
541 i->ptr = next;
543 return require_eh_edge_purge;
547 /* Finds iterator for STMT. */
549 gimple_stmt_iterator
550 gsi_for_stmt (gimple stmt)
552 gimple_stmt_iterator i;
553 basic_block bb = gimple_bb (stmt);
555 if (gimple_code (stmt) == GIMPLE_PHI)
556 i = gsi_start_phis (bb);
557 else
558 i = gsi_start_bb (bb);
560 for (; !gsi_end_p (i); gsi_next (&i))
561 if (gsi_stmt (i) == stmt)
562 return i;
564 gcc_unreachable ();
568 /* Move the statement at FROM so it comes right after the statement at TO. */
570 void
571 gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
573 gimple stmt = gsi_stmt (*from);
574 gsi_remove (from, false);
576 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
577 move statements to an empty block. */
578 gsi_insert_after (to, stmt, GSI_NEW_STMT);
582 /* Move the statement at FROM so it comes right before the statement
583 at TO. */
585 void
586 gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
588 gimple stmt = gsi_stmt (*from);
589 gsi_remove (from, false);
591 /* For consistency with gsi_move_after, it might be better to have
592 GSI_NEW_STMT here; however, that breaks several places that expect
593 that TO does not change. */
594 gsi_insert_before (to, stmt, GSI_SAME_STMT);
598 /* Move the statement at FROM to the end of basic block BB. */
600 void
601 gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
603 gimple_stmt_iterator last = gsi_last_bb (bb);
604 gcc_checking_assert (gsi_bb (last) == bb);
606 /* Have to check gsi_end_p because it could be an empty block. */
607 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
608 gsi_move_before (from, &last);
609 else
610 gsi_move_after (from, &last);
614 /* Add STMT to the pending list of edge E. No actual insertion is
615 made until a call to gsi_commit_edge_inserts () is made. */
617 void
618 gsi_insert_on_edge (edge e, gimple stmt)
620 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
623 /* Add the sequence of statements SEQ to the pending list of edge E.
624 No actual insertion is made until a call to gsi_commit_edge_inserts
625 is made. */
627 void
628 gsi_insert_seq_on_edge (edge e, gimple_seq seq)
630 gimple_seq_add_seq (&PENDING_STMT (e), seq);
634 /* Insert the statement pointed-to by GSI into edge E. Every attempt
635 is made to place the statement in an existing basic block, but
636 sometimes that isn't possible. When it isn't possible, the edge is
637 split and the statement is added to the new block.
639 In all cases, the returned *GSI points to the correct location. The
640 return value is true if insertion should be done after the location,
641 or false if it should be done before the location. If a new basic block
642 has to be created, it is stored in *NEW_BB. */
644 static bool
645 gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
646 basic_block *new_bb)
648 basic_block dest, src;
649 gimple tmp;
651 dest = e->dest;
653 /* If the destination has one predecessor which has no PHI nodes,
654 insert there. Except for the exit block.
656 The requirement for no PHI nodes could be relaxed. Basically we
657 would have to examine the PHIs to prove that none of them used
658 the value set by the statement we want to insert on E. That
659 hardly seems worth the effort. */
660 restart:
661 if (single_pred_p (dest)
662 && gimple_seq_empty_p (phi_nodes (dest))
663 && dest != EXIT_BLOCK_PTR)
665 *gsi = gsi_start_bb (dest);
666 if (gsi_end_p (*gsi))
667 return true;
669 /* Make sure we insert after any leading labels. */
670 tmp = gsi_stmt (*gsi);
671 while (gimple_code (tmp) == GIMPLE_LABEL)
673 gsi_next (gsi);
674 if (gsi_end_p (*gsi))
675 break;
676 tmp = gsi_stmt (*gsi);
679 if (gsi_end_p (*gsi))
681 *gsi = gsi_last_bb (dest);
682 return true;
684 else
685 return false;
688 /* If the source has one successor, the edge is not abnormal and
689 the last statement does not end a basic block, insert there.
690 Except for the entry block. */
691 src = e->src;
692 if ((e->flags & EDGE_ABNORMAL) == 0
693 && single_succ_p (src)
694 && src != ENTRY_BLOCK_PTR)
696 *gsi = gsi_last_bb (src);
697 if (gsi_end_p (*gsi))
698 return true;
700 tmp = gsi_stmt (*gsi);
701 if (!stmt_ends_bb_p (tmp))
702 return true;
704 switch (gimple_code (tmp))
706 case GIMPLE_RETURN:
707 case GIMPLE_RESX:
708 return false;
709 default:
710 break;
714 /* Otherwise, create a new basic block, and split this edge. */
715 dest = split_edge (e);
716 if (new_bb)
717 *new_bb = dest;
718 e = single_pred_edge (dest);
719 goto restart;
723 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
724 block has to be created, it is returned. */
726 basic_block
727 gsi_insert_on_edge_immediate (edge e, gimple stmt)
729 gimple_stmt_iterator gsi;
730 struct gimple_seq_node_d node;
731 basic_block new_bb = NULL;
732 bool ins_after;
734 gcc_assert (!PENDING_STMT (e));
736 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
738 node.stmt = stmt;
739 node.prev = node.next = NULL;
740 update_call_edge_frequencies (&node, gsi.bb);
742 if (ins_after)
743 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
744 else
745 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
747 return new_bb;
750 /* Insert STMTS on edge E. If a new block has to be created, it
751 is returned. */
753 basic_block
754 gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
756 gimple_stmt_iterator gsi;
757 basic_block new_bb = NULL;
758 bool ins_after;
760 gcc_assert (!PENDING_STMT (e));
762 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
763 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
765 if (ins_after)
766 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
767 else
768 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
770 return new_bb;
773 /* This routine will commit all pending edge insertions, creating any new
774 basic blocks which are necessary. */
776 void
777 gsi_commit_edge_inserts (void)
779 basic_block bb;
780 edge e;
781 edge_iterator ei;
783 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
785 FOR_EACH_BB (bb)
786 FOR_EACH_EDGE (e, ei, bb->succs)
787 gsi_commit_one_edge_insert (e, NULL);
791 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
792 to this block, otherwise set it to NULL. */
794 void
795 gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
797 if (new_bb)
798 *new_bb = NULL;
800 if (PENDING_STMT (e))
802 gimple_stmt_iterator gsi;
803 gimple_seq seq = PENDING_STMT (e);
804 bool ins_after;
806 PENDING_STMT (e) = NULL;
808 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
809 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
811 if (ins_after)
812 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
813 else
814 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
818 /* Returns iterator at the start of the list of phi nodes of BB. */
820 gimple_stmt_iterator
821 gsi_start_phis (basic_block bb)
823 return gsi_start (phi_nodes (bb));