1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2013 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"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
30 #include "gimple-expr.h"
33 #include "gimple-iterator.h"
34 #include "gimple-ssa.h"
37 #include "tree-phinodes.h"
38 #include "ssa-iterators.h"
40 #include "value-prof.h"
43 /* Mark the statement STMT as modified, and update it. */
46 update_modified_stmt (gimple stmt
)
48 if (!ssa_operands_active (cfun
))
50 update_stmt_if_modified (stmt
);
54 /* Mark the statements in SEQ as modified, and update them. */
57 update_modified_stmts (gimple_seq seq
)
59 gimple_stmt_iterator gsi
;
61 if (!ssa_operands_active (cfun
))
63 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
64 update_stmt_if_modified (gsi_stmt (gsi
));
68 /* Set BB to be the basic block for all the statements in the list
69 starting at FIRST and LAST. */
72 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
77 for (n
= first
; n
; n
= n
->next
)
79 gimple_set_bb (n
, bb
);
85 /* Set the frequencies for the cgraph_edges for each of the calls
86 starting at FIRST for their new position within BB. */
89 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
91 struct cgraph_node
*cfun_node
= NULL
;
95 for (n
= first
; n
; n
= n
->next
)
96 if (is_gimple_call (n
))
98 struct cgraph_edge
*e
;
100 /* These function calls are expensive enough that we want
101 to avoid calling them if we never see any calls. */
102 if (cfun_node
== NULL
)
104 cfun_node
= cgraph_get_node (current_function_decl
);
105 bb_freq
= (compute_call_stmt_bb_frequency
106 (current_function_decl
, bb
));
109 e
= cgraph_edge (cfun_node
, n
);
111 e
->frequency
= bb_freq
;
115 /* Insert the sequence delimited by nodes FIRST and LAST before
116 iterator I. M specifies how to update iterator I after insertion
117 (see enum gsi_iterator_update).
119 This routine assumes that there is a forward and backward path
120 between FIRST and LAST (i.e., they are linked in a doubly-linked
121 list). Additionally, if FIRST == LAST, this routine will properly
122 insert a single node. */
125 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
126 gimple_seq_node first
,
127 gimple_seq_node last
,
128 enum gsi_iterator_update mode
)
131 gimple_seq_node cur
= i
->ptr
;
133 gcc_assert (!cur
|| cur
->prev
);
135 if ((bb
= gsi_bb (*i
)) != NULL
)
136 update_bb_for_stmts (first
, last
, bb
);
138 /* Link SEQ before CUR in the sequence. */
141 first
->prev
= cur
->prev
;
142 if (first
->prev
->next
)
143 first
->prev
->next
= first
;
145 gimple_seq_set_first (i
->seq
, first
);
151 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
153 /* If CUR is NULL, we link at the end of the sequence (this case happens
154 when gsi_after_labels is called for a basic block that contains only
155 labels, so it returns an iterator after the end of the block, and
156 we need to insert before it; it might be cleaner to add a flag to the
157 iterator saying whether we are at the start or end of the list). */
161 first
->prev
= itlast
;
162 itlast
->next
= first
;
165 gimple_seq_set_first (i
->seq
, first
);
166 gimple_seq_set_last (i
->seq
, last
);
169 /* Update the iterator, if requested. */
173 case GSI_CONTINUE_LINKING
:
184 /* Inserts the sequence of statements SEQ before the statement pointed
185 by iterator I. MODE indicates what to do with the iterator after
186 insertion (see enum gsi_iterator_update).
188 This function does not scan for new operands. It is provided for
189 the use of the gimplifier, which manipulates statements for which
190 def/use information has not yet been constructed. Most callers
191 should use gsi_insert_seq_before. */
194 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
195 enum gsi_iterator_update mode
)
197 gimple_seq_node first
, last
;
202 /* Don't allow inserting a sequence into itself. */
203 gcc_assert (seq
!= *i
->seq
);
205 first
= gimple_seq_first (seq
);
206 last
= gimple_seq_last (seq
);
208 /* Empty sequences need no work. */
211 gcc_assert (first
== last
);
215 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
219 /* Inserts the sequence of statements SEQ before the statement pointed
220 by iterator I. MODE indicates what to do with the iterator after
221 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
225 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
226 enum gsi_iterator_update mode
)
228 update_modified_stmts (seq
);
229 gsi_insert_seq_before_without_update (i
, seq
, mode
);
233 /* Insert the sequence delimited by nodes FIRST and LAST after
234 iterator I. M specifies how to update iterator I after insertion
235 (see enum gsi_iterator_update).
237 This routine assumes that there is a forward and backward path
238 between FIRST and LAST (i.e., they are linked in a doubly-linked
239 list). Additionally, if FIRST == LAST, this routine will properly
240 insert a single node. */
243 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
244 gimple_seq_node first
,
245 gimple_seq_node last
,
246 enum gsi_iterator_update m
)
249 gimple_seq_node cur
= i
->ptr
;
251 gcc_assert (!cur
|| cur
->prev
);
253 /* If the iterator is inside a basic block, we need to update the
254 basic block information for all the nodes between FIRST and LAST. */
255 if ((bb
= gsi_bb (*i
)) != NULL
)
256 update_bb_for_stmts (first
, last
, bb
);
258 /* Link SEQ after CUR. */
261 last
->next
= cur
->next
;
264 last
->next
->prev
= last
;
267 gimple_seq_set_last (i
->seq
, last
);
273 gcc_assert (!gimple_seq_last (*i
->seq
));
275 gimple_seq_set_first (i
->seq
, first
);
276 gimple_seq_set_last (i
->seq
, last
);
279 /* Update the iterator, if requested. */
285 case GSI_CONTINUE_LINKING
:
297 /* Links sequence SEQ after the statement pointed-to by iterator I.
298 MODE is as in gsi_insert_after.
300 This function does not scan for new operands. It is provided for
301 the use of the gimplifier, which manipulates statements for which
302 def/use information has not yet been constructed. Most callers
303 should use gsi_insert_seq_after. */
306 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
307 enum gsi_iterator_update mode
)
309 gimple_seq_node first
, last
;
314 /* Don't allow inserting a sequence into itself. */
315 gcc_assert (seq
!= *i
->seq
);
317 first
= gimple_seq_first (seq
);
318 last
= gimple_seq_last (seq
);
320 /* Empty sequences need no work. */
323 gcc_assert (first
== last
);
327 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
331 /* Links sequence SEQ after the statement pointed-to by iterator I.
332 MODE is as in gsi_insert_after. Scan the statements in SEQ
336 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
337 enum gsi_iterator_update mode
)
339 update_modified_stmts (seq
);
340 gsi_insert_seq_after_without_update (i
, seq
, mode
);
344 /* Move all statements in the sequence after I to a new sequence.
345 Return this new sequence. */
348 gsi_split_seq_after (gimple_stmt_iterator i
)
350 gimple_seq_node cur
, next
;
351 gimple_seq
*pold_seq
, new_seq
;
355 /* How can we possibly split after the end, or before the beginning? */
356 gcc_assert (cur
&& cur
->next
);
361 gimple_seq_set_first (&new_seq
, next
);
362 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
363 gimple_seq_set_last (pold_seq
, cur
);
370 /* Set the statement to which GSI points to STMT. This only updates
371 the iterator and the gimple sequence, it doesn't do the bookkeeping
375 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
377 gimple orig_stmt
= gsi_stmt (*gsi
);
380 stmt
->next
= next
= orig_stmt
->next
;
381 stmt
->prev
= prev
= orig_stmt
->prev
;
382 /* Note how we don't clear next/prev of orig_stmt. This is so that
383 copies of *GSI our callers might still hold (to orig_stmt)
384 can be advanced as if they too were replaced. */
388 gimple_seq_set_first (gsi
->seq
, stmt
);
392 gimple_seq_set_last (gsi
->seq
, stmt
);
398 /* Move all statements in the sequence before I to a new sequence.
399 Return this new sequence. I is set to the head of the new list. */
402 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
404 gimple_seq_node cur
, prev
;
409 /* How can we possibly split after the end? */
418 /* Set the limits on NEW_SEQ. */
419 gimple_seq_set_first (pnew_seq
, cur
);
420 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
422 /* Cut OLD_SEQ before I. */
423 gimple_seq_set_last (&old_seq
, prev
);
429 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
430 is true, the exception handling information of the original
431 statement is moved to the new statement. Assignments must only be
432 replaced with assignments to the same LHS. */
435 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
437 gimple orig_stmt
= gsi_stmt (*gsi
);
439 if (stmt
== orig_stmt
)
442 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
443 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
445 gimple_set_location (stmt
, gimple_location (orig_stmt
));
446 gimple_set_bb (stmt
, gsi_bb (*gsi
));
448 /* Preserve EH region information from the original statement, if
449 requested by the caller. */
451 maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
453 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
455 /* Free all the data flow information for ORIG_STMT. */
456 gimple_set_bb (orig_stmt
, NULL
);
457 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
458 delink_stmt_imm_use (orig_stmt
);
460 gsi_set_stmt (gsi
, stmt
);
461 gimple_set_modified (stmt
, true);
462 update_modified_stmt (stmt
);
466 /* Replace the statement pointed-to by GSI with the sequence SEQ.
467 If UPDATE_EH_INFO is true, the exception handling information of
468 the original statement is moved to the last statement of the new
469 sequence. If the old statement is an assignment, then so must
470 be the last statement of the new sequence, and they must have the
474 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
477 gimple_stmt_iterator seqi
;
479 if (gimple_seq_empty_p (seq
))
481 gsi_remove (gsi
, true);
484 seqi
= gsi_last (seq
);
485 last
= gsi_stmt (seqi
);
486 gsi_remove (&seqi
, false);
487 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
488 gsi_replace (gsi
, last
, update_eh_info
);
492 /* Insert statement STMT before the statement pointed-to by iterator I.
493 M specifies how to update iterator I after insertion (see enum
494 gsi_iterator_update).
496 This function does not scan for new operands. It is provided for
497 the use of the gimplifier, which manipulates statements for which
498 def/use information has not yet been constructed. Most callers
499 should use gsi_insert_before. */
502 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
503 enum gsi_iterator_update m
)
505 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
508 /* Insert statement STMT before the statement pointed-to by iterator I.
509 Update STMT's basic block and scan it for new operands. M
510 specifies how to update iterator I after insertion (see enum
511 gsi_iterator_update). */
514 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
515 enum gsi_iterator_update m
)
517 update_modified_stmt (stmt
);
518 gsi_insert_before_without_update (i
, stmt
, m
);
522 /* Insert statement STMT after the statement pointed-to by iterator I.
523 M specifies how to update iterator I after insertion (see enum
524 gsi_iterator_update).
526 This function does not scan for new operands. It is provided for
527 the use of the gimplifier, which manipulates statements for which
528 def/use information has not yet been constructed. Most callers
529 should use gsi_insert_after. */
532 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
533 enum gsi_iterator_update m
)
535 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
539 /* Insert statement STMT after the statement pointed-to by iterator I.
540 Update STMT's basic block and scan it for new operands. M
541 specifies how to update iterator I after insertion (see enum
542 gsi_iterator_update). */
545 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
546 enum gsi_iterator_update m
)
548 update_modified_stmt (stmt
);
549 gsi_insert_after_without_update (i
, stmt
, m
);
553 /* Remove the current stmt from the sequence. The iterator is updated
554 to point to the next statement.
556 REMOVE_PERMANENTLY is true when the statement is going to be removed
557 from the IL and not reinserted elsewhere. In that case we remove the
558 statement pointed to by iterator I from the EH tables, and free its
559 operand caches. Otherwise we do not modify this information. Returns
560 true whether EH edge cleanup is required. */
563 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
565 gimple_seq_node cur
, next
, prev
;
566 gimple stmt
= gsi_stmt (*i
);
567 bool require_eh_edge_purge
= false;
569 if (gimple_code (stmt
) != GIMPLE_PHI
)
570 insert_debug_temps_for_defs (i
);
572 /* Free all the data flow information for STMT. */
573 gimple_set_bb (stmt
, NULL
);
574 delink_stmt_imm_use (stmt
);
575 gimple_set_modified (stmt
, true);
577 if (remove_permanently
)
579 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
580 gimple_remove_stmt_histograms (cfun
, stmt
);
583 /* Update the iterator and re-wire the links in I->SEQ. */
587 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
590 /* Cur is not last. */
593 /* Cur is last but not first. */
594 gimple_seq_set_last (i
->seq
, prev
);
597 /* Cur is not first. */
605 return require_eh_edge_purge
;
609 /* Finds iterator for STMT. */
612 gsi_for_stmt (gimple stmt
)
614 gimple_stmt_iterator i
;
615 basic_block bb
= gimple_bb (stmt
);
617 if (gimple_code (stmt
) == GIMPLE_PHI
)
618 i
= gsi_start_phis (bb
);
620 i
= gsi_start_bb (bb
);
627 /* Move the statement at FROM so it comes right after the statement at TO. */
630 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
632 gimple stmt
= gsi_stmt (*from
);
633 gsi_remove (from
, false);
635 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
636 move statements to an empty block. */
637 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
641 /* Move the statement at FROM so it comes right before the statement
645 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
647 gimple stmt
= gsi_stmt (*from
);
648 gsi_remove (from
, false);
650 /* For consistency with gsi_move_after, it might be better to have
651 GSI_NEW_STMT here; however, that breaks several places that expect
652 that TO does not change. */
653 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
657 /* Move the statement at FROM to the end of basic block BB. */
660 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
662 gimple_stmt_iterator last
= gsi_last_bb (bb
);
663 gcc_checking_assert (gsi_bb (last
) == bb
);
665 /* Have to check gsi_end_p because it could be an empty block. */
666 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
667 gsi_move_before (from
, &last
);
669 gsi_move_after (from
, &last
);
673 /* Add STMT to the pending list of edge E. No actual insertion is
674 made until a call to gsi_commit_edge_inserts () is made. */
677 gsi_insert_on_edge (edge e
, gimple stmt
)
679 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
682 /* Add the sequence of statements SEQ to the pending list of edge E.
683 No actual insertion is made until a call to gsi_commit_edge_inserts
687 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
689 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
693 /* Insert the statement pointed-to by GSI into edge E. Every attempt
694 is made to place the statement in an existing basic block, but
695 sometimes that isn't possible. When it isn't possible, the edge is
696 split and the statement is added to the new block.
698 In all cases, the returned *GSI points to the correct location. The
699 return value is true if insertion should be done after the location,
700 or false if it should be done before the location. If a new basic block
701 has to be created, it is stored in *NEW_BB. */
704 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
707 basic_block dest
, src
;
712 /* If the destination has one predecessor which has no PHI nodes,
713 insert there. Except for the exit block.
715 The requirement for no PHI nodes could be relaxed. Basically we
716 would have to examine the PHIs to prove that none of them used
717 the value set by the statement we want to insert on E. That
718 hardly seems worth the effort. */
720 if (single_pred_p (dest
)
721 && gimple_seq_empty_p (phi_nodes (dest
))
722 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
724 *gsi
= gsi_start_bb (dest
);
725 if (gsi_end_p (*gsi
))
728 /* Make sure we insert after any leading labels. */
729 tmp
= gsi_stmt (*gsi
);
730 while (gimple_code (tmp
) == GIMPLE_LABEL
)
733 if (gsi_end_p (*gsi
))
735 tmp
= gsi_stmt (*gsi
);
738 if (gsi_end_p (*gsi
))
740 *gsi
= gsi_last_bb (dest
);
747 /* If the source has one successor, the edge is not abnormal and
748 the last statement does not end a basic block, insert there.
749 Except for the entry block. */
751 if ((e
->flags
& EDGE_ABNORMAL
) == 0
752 && single_succ_p (src
)
753 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
755 *gsi
= gsi_last_bb (src
);
756 if (gsi_end_p (*gsi
))
759 tmp
= gsi_stmt (*gsi
);
760 if (!stmt_ends_bb_p (tmp
))
763 switch (gimple_code (tmp
))
773 /* Otherwise, create a new basic block, and split this edge. */
774 dest
= split_edge (e
);
777 e
= single_pred_edge (dest
);
782 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
783 block has to be created, it is returned. */
786 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
788 gimple_stmt_iterator gsi
;
789 basic_block new_bb
= NULL
;
792 gcc_assert (!PENDING_STMT (e
));
794 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
796 update_call_edge_frequencies (stmt
, gsi
.bb
);
799 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
801 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
806 /* Insert STMTS on edge E. If a new block has to be created, it
810 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
812 gimple_stmt_iterator gsi
;
813 basic_block new_bb
= NULL
;
816 gcc_assert (!PENDING_STMT (e
));
818 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
819 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
822 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
824 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
829 /* This routine will commit all pending edge insertions, creating any new
830 basic blocks which are necessary. */
833 gsi_commit_edge_inserts (void)
839 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
843 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
844 gsi_commit_one_edge_insert (e
, NULL
);
848 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
849 to this block, otherwise set it to NULL. */
852 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
857 if (PENDING_STMT (e
))
859 gimple_stmt_iterator gsi
;
860 gimple_seq seq
= PENDING_STMT (e
);
863 PENDING_STMT (e
) = NULL
;
865 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
866 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
869 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
871 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
875 /* Returns iterator at the start of the list of phi nodes of BB. */
878 gsi_start_phis (basic_block bb
)
880 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
881 return gsi_start_1 (pseq
);