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
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
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/>. */
23 #include "coretypes.h"
27 #include "tree-flow.h"
28 #include "value-prof.h"
31 /* Mark the statement STMT as modified, and update it. */
34 update_modified_stmt (gimple stmt
)
36 if (!ssa_operands_active ())
38 update_stmt_if_modified (stmt
);
42 /* Mark the statements in SEQ as modified, and update them. */
45 update_modified_stmts (gimple_seq seq
)
47 gimple_stmt_iterator gsi
;
49 if (!ssa_operands_active ())
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. */
60 update_bb_for_stmts (gimple_seq_node first
, basic_block bb
)
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. */
72 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
74 struct cgraph_node
*cfun_node
= NULL
;
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_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
);
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. */
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
)
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. */
122 first
->prev
= cur
->prev
;
124 first
->prev
->next
= first
;
126 gimple_seq_set_first (i
->seq
, first
);
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
;
141 itlast
->next
= first
;
143 gimple_seq_set_first (i
->seq
, first
);
144 gimple_seq_set_last (i
->seq
, last
);
147 /* Update the iterator, if requested. */
151 case GSI_CONTINUE_LINKING
:
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. */
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
;
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. */
193 gcc_assert (first
== last
);
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
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. */
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
)
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. */
241 last
->next
= cur
->next
;
243 last
->next
->prev
= last
;
245 gimple_seq_set_last (i
->seq
, last
);
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. */
262 case GSI_CONTINUE_LINKING
:
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. */
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
;
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. */
304 gcc_assert (first
== last
);
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
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. */
329 gsi_split_seq_after (gimple_stmt_iterator i
)
331 gimple_seq_node cur
, next
;
332 gimple_seq old_seq
, new_seq
;
336 /* How can we possibly split after the end, or before the beginning? */
337 gcc_assert (cur
&& cur
->next
);
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
);
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. */
357 gsi_split_seq_before (gimple_stmt_iterator
*i
)
359 gimple_seq_node cur
, prev
;
360 gimple_seq old_seq
, new_seq
;
364 /* How can we possibly split after the end? */
369 new_seq
= gimple_seq_alloc ();
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
);
382 gimple_seq_set_first (old_seq
, NULL
);
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. */
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
)
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. */
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. */
435 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
436 enum gsi_iterator_update m
)
440 n
= ggc_alloc_gimple_seq_node_d ();
441 n
->prev
= n
->next
= NULL
;
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). */
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. */
470 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
471 enum gsi_iterator_update m
)
475 n
= ggc_alloc_gimple_seq_node_d ();
476 n
->prev
= n
->next
= NULL
;
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). */
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. */
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. */
532 gimple_seq_set_first (i
->seq
, next
);
537 gimple_seq_set_last (i
->seq
, prev
);
543 /* Finds iterator for STMT. */
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
);
554 i
= gsi_start_bb (bb
);
556 for (; !gsi_end_p (i
); gsi_next (&i
))
557 if (gsi_stmt (i
) == stmt
)
564 /* Move the statement at FROM so it comes right after the statement at TO. */
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
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. */
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
);
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. */
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
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. */
641 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
644 basic_block dest
, src
;
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. */
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
))
665 /* Make sure we insert after any leading labels. */
666 tmp
= gsi_stmt (*gsi
);
667 while (gimple_code (tmp
) == GIMPLE_LABEL
)
670 if (gsi_end_p (*gsi
))
672 tmp
= gsi_stmt (*gsi
);
675 if (gsi_end_p (*gsi
))
677 *gsi
= gsi_last_bb (dest
);
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. */
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
))
696 tmp
= gsi_stmt (*gsi
);
697 if (!stmt_ends_bb_p (tmp
))
700 switch (gimple_code (tmp
))
710 /* Otherwise, create a new basic block, and split this edge. */
711 dest
= split_edge (e
);
714 e
= single_pred_edge (dest
);
719 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
720 block has to be created, it is returned. */
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
;
730 gcc_assert (!PENDING_STMT (e
));
732 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
735 node
.prev
= node
.next
= NULL
;
736 update_call_edge_frequencies (&node
, gsi
.bb
);
739 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
741 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
746 /* Insert STMTS on edge E. If a new block has to be created, it
750 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
752 gimple_stmt_iterator gsi
;
753 basic_block new_bb
= NULL
;
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
);
762 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
764 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
769 /* This routine will commit all pending edge insertions, creating any new
770 basic blocks which are necessary. */
773 gsi_commit_edge_inserts (void)
779 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR
), NULL
);
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. */
791 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
796 if (PENDING_STMT (e
))
798 gimple_stmt_iterator gsi
;
799 gimple_seq seq
= PENDING_STMT (e
);
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
);
808 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
810 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
814 /* Returns iterator at the start of the list of phi nodes of BB. */
817 gsi_start_phis (basic_block bb
)
819 return gsi_start (phi_nodes (bb
));