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"
42 #include "plugin-api.h"
46 #include "tree-phinodes.h"
47 #include "ssa-iterators.h"
49 #include "value-prof.h"
52 /* Mark the statement STMT as modified, and update it. */
55 update_modified_stmt (gimple stmt
)
57 if (!ssa_operands_active (cfun
))
59 update_stmt_if_modified (stmt
);
63 /* Mark the statements in SEQ as modified, and update them. */
66 update_modified_stmts (gimple_seq seq
)
68 gimple_stmt_iterator gsi
;
70 if (!ssa_operands_active (cfun
))
72 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
73 update_stmt_if_modified (gsi_stmt (gsi
));
77 /* Set BB to be the basic block for all the statements in the list
78 starting at FIRST and LAST. */
81 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
86 for (n
= first
; n
; n
= n
->next
)
88 gimple_set_bb (n
, bb
);
94 /* Set the frequencies for the cgraph_edges for each of the calls
95 starting at FIRST for their new position within BB. */
98 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
100 struct cgraph_node
*cfun_node
= NULL
;
104 for (n
= first
; n
; n
= n
->next
)
105 if (is_gimple_call (n
))
107 struct cgraph_edge
*e
;
109 /* These function calls are expensive enough that we want
110 to avoid calling them if we never see any calls. */
111 if (cfun_node
== NULL
)
113 cfun_node
= cgraph_node::get (current_function_decl
);
114 bb_freq
= (compute_call_stmt_bb_frequency
115 (current_function_decl
, bb
));
118 e
= cfun_node
->get_edge (n
);
120 e
->frequency
= bb_freq
;
124 /* Insert the sequence delimited by nodes FIRST and LAST before
125 iterator I. M specifies how to update iterator I after insertion
126 (see enum gsi_iterator_update).
128 This routine assumes that there is a forward and backward path
129 between FIRST and LAST (i.e., they are linked in a doubly-linked
130 list). Additionally, if FIRST == LAST, this routine will properly
131 insert a single node. */
134 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
135 gimple_seq_node first
,
136 gimple_seq_node last
,
137 enum gsi_iterator_update mode
)
140 gimple_seq_node cur
= i
->ptr
;
142 gcc_assert (!cur
|| cur
->prev
);
144 if ((bb
= gsi_bb (*i
)) != NULL
)
145 update_bb_for_stmts (first
, last
, bb
);
147 /* Link SEQ before CUR in the sequence. */
150 first
->prev
= cur
->prev
;
151 if (first
->prev
->next
)
152 first
->prev
->next
= first
;
154 gimple_seq_set_first (i
->seq
, first
);
160 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
162 /* If CUR is NULL, we link at the end of the sequence (this case happens
163 when gsi_after_labels is called for a basic block that contains only
164 labels, so it returns an iterator after the end of the block, and
165 we need to insert before it; it might be cleaner to add a flag to the
166 iterator saying whether we are at the start or end of the list). */
170 first
->prev
= itlast
;
171 itlast
->next
= first
;
174 gimple_seq_set_first (i
->seq
, first
);
175 gimple_seq_set_last (i
->seq
, last
);
178 /* Update the iterator, if requested. */
182 case GSI_CONTINUE_LINKING
:
193 /* Inserts the sequence of statements SEQ before the statement pointed
194 by iterator I. MODE indicates what to do with the iterator after
195 insertion (see enum gsi_iterator_update).
197 This function does not scan for new operands. It is provided for
198 the use of the gimplifier, which manipulates statements for which
199 def/use information has not yet been constructed. Most callers
200 should use gsi_insert_seq_before. */
203 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
204 enum gsi_iterator_update mode
)
206 gimple_seq_node first
, last
;
211 /* Don't allow inserting a sequence into itself. */
212 gcc_assert (seq
!= *i
->seq
);
214 first
= gimple_seq_first (seq
);
215 last
= gimple_seq_last (seq
);
217 /* Empty sequences need no work. */
220 gcc_assert (first
== last
);
224 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
228 /* Inserts the sequence of statements SEQ before the statement pointed
229 by iterator I. MODE indicates what to do with the iterator after
230 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
234 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
235 enum gsi_iterator_update mode
)
237 update_modified_stmts (seq
);
238 gsi_insert_seq_before_without_update (i
, seq
, mode
);
242 /* Insert the sequence delimited by nodes FIRST and LAST after
243 iterator I. M specifies how to update iterator I after insertion
244 (see enum gsi_iterator_update).
246 This routine assumes that there is a forward and backward path
247 between FIRST and LAST (i.e., they are linked in a doubly-linked
248 list). Additionally, if FIRST == LAST, this routine will properly
249 insert a single node. */
252 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
253 gimple_seq_node first
,
254 gimple_seq_node last
,
255 enum gsi_iterator_update m
)
258 gimple_seq_node cur
= i
->ptr
;
260 gcc_assert (!cur
|| cur
->prev
);
262 /* If the iterator is inside a basic block, we need to update the
263 basic block information for all the nodes between FIRST and LAST. */
264 if ((bb
= gsi_bb (*i
)) != NULL
)
265 update_bb_for_stmts (first
, last
, bb
);
267 /* Link SEQ after CUR. */
270 last
->next
= cur
->next
;
273 last
->next
->prev
= last
;
276 gimple_seq_set_last (i
->seq
, last
);
282 gcc_assert (!gimple_seq_last (*i
->seq
));
284 gimple_seq_set_first (i
->seq
, first
);
285 gimple_seq_set_last (i
->seq
, last
);
288 /* Update the iterator, if requested. */
294 case GSI_CONTINUE_LINKING
:
306 /* Links sequence SEQ after the statement pointed-to by iterator I.
307 MODE is as in gsi_insert_after.
309 This function does not scan for new operands. It is provided for
310 the use of the gimplifier, which manipulates statements for which
311 def/use information has not yet been constructed. Most callers
312 should use gsi_insert_seq_after. */
315 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
316 enum gsi_iterator_update mode
)
318 gimple_seq_node first
, last
;
323 /* Don't allow inserting a sequence into itself. */
324 gcc_assert (seq
!= *i
->seq
);
326 first
= gimple_seq_first (seq
);
327 last
= gimple_seq_last (seq
);
329 /* Empty sequences need no work. */
332 gcc_assert (first
== last
);
336 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
340 /* Links sequence SEQ after the statement pointed-to by iterator I.
341 MODE is as in gsi_insert_after. Scan the statements in SEQ
345 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
346 enum gsi_iterator_update mode
)
348 update_modified_stmts (seq
);
349 gsi_insert_seq_after_without_update (i
, seq
, mode
);
353 /* Move all statements in the sequence after I to a new sequence.
354 Return this new sequence. */
357 gsi_split_seq_after (gimple_stmt_iterator i
)
359 gimple_seq_node cur
, next
;
360 gimple_seq
*pold_seq
, new_seq
;
364 /* How can we possibly split after the end, or before the beginning? */
365 gcc_assert (cur
&& cur
->next
);
370 gimple_seq_set_first (&new_seq
, next
);
371 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
372 gimple_seq_set_last (pold_seq
, cur
);
379 /* Set the statement to which GSI points to STMT. This only updates
380 the iterator and the gimple sequence, it doesn't do the bookkeeping
384 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
386 gimple orig_stmt
= gsi_stmt (*gsi
);
389 stmt
->next
= next
= orig_stmt
->next
;
390 stmt
->prev
= prev
= orig_stmt
->prev
;
391 /* Note how we don't clear next/prev of orig_stmt. This is so that
392 copies of *GSI our callers might still hold (to orig_stmt)
393 can be advanced as if they too were replaced. */
397 gimple_seq_set_first (gsi
->seq
, stmt
);
401 gimple_seq_set_last (gsi
->seq
, stmt
);
407 /* Move all statements in the sequence before I to a new sequence.
408 Return this new sequence. I is set to the head of the new list. */
411 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
413 gimple_seq_node cur
, prev
;
418 /* How can we possibly split after the end? */
427 /* Set the limits on NEW_SEQ. */
428 gimple_seq_set_first (pnew_seq
, cur
);
429 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
431 /* Cut OLD_SEQ before I. */
432 gimple_seq_set_last (&old_seq
, prev
);
438 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
439 is true, the exception handling information of the original
440 statement is moved to the new statement. Assignments must only be
441 replaced with assignments to the same LHS. Returns whether EH edge
442 cleanup is required. */
445 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
447 gimple orig_stmt
= gsi_stmt (*gsi
);
448 bool require_eh_edge_purge
= false;
450 if (stmt
== orig_stmt
)
453 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
454 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
456 gimple_set_location (stmt
, gimple_location (orig_stmt
));
457 gimple_set_bb (stmt
, gsi_bb (*gsi
));
459 /* Preserve EH region information from the original statement, if
460 requested by the caller. */
462 require_eh_edge_purge
= maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
464 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
466 /* Free all the data flow information for ORIG_STMT. */
467 gimple_set_bb (orig_stmt
, NULL
);
468 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
469 delink_stmt_imm_use (orig_stmt
);
471 gsi_set_stmt (gsi
, stmt
);
472 gimple_set_modified (stmt
, true);
473 update_modified_stmt (stmt
);
474 return require_eh_edge_purge
;
478 /* Replace the statement pointed-to by GSI with the sequence SEQ.
479 If UPDATE_EH_INFO is true, the exception handling information of
480 the original statement is moved to the last statement of the new
481 sequence. If the old statement is an assignment, then so must
482 be the last statement of the new sequence, and they must have the
486 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
489 gimple_stmt_iterator seqi
;
491 if (gimple_seq_empty_p (seq
))
493 gsi_remove (gsi
, true);
496 seqi
= gsi_last (seq
);
497 last
= gsi_stmt (seqi
);
498 gsi_remove (&seqi
, false);
499 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
500 gsi_replace (gsi
, last
, update_eh_info
);
504 /* Insert statement STMT before the statement pointed-to by iterator I.
505 M specifies how to update iterator I after insertion (see enum
506 gsi_iterator_update).
508 This function does not scan for new operands. It is provided for
509 the use of the gimplifier, which manipulates statements for which
510 def/use information has not yet been constructed. Most callers
511 should use gsi_insert_before. */
514 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
515 enum gsi_iterator_update m
)
517 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
520 /* Insert statement STMT before the statement pointed-to by iterator I.
521 Update STMT's basic block and scan it for new operands. M
522 specifies how to update iterator I after insertion (see enum
523 gsi_iterator_update). */
526 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
527 enum gsi_iterator_update m
)
529 update_modified_stmt (stmt
);
530 gsi_insert_before_without_update (i
, stmt
, m
);
534 /* Insert statement STMT after the statement pointed-to by iterator I.
535 M specifies how to update iterator I after insertion (see enum
536 gsi_iterator_update).
538 This function does not scan for new operands. It is provided for
539 the use of the gimplifier, which manipulates statements for which
540 def/use information has not yet been constructed. Most callers
541 should use gsi_insert_after. */
544 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
545 enum gsi_iterator_update m
)
547 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
551 /* Insert statement STMT after the statement pointed-to by iterator I.
552 Update STMT's basic block and scan it for new operands. M
553 specifies how to update iterator I after insertion (see enum
554 gsi_iterator_update). */
557 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
558 enum gsi_iterator_update m
)
560 update_modified_stmt (stmt
);
561 gsi_insert_after_without_update (i
, stmt
, m
);
565 /* Remove the current stmt from the sequence. The iterator is updated
566 to point to the next statement.
568 REMOVE_PERMANENTLY is true when the statement is going to be removed
569 from the IL and not reinserted elsewhere. In that case we remove the
570 statement pointed to by iterator I from the EH tables, and free its
571 operand caches. Otherwise we do not modify this information. Returns
572 true whether EH edge cleanup is required. */
575 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
577 gimple_seq_node cur
, next
, prev
;
578 gimple stmt
= gsi_stmt (*i
);
579 bool require_eh_edge_purge
= false;
581 if (gimple_code (stmt
) != GIMPLE_PHI
)
582 insert_debug_temps_for_defs (i
);
584 /* Free all the data flow information for STMT. */
585 gimple_set_bb (stmt
, NULL
);
586 delink_stmt_imm_use (stmt
);
587 gimple_set_modified (stmt
, true);
589 if (remove_permanently
)
591 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
592 gimple_remove_stmt_histograms (cfun
, stmt
);
595 /* Update the iterator and re-wire the links in I->SEQ. */
599 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
602 /* Cur is not last. */
605 /* Cur is last but not first. */
606 gimple_seq_set_last (i
->seq
, prev
);
609 /* Cur is not first. */
617 return require_eh_edge_purge
;
621 /* Finds iterator for STMT. */
624 gsi_for_stmt (gimple stmt
)
626 gimple_stmt_iterator i
;
627 basic_block bb
= gimple_bb (stmt
);
629 if (gimple_code (stmt
) == GIMPLE_PHI
)
630 i
= gsi_start_phis (bb
);
632 i
= gsi_start_bb (bb
);
638 /* Finds iterator for PHI. */
641 gsi_for_phi (gphi
*phi
)
644 basic_block bb
= gimple_bb (phi
);
646 i
= gsi_start_phis (bb
);
652 /* Move the statement at FROM so it comes right after the statement at TO. */
655 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
657 gimple stmt
= gsi_stmt (*from
);
658 gsi_remove (from
, false);
660 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
661 move statements to an empty block. */
662 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
666 /* Move the statement at FROM so it comes right before the statement
670 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
672 gimple stmt
= gsi_stmt (*from
);
673 gsi_remove (from
, false);
675 /* For consistency with gsi_move_after, it might be better to have
676 GSI_NEW_STMT here; however, that breaks several places that expect
677 that TO does not change. */
678 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
682 /* Move the statement at FROM to the end of basic block BB. */
685 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
687 gimple_stmt_iterator last
= gsi_last_bb (bb
);
688 gcc_checking_assert (gsi_bb (last
) == bb
);
690 /* Have to check gsi_end_p because it could be an empty block. */
691 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
692 gsi_move_before (from
, &last
);
694 gsi_move_after (from
, &last
);
698 /* Add STMT to the pending list of edge E. No actual insertion is
699 made until a call to gsi_commit_edge_inserts () is made. */
702 gsi_insert_on_edge (edge e
, gimple stmt
)
704 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
707 /* Add the sequence of statements SEQ to the pending list of edge E.
708 No actual insertion is made until a call to gsi_commit_edge_inserts
712 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
714 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
717 /* Return a new iterator pointing to the first statement in sequence of
718 statements on edge E. Such statements need to be subsequently moved into a
719 basic block by calling gsi_commit_edge_inserts. */
722 gsi_start_edge (edge e
)
724 return gsi_start (PENDING_STMT (e
));
727 /* Insert the statement pointed-to by GSI into edge E. Every attempt
728 is made to place the statement in an existing basic block, but
729 sometimes that isn't possible. When it isn't possible, the edge is
730 split and the statement is added to the new block.
732 In all cases, the returned *GSI points to the correct location. The
733 return value is true if insertion should be done after the location,
734 or false if it should be done before the location. If a new basic block
735 has to be created, it is stored in *NEW_BB. */
738 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
741 basic_block dest
, src
;
746 /* If the destination has one predecessor which has no PHI nodes,
747 insert there. Except for the exit block.
749 The requirement for no PHI nodes could be relaxed. Basically we
750 would have to examine the PHIs to prove that none of them used
751 the value set by the statement we want to insert on E. That
752 hardly seems worth the effort. */
754 if (single_pred_p (dest
)
755 && gimple_seq_empty_p (phi_nodes (dest
))
756 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
758 *gsi
= gsi_start_bb (dest
);
759 if (gsi_end_p (*gsi
))
762 /* Make sure we insert after any leading labels. */
763 tmp
= gsi_stmt (*gsi
);
764 while (gimple_code (tmp
) == GIMPLE_LABEL
)
767 if (gsi_end_p (*gsi
))
769 tmp
= gsi_stmt (*gsi
);
772 if (gsi_end_p (*gsi
))
774 *gsi
= gsi_last_bb (dest
);
781 /* If the source has one successor, the edge is not abnormal and
782 the last statement does not end a basic block, insert there.
783 Except for the entry block. */
785 if ((e
->flags
& EDGE_ABNORMAL
) == 0
786 && single_succ_p (src
)
787 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
789 *gsi
= gsi_last_bb (src
);
790 if (gsi_end_p (*gsi
))
793 tmp
= gsi_stmt (*gsi
);
794 if (!stmt_ends_bb_p (tmp
))
797 switch (gimple_code (tmp
))
807 /* Otherwise, create a new basic block, and split this edge. */
808 dest
= split_edge (e
);
811 e
= single_pred_edge (dest
);
816 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
817 block has to be created, it is returned. */
820 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
822 gimple_stmt_iterator gsi
;
823 basic_block new_bb
= NULL
;
826 gcc_assert (!PENDING_STMT (e
));
828 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
830 update_call_edge_frequencies (stmt
, gsi
.bb
);
833 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
835 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
840 /* Insert STMTS on edge E. If a new block has to be created, it
844 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
846 gimple_stmt_iterator gsi
;
847 basic_block new_bb
= NULL
;
850 gcc_assert (!PENDING_STMT (e
));
852 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
853 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
856 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
858 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
863 /* This routine will commit all pending edge insertions, creating any new
864 basic blocks which are necessary. */
867 gsi_commit_edge_inserts (void)
873 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
876 FOR_EACH_BB_FN (bb
, cfun
)
877 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
878 gsi_commit_one_edge_insert (e
, NULL
);
882 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
883 to this block, otherwise set it to NULL. */
886 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
891 if (PENDING_STMT (e
))
893 gimple_stmt_iterator gsi
;
894 gimple_seq seq
= PENDING_STMT (e
);
897 PENDING_STMT (e
) = NULL
;
899 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
900 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
903 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
905 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
909 /* Returns iterator at the start of the list of phi nodes of BB. */
912 gsi_start_phis (basic_block bb
)
914 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
916 /* Adapted from gsi_start_1. */
919 i
.ptr
= gimple_seq_first (*pseq
);
921 i
.bb
= i
.ptr
? gimple_bb (i
.ptr
) : NULL
;