1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2015 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"
28 #include "fold-const.h"
30 #include "hard-reg-set.h"
32 #include "dominance.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
38 #include "gimple-expr.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
44 #include "tree-phinodes.h"
45 #include "ssa-iterators.h"
47 #include "value-prof.h"
50 /* Mark the statement STMT as modified, and update it. */
53 update_modified_stmt (gimple stmt
)
55 if (!ssa_operands_active (cfun
))
57 update_stmt_if_modified (stmt
);
61 /* Mark the statements in SEQ as modified, and update them. */
64 update_modified_stmts (gimple_seq seq
)
66 gimple_stmt_iterator gsi
;
68 if (!ssa_operands_active (cfun
))
70 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
71 update_stmt_if_modified (gsi_stmt (gsi
));
75 /* Set BB to be the basic block for all the statements in the list
76 starting at FIRST and LAST. */
79 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
84 for (n
= first
; n
; n
= n
->next
)
86 gimple_set_bb (n
, bb
);
92 /* Set the frequencies for the cgraph_edges for each of the calls
93 starting at FIRST for their new position within BB. */
96 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
98 struct cgraph_node
*cfun_node
= NULL
;
102 for (n
= first
; n
; n
= n
->next
)
103 if (is_gimple_call (n
))
105 struct cgraph_edge
*e
;
107 /* These function calls are expensive enough that we want
108 to avoid calling them if we never see any calls. */
109 if (cfun_node
== NULL
)
111 cfun_node
= cgraph_node::get (current_function_decl
);
112 bb_freq
= (compute_call_stmt_bb_frequency
113 (current_function_decl
, bb
));
116 e
= cfun_node
->get_edge (n
);
118 e
->frequency
= bb_freq
;
122 /* Insert the sequence delimited by nodes FIRST and LAST before
123 iterator I. M specifies how to update iterator I after insertion
124 (see enum gsi_iterator_update).
126 This routine assumes that there is a forward and backward path
127 between FIRST and LAST (i.e., they are linked in a doubly-linked
128 list). Additionally, if FIRST == LAST, this routine will properly
129 insert a single node. */
132 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
133 gimple_seq_node first
,
134 gimple_seq_node last
,
135 enum gsi_iterator_update mode
)
138 gimple_seq_node cur
= i
->ptr
;
140 gcc_assert (!cur
|| cur
->prev
);
142 if ((bb
= gsi_bb (*i
)) != NULL
)
143 update_bb_for_stmts (first
, last
, bb
);
145 /* Link SEQ before CUR in the sequence. */
148 first
->prev
= cur
->prev
;
149 if (first
->prev
->next
)
150 first
->prev
->next
= first
;
152 gimple_seq_set_first (i
->seq
, first
);
158 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
160 /* If CUR is NULL, we link at the end of the sequence (this case happens
161 when gsi_after_labels is called for a basic block that contains only
162 labels, so it returns an iterator after the end of the block, and
163 we need to insert before it; it might be cleaner to add a flag to the
164 iterator saying whether we are at the start or end of the list). */
168 first
->prev
= itlast
;
169 itlast
->next
= first
;
172 gimple_seq_set_first (i
->seq
, first
);
173 gimple_seq_set_last (i
->seq
, last
);
176 /* Update the iterator, if requested. */
180 case GSI_CONTINUE_LINKING
:
191 /* Inserts the sequence of statements SEQ before the statement pointed
192 by iterator I. MODE indicates what to do with the iterator after
193 insertion (see enum gsi_iterator_update).
195 This function does not scan for new operands. It is provided for
196 the use of the gimplifier, which manipulates statements for which
197 def/use information has not yet been constructed. Most callers
198 should use gsi_insert_seq_before. */
201 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
202 enum gsi_iterator_update mode
)
204 gimple_seq_node first
, last
;
209 /* Don't allow inserting a sequence into itself. */
210 gcc_assert (seq
!= *i
->seq
);
212 first
= gimple_seq_first (seq
);
213 last
= gimple_seq_last (seq
);
215 /* Empty sequences need no work. */
218 gcc_assert (first
== last
);
222 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
226 /* Inserts the sequence of statements SEQ before the statement pointed
227 by iterator I. MODE indicates what to do with the iterator after
228 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
232 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
233 enum gsi_iterator_update mode
)
235 update_modified_stmts (seq
);
236 gsi_insert_seq_before_without_update (i
, seq
, mode
);
240 /* Insert the sequence delimited by nodes FIRST and LAST after
241 iterator I. M specifies how to update iterator I after insertion
242 (see enum gsi_iterator_update).
244 This routine assumes that there is a forward and backward path
245 between FIRST and LAST (i.e., they are linked in a doubly-linked
246 list). Additionally, if FIRST == LAST, this routine will properly
247 insert a single node. */
250 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
251 gimple_seq_node first
,
252 gimple_seq_node last
,
253 enum gsi_iterator_update m
)
256 gimple_seq_node cur
= i
->ptr
;
258 gcc_assert (!cur
|| cur
->prev
);
260 /* If the iterator is inside a basic block, we need to update the
261 basic block information for all the nodes between FIRST and LAST. */
262 if ((bb
= gsi_bb (*i
)) != NULL
)
263 update_bb_for_stmts (first
, last
, bb
);
265 /* Link SEQ after CUR. */
268 last
->next
= cur
->next
;
271 last
->next
->prev
= last
;
274 gimple_seq_set_last (i
->seq
, last
);
280 gcc_assert (!gimple_seq_last (*i
->seq
));
282 gimple_seq_set_first (i
->seq
, first
);
283 gimple_seq_set_last (i
->seq
, last
);
286 /* Update the iterator, if requested. */
292 case GSI_CONTINUE_LINKING
:
304 /* Links sequence SEQ after the statement pointed-to by iterator I.
305 MODE is as in gsi_insert_after.
307 This function does not scan for new operands. It is provided for
308 the use of the gimplifier, which manipulates statements for which
309 def/use information has not yet been constructed. Most callers
310 should use gsi_insert_seq_after. */
313 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
314 enum gsi_iterator_update mode
)
316 gimple_seq_node first
, last
;
321 /* Don't allow inserting a sequence into itself. */
322 gcc_assert (seq
!= *i
->seq
);
324 first
= gimple_seq_first (seq
);
325 last
= gimple_seq_last (seq
);
327 /* Empty sequences need no work. */
330 gcc_assert (first
== last
);
334 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
338 /* Links sequence SEQ after the statement pointed-to by iterator I.
339 MODE is as in gsi_insert_after. Scan the statements in SEQ
343 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
344 enum gsi_iterator_update mode
)
346 update_modified_stmts (seq
);
347 gsi_insert_seq_after_without_update (i
, seq
, mode
);
351 /* Move all statements in the sequence after I to a new sequence.
352 Return this new sequence. */
355 gsi_split_seq_after (gimple_stmt_iterator i
)
357 gimple_seq_node cur
, next
;
358 gimple_seq
*pold_seq
, new_seq
;
362 /* How can we possibly split after the end, or before the beginning? */
363 gcc_assert (cur
&& cur
->next
);
368 gimple_seq_set_first (&new_seq
, next
);
369 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
370 gimple_seq_set_last (pold_seq
, cur
);
377 /* Set the statement to which GSI points to STMT. This only updates
378 the iterator and the gimple sequence, it doesn't do the bookkeeping
382 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
384 gimple orig_stmt
= gsi_stmt (*gsi
);
387 stmt
->next
= next
= orig_stmt
->next
;
388 stmt
->prev
= prev
= orig_stmt
->prev
;
389 /* Note how we don't clear next/prev of orig_stmt. This is so that
390 copies of *GSI our callers might still hold (to orig_stmt)
391 can be advanced as if they too were replaced. */
395 gimple_seq_set_first (gsi
->seq
, stmt
);
399 gimple_seq_set_last (gsi
->seq
, stmt
);
405 /* Move all statements in the sequence before I to a new sequence.
406 Return this new sequence. I is set to the head of the new list. */
409 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
411 gimple_seq_node cur
, prev
;
416 /* How can we possibly split after the end? */
425 /* Set the limits on NEW_SEQ. */
426 gimple_seq_set_first (pnew_seq
, cur
);
427 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
429 /* Cut OLD_SEQ before I. */
430 gimple_seq_set_last (&old_seq
, prev
);
436 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
437 is true, the exception handling information of the original
438 statement is moved to the new statement. Assignments must only be
439 replaced with assignments to the same LHS. Returns whether EH edge
440 cleanup is required. */
443 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
445 gimple orig_stmt
= gsi_stmt (*gsi
);
446 bool require_eh_edge_purge
= false;
448 if (stmt
== orig_stmt
)
451 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
452 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
454 gimple_set_location (stmt
, gimple_location (orig_stmt
));
455 gimple_set_bb (stmt
, gsi_bb (*gsi
));
457 /* Preserve EH region information from the original statement, if
458 requested by the caller. */
460 require_eh_edge_purge
= maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
462 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
464 /* Free all the data flow information for ORIG_STMT. */
465 gimple_set_bb (orig_stmt
, NULL
);
466 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
467 delink_stmt_imm_use (orig_stmt
);
469 gsi_set_stmt (gsi
, stmt
);
470 gimple_set_modified (stmt
, true);
471 update_modified_stmt (stmt
);
472 return require_eh_edge_purge
;
476 /* Replace the statement pointed-to by GSI with the sequence SEQ.
477 If UPDATE_EH_INFO is true, the exception handling information of
478 the original statement is moved to the last statement of the new
479 sequence. If the old statement is an assignment, then so must
480 be the last statement of the new sequence, and they must have the
484 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
487 gimple_stmt_iterator seqi
;
489 if (gimple_seq_empty_p (seq
))
491 gsi_remove (gsi
, true);
494 seqi
= gsi_last (seq
);
495 last
= gsi_stmt (seqi
);
496 gsi_remove (&seqi
, false);
497 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
498 gsi_replace (gsi
, last
, update_eh_info
);
502 /* Insert statement STMT before the statement pointed-to by iterator I.
503 M specifies how to update iterator I after insertion (see enum
504 gsi_iterator_update).
506 This function does not scan for new operands. It is provided for
507 the use of the gimplifier, which manipulates statements for which
508 def/use information has not yet been constructed. Most callers
509 should use gsi_insert_before. */
512 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
513 enum gsi_iterator_update m
)
515 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
518 /* Insert statement STMT before the statement pointed-to by iterator I.
519 Update STMT's basic block and scan it for new operands. M
520 specifies how to update iterator I after insertion (see enum
521 gsi_iterator_update). */
524 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
525 enum gsi_iterator_update m
)
527 update_modified_stmt (stmt
);
528 gsi_insert_before_without_update (i
, stmt
, m
);
532 /* Insert statement STMT after the statement pointed-to by iterator I.
533 M specifies how to update iterator I after insertion (see enum
534 gsi_iterator_update).
536 This function does not scan for new operands. It is provided for
537 the use of the gimplifier, which manipulates statements for which
538 def/use information has not yet been constructed. Most callers
539 should use gsi_insert_after. */
542 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
543 enum gsi_iterator_update m
)
545 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
549 /* Insert statement STMT after the statement pointed-to by iterator I.
550 Update STMT's basic block and scan it for new operands. M
551 specifies how to update iterator I after insertion (see enum
552 gsi_iterator_update). */
555 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
556 enum gsi_iterator_update m
)
558 update_modified_stmt (stmt
);
559 gsi_insert_after_without_update (i
, stmt
, m
);
563 /* Remove the current stmt from the sequence. The iterator is updated
564 to point to the next statement.
566 REMOVE_PERMANENTLY is true when the statement is going to be removed
567 from the IL and not reinserted elsewhere. In that case we remove the
568 statement pointed to by iterator I from the EH tables, and free its
569 operand caches. Otherwise we do not modify this information. Returns
570 true whether EH edge cleanup is required. */
573 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
575 gimple_seq_node cur
, next
, prev
;
576 gimple stmt
= gsi_stmt (*i
);
577 bool require_eh_edge_purge
= false;
579 if (gimple_code (stmt
) != GIMPLE_PHI
)
580 insert_debug_temps_for_defs (i
);
582 /* Free all the data flow information for STMT. */
583 gimple_set_bb (stmt
, NULL
);
584 delink_stmt_imm_use (stmt
);
585 gimple_set_modified (stmt
, true);
587 if (remove_permanently
)
589 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
590 gimple_remove_stmt_histograms (cfun
, stmt
);
593 /* Update the iterator and re-wire the links in I->SEQ. */
597 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
600 /* Cur is not last. */
603 /* Cur is last but not first. */
604 gimple_seq_set_last (i
->seq
, prev
);
607 /* Cur is not first. */
615 return require_eh_edge_purge
;
619 /* Finds iterator for STMT. */
622 gsi_for_stmt (gimple stmt
)
624 gimple_stmt_iterator i
;
625 basic_block bb
= gimple_bb (stmt
);
627 if (gimple_code (stmt
) == GIMPLE_PHI
)
628 i
= gsi_start_phis (bb
);
630 i
= gsi_start_bb (bb
);
636 /* Finds iterator for PHI. */
639 gsi_for_phi (gphi
*phi
)
642 basic_block bb
= gimple_bb (phi
);
644 i
= gsi_start_phis (bb
);
650 /* Move the statement at FROM so it comes right after the statement at TO. */
653 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
655 gimple stmt
= gsi_stmt (*from
);
656 gsi_remove (from
, false);
658 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
659 move statements to an empty block. */
660 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
664 /* Move the statement at FROM so it comes right before the statement
668 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
670 gimple stmt
= gsi_stmt (*from
);
671 gsi_remove (from
, false);
673 /* For consistency with gsi_move_after, it might be better to have
674 GSI_NEW_STMT here; however, that breaks several places that expect
675 that TO does not change. */
676 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
680 /* Move the statement at FROM to the end of basic block BB. */
683 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
685 gimple_stmt_iterator last
= gsi_last_bb (bb
);
686 gcc_checking_assert (gsi_bb (last
) == bb
);
688 /* Have to check gsi_end_p because it could be an empty block. */
689 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
690 gsi_move_before (from
, &last
);
692 gsi_move_after (from
, &last
);
696 /* Add STMT to the pending list of edge E. No actual insertion is
697 made until a call to gsi_commit_edge_inserts () is made. */
700 gsi_insert_on_edge (edge e
, gimple stmt
)
702 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
705 /* Add the sequence of statements SEQ to the pending list of edge E.
706 No actual insertion is made until a call to gsi_commit_edge_inserts
710 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
712 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
715 /* Return a new iterator pointing to the first statement in sequence of
716 statements on edge E. Such statements need to be subsequently moved into a
717 basic block by calling gsi_commit_edge_inserts. */
720 gsi_start_edge (edge e
)
722 return gsi_start (PENDING_STMT (e
));
725 /* Insert the statement pointed-to by GSI into edge E. Every attempt
726 is made to place the statement in an existing basic block, but
727 sometimes that isn't possible. When it isn't possible, the edge is
728 split and the statement is added to the new block.
730 In all cases, the returned *GSI points to the correct location. The
731 return value is true if insertion should be done after the location,
732 or false if it should be done before the location. If a new basic block
733 has to be created, it is stored in *NEW_BB. */
736 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
739 basic_block dest
, src
;
744 /* If the destination has one predecessor which has no PHI nodes,
745 insert there. Except for the exit block.
747 The requirement for no PHI nodes could be relaxed. Basically we
748 would have to examine the PHIs to prove that none of them used
749 the value set by the statement we want to insert on E. That
750 hardly seems worth the effort. */
752 if (single_pred_p (dest
)
753 && gimple_seq_empty_p (phi_nodes (dest
))
754 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
756 *gsi
= gsi_start_bb (dest
);
757 if (gsi_end_p (*gsi
))
760 /* Make sure we insert after any leading labels. */
761 tmp
= gsi_stmt (*gsi
);
762 while (gimple_code (tmp
) == GIMPLE_LABEL
)
765 if (gsi_end_p (*gsi
))
767 tmp
= gsi_stmt (*gsi
);
770 if (gsi_end_p (*gsi
))
772 *gsi
= gsi_last_bb (dest
);
779 /* If the source has one successor, the edge is not abnormal and
780 the last statement does not end a basic block, insert there.
781 Except for the entry block. */
783 if ((e
->flags
& EDGE_ABNORMAL
) == 0
784 && single_succ_p (src
)
785 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
787 *gsi
= gsi_last_bb (src
);
788 if (gsi_end_p (*gsi
))
791 tmp
= gsi_stmt (*gsi
);
792 if (!stmt_ends_bb_p (tmp
))
795 switch (gimple_code (tmp
))
805 /* Otherwise, create a new basic block, and split this edge. */
806 dest
= split_edge (e
);
809 e
= single_pred_edge (dest
);
814 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
815 block has to be created, it is returned. */
818 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
820 gimple_stmt_iterator gsi
;
821 basic_block new_bb
= NULL
;
824 gcc_assert (!PENDING_STMT (e
));
826 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
828 update_call_edge_frequencies (stmt
, gsi
.bb
);
831 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
833 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
838 /* Insert STMTS on edge E. If a new block has to be created, it
842 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
844 gimple_stmt_iterator gsi
;
845 basic_block new_bb
= NULL
;
848 gcc_assert (!PENDING_STMT (e
));
850 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
851 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
854 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
856 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
861 /* This routine will commit all pending edge insertions, creating any new
862 basic blocks which are necessary. */
865 gsi_commit_edge_inserts (void)
871 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
874 FOR_EACH_BB_FN (bb
, cfun
)
875 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
876 gsi_commit_one_edge_insert (e
, NULL
);
880 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
881 to this block, otherwise set it to NULL. */
884 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
889 if (PENDING_STMT (e
))
891 gimple_stmt_iterator gsi
;
892 gimple_seq seq
= PENDING_STMT (e
);
895 PENDING_STMT (e
) = NULL
;
897 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
898 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
901 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
903 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
907 /* Returns iterator at the start of the list of phi nodes of BB. */
910 gsi_start_phis (basic_block bb
)
912 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
914 /* Adapted from gsi_start_1. */
917 i
.ptr
= gimple_seq_first (*pseq
);
919 i
.bb
= i
.ptr
? gimple_bb (i
.ptr
) : NULL
;