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 "double-int.h"
35 #include "fold-const.h"
37 #include "hard-reg-set.h"
40 #include "dominance.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
46 #include "gimple-expr.h"
49 #include "gimple-iterator.h"
50 #include "gimple-ssa.h"
52 #include "plugin-api.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
59 #include "value-prof.h"
62 /* Mark the statement STMT as modified, and update it. */
65 update_modified_stmt (gimple stmt
)
67 if (!ssa_operands_active (cfun
))
69 update_stmt_if_modified (stmt
);
73 /* Mark the statements in SEQ as modified, and update them. */
76 update_modified_stmts (gimple_seq seq
)
78 gimple_stmt_iterator gsi
;
80 if (!ssa_operands_active (cfun
))
82 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
83 update_stmt_if_modified (gsi_stmt (gsi
));
87 /* Set BB to be the basic block for all the statements in the list
88 starting at FIRST and LAST. */
91 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
96 for (n
= first
; n
; n
= n
->next
)
98 gimple_set_bb (n
, bb
);
104 /* Set the frequencies for the cgraph_edges for each of the calls
105 starting at FIRST for their new position within BB. */
108 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
110 struct cgraph_node
*cfun_node
= NULL
;
114 for (n
= first
; n
; n
= n
->next
)
115 if (is_gimple_call (n
))
117 struct cgraph_edge
*e
;
119 /* These function calls are expensive enough that we want
120 to avoid calling them if we never see any calls. */
121 if (cfun_node
== NULL
)
123 cfun_node
= cgraph_node::get (current_function_decl
);
124 bb_freq
= (compute_call_stmt_bb_frequency
125 (current_function_decl
, bb
));
128 e
= cfun_node
->get_edge (n
);
130 e
->frequency
= bb_freq
;
134 /* Insert the sequence delimited by nodes FIRST and LAST before
135 iterator I. M specifies how to update iterator I after insertion
136 (see enum gsi_iterator_update).
138 This routine assumes that there is a forward and backward path
139 between FIRST and LAST (i.e., they are linked in a doubly-linked
140 list). Additionally, if FIRST == LAST, this routine will properly
141 insert a single node. */
144 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
145 gimple_seq_node first
,
146 gimple_seq_node last
,
147 enum gsi_iterator_update mode
)
150 gimple_seq_node cur
= i
->ptr
;
152 gcc_assert (!cur
|| cur
->prev
);
154 if ((bb
= gsi_bb (*i
)) != NULL
)
155 update_bb_for_stmts (first
, last
, bb
);
157 /* Link SEQ before CUR in the sequence. */
160 first
->prev
= cur
->prev
;
161 if (first
->prev
->next
)
162 first
->prev
->next
= first
;
164 gimple_seq_set_first (i
->seq
, first
);
170 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
172 /* If CUR is NULL, we link at the end of the sequence (this case happens
173 when gsi_after_labels is called for a basic block that contains only
174 labels, so it returns an iterator after the end of the block, and
175 we need to insert before it; it might be cleaner to add a flag to the
176 iterator saying whether we are at the start or end of the list). */
180 first
->prev
= itlast
;
181 itlast
->next
= first
;
184 gimple_seq_set_first (i
->seq
, first
);
185 gimple_seq_set_last (i
->seq
, last
);
188 /* Update the iterator, if requested. */
192 case GSI_CONTINUE_LINKING
:
203 /* Inserts the sequence of statements SEQ before the statement pointed
204 by iterator I. MODE indicates what to do with the iterator after
205 insertion (see enum gsi_iterator_update).
207 This function does not scan for new operands. It is provided for
208 the use of the gimplifier, which manipulates statements for which
209 def/use information has not yet been constructed. Most callers
210 should use gsi_insert_seq_before. */
213 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
214 enum gsi_iterator_update mode
)
216 gimple_seq_node first
, last
;
221 /* Don't allow inserting a sequence into itself. */
222 gcc_assert (seq
!= *i
->seq
);
224 first
= gimple_seq_first (seq
);
225 last
= gimple_seq_last (seq
);
227 /* Empty sequences need no work. */
230 gcc_assert (first
== last
);
234 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
238 /* Inserts the sequence of statements SEQ before the statement pointed
239 by iterator I. MODE indicates what to do with the iterator after
240 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
244 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
245 enum gsi_iterator_update mode
)
247 update_modified_stmts (seq
);
248 gsi_insert_seq_before_without_update (i
, seq
, mode
);
252 /* Insert the sequence delimited by nodes FIRST and LAST after
253 iterator I. M specifies how to update iterator I after insertion
254 (see enum gsi_iterator_update).
256 This routine assumes that there is a forward and backward path
257 between FIRST and LAST (i.e., they are linked in a doubly-linked
258 list). Additionally, if FIRST == LAST, this routine will properly
259 insert a single node. */
262 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
263 gimple_seq_node first
,
264 gimple_seq_node last
,
265 enum gsi_iterator_update m
)
268 gimple_seq_node cur
= i
->ptr
;
270 gcc_assert (!cur
|| cur
->prev
);
272 /* If the iterator is inside a basic block, we need to update the
273 basic block information for all the nodes between FIRST and LAST. */
274 if ((bb
= gsi_bb (*i
)) != NULL
)
275 update_bb_for_stmts (first
, last
, bb
);
277 /* Link SEQ after CUR. */
280 last
->next
= cur
->next
;
283 last
->next
->prev
= last
;
286 gimple_seq_set_last (i
->seq
, last
);
292 gcc_assert (!gimple_seq_last (*i
->seq
));
294 gimple_seq_set_first (i
->seq
, first
);
295 gimple_seq_set_last (i
->seq
, last
);
298 /* Update the iterator, if requested. */
304 case GSI_CONTINUE_LINKING
:
316 /* Links sequence SEQ after the statement pointed-to by iterator I.
317 MODE is as in gsi_insert_after.
319 This function does not scan for new operands. It is provided for
320 the use of the gimplifier, which manipulates statements for which
321 def/use information has not yet been constructed. Most callers
322 should use gsi_insert_seq_after. */
325 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
326 enum gsi_iterator_update mode
)
328 gimple_seq_node first
, last
;
333 /* Don't allow inserting a sequence into itself. */
334 gcc_assert (seq
!= *i
->seq
);
336 first
= gimple_seq_first (seq
);
337 last
= gimple_seq_last (seq
);
339 /* Empty sequences need no work. */
342 gcc_assert (first
== last
);
346 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
350 /* Links sequence SEQ after the statement pointed-to by iterator I.
351 MODE is as in gsi_insert_after. Scan the statements in SEQ
355 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
356 enum gsi_iterator_update mode
)
358 update_modified_stmts (seq
);
359 gsi_insert_seq_after_without_update (i
, seq
, mode
);
363 /* Move all statements in the sequence after I to a new sequence.
364 Return this new sequence. */
367 gsi_split_seq_after (gimple_stmt_iterator i
)
369 gimple_seq_node cur
, next
;
370 gimple_seq
*pold_seq
, new_seq
;
374 /* How can we possibly split after the end, or before the beginning? */
375 gcc_assert (cur
&& cur
->next
);
380 gimple_seq_set_first (&new_seq
, next
);
381 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
382 gimple_seq_set_last (pold_seq
, cur
);
389 /* Set the statement to which GSI points to STMT. This only updates
390 the iterator and the gimple sequence, it doesn't do the bookkeeping
394 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
396 gimple orig_stmt
= gsi_stmt (*gsi
);
399 stmt
->next
= next
= orig_stmt
->next
;
400 stmt
->prev
= prev
= orig_stmt
->prev
;
401 /* Note how we don't clear next/prev of orig_stmt. This is so that
402 copies of *GSI our callers might still hold (to orig_stmt)
403 can be advanced as if they too were replaced. */
407 gimple_seq_set_first (gsi
->seq
, stmt
);
411 gimple_seq_set_last (gsi
->seq
, stmt
);
417 /* Move all statements in the sequence before I to a new sequence.
418 Return this new sequence. I is set to the head of the new list. */
421 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
423 gimple_seq_node cur
, prev
;
428 /* How can we possibly split after the end? */
437 /* Set the limits on NEW_SEQ. */
438 gimple_seq_set_first (pnew_seq
, cur
);
439 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
441 /* Cut OLD_SEQ before I. */
442 gimple_seq_set_last (&old_seq
, prev
);
448 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
449 is true, the exception handling information of the original
450 statement is moved to the new statement. Assignments must only be
451 replaced with assignments to the same LHS. Returns whether EH edge
452 cleanup is required. */
455 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
457 gimple orig_stmt
= gsi_stmt (*gsi
);
458 bool require_eh_edge_purge
= false;
460 if (stmt
== orig_stmt
)
463 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
464 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
466 gimple_set_location (stmt
, gimple_location (orig_stmt
));
467 gimple_set_bb (stmt
, gsi_bb (*gsi
));
469 /* Preserve EH region information from the original statement, if
470 requested by the caller. */
472 require_eh_edge_purge
= maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
474 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
476 /* Free all the data flow information for ORIG_STMT. */
477 gimple_set_bb (orig_stmt
, NULL
);
478 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
479 delink_stmt_imm_use (orig_stmt
);
481 gsi_set_stmt (gsi
, stmt
);
482 gimple_set_modified (stmt
, true);
483 update_modified_stmt (stmt
);
484 return require_eh_edge_purge
;
488 /* Replace the statement pointed-to by GSI with the sequence SEQ.
489 If UPDATE_EH_INFO is true, the exception handling information of
490 the original statement is moved to the last statement of the new
491 sequence. If the old statement is an assignment, then so must
492 be the last statement of the new sequence, and they must have the
496 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
499 gimple_stmt_iterator seqi
;
501 if (gimple_seq_empty_p (seq
))
503 gsi_remove (gsi
, true);
506 seqi
= gsi_last (seq
);
507 last
= gsi_stmt (seqi
);
508 gsi_remove (&seqi
, false);
509 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
510 gsi_replace (gsi
, last
, update_eh_info
);
514 /* Insert statement STMT before the statement pointed-to by iterator I.
515 M specifies how to update iterator I after insertion (see enum
516 gsi_iterator_update).
518 This function does not scan for new operands. It is provided for
519 the use of the gimplifier, which manipulates statements for which
520 def/use information has not yet been constructed. Most callers
521 should use gsi_insert_before. */
524 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
525 enum gsi_iterator_update m
)
527 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
530 /* Insert statement STMT before the statement pointed-to by iterator I.
531 Update STMT's basic block and scan it for new operands. M
532 specifies how to update iterator I after insertion (see enum
533 gsi_iterator_update). */
536 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
537 enum gsi_iterator_update m
)
539 update_modified_stmt (stmt
);
540 gsi_insert_before_without_update (i
, stmt
, m
);
544 /* Insert statement STMT after the statement pointed-to by iterator I.
545 M specifies how to update iterator I after insertion (see enum
546 gsi_iterator_update).
548 This function does not scan for new operands. It is provided for
549 the use of the gimplifier, which manipulates statements for which
550 def/use information has not yet been constructed. Most callers
551 should use gsi_insert_after. */
554 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
555 enum gsi_iterator_update m
)
557 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
561 /* Insert statement STMT after the statement pointed-to by iterator I.
562 Update STMT's basic block and scan it for new operands. M
563 specifies how to update iterator I after insertion (see enum
564 gsi_iterator_update). */
567 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
568 enum gsi_iterator_update m
)
570 update_modified_stmt (stmt
);
571 gsi_insert_after_without_update (i
, stmt
, m
);
575 /* Remove the current stmt from the sequence. The iterator is updated
576 to point to the next statement.
578 REMOVE_PERMANENTLY is true when the statement is going to be removed
579 from the IL and not reinserted elsewhere. In that case we remove the
580 statement pointed to by iterator I from the EH tables, and free its
581 operand caches. Otherwise we do not modify this information. Returns
582 true whether EH edge cleanup is required. */
585 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
587 gimple_seq_node cur
, next
, prev
;
588 gimple stmt
= gsi_stmt (*i
);
589 bool require_eh_edge_purge
= false;
591 if (gimple_code (stmt
) != GIMPLE_PHI
)
592 insert_debug_temps_for_defs (i
);
594 /* Free all the data flow information for STMT. */
595 gimple_set_bb (stmt
, NULL
);
596 delink_stmt_imm_use (stmt
);
597 gimple_set_modified (stmt
, true);
599 if (remove_permanently
)
601 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
602 gimple_remove_stmt_histograms (cfun
, stmt
);
605 /* Update the iterator and re-wire the links in I->SEQ. */
609 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
612 /* Cur is not last. */
615 /* Cur is last but not first. */
616 gimple_seq_set_last (i
->seq
, prev
);
619 /* Cur is not first. */
627 return require_eh_edge_purge
;
631 /* Finds iterator for STMT. */
634 gsi_for_stmt (gimple stmt
)
636 gimple_stmt_iterator i
;
637 basic_block bb
= gimple_bb (stmt
);
639 if (gimple_code (stmt
) == GIMPLE_PHI
)
640 i
= gsi_start_phis (bb
);
642 i
= gsi_start_bb (bb
);
648 /* Finds iterator for PHI. */
651 gsi_for_phi (gphi
*phi
)
654 basic_block bb
= gimple_bb (phi
);
656 i
= gsi_start_phis (bb
);
662 /* Move the statement at FROM so it comes right after the statement at TO. */
665 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
667 gimple stmt
= gsi_stmt (*from
);
668 gsi_remove (from
, false);
670 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
671 move statements to an empty block. */
672 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
676 /* Move the statement at FROM so it comes right before the statement
680 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
682 gimple stmt
= gsi_stmt (*from
);
683 gsi_remove (from
, false);
685 /* For consistency with gsi_move_after, it might be better to have
686 GSI_NEW_STMT here; however, that breaks several places that expect
687 that TO does not change. */
688 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
692 /* Move the statement at FROM to the end of basic block BB. */
695 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
697 gimple_stmt_iterator last
= gsi_last_bb (bb
);
698 gcc_checking_assert (gsi_bb (last
) == bb
);
700 /* Have to check gsi_end_p because it could be an empty block. */
701 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
702 gsi_move_before (from
, &last
);
704 gsi_move_after (from
, &last
);
708 /* Add STMT to the pending list of edge E. No actual insertion is
709 made until a call to gsi_commit_edge_inserts () is made. */
712 gsi_insert_on_edge (edge e
, gimple stmt
)
714 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
717 /* Add the sequence of statements SEQ to the pending list of edge E.
718 No actual insertion is made until a call to gsi_commit_edge_inserts
722 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
724 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
727 /* Return a new iterator pointing to the first statement in sequence of
728 statements on edge E. Such statements need to be subsequently moved into a
729 basic block by calling gsi_commit_edge_inserts. */
732 gsi_start_edge (edge e
)
734 return gsi_start (PENDING_STMT (e
));
737 /* Insert the statement pointed-to by GSI into edge E. Every attempt
738 is made to place the statement in an existing basic block, but
739 sometimes that isn't possible. When it isn't possible, the edge is
740 split and the statement is added to the new block.
742 In all cases, the returned *GSI points to the correct location. The
743 return value is true if insertion should be done after the location,
744 or false if it should be done before the location. If a new basic block
745 has to be created, it is stored in *NEW_BB. */
748 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
751 basic_block dest
, src
;
756 /* If the destination has one predecessor which has no PHI nodes,
757 insert there. Except for the exit block.
759 The requirement for no PHI nodes could be relaxed. Basically we
760 would have to examine the PHIs to prove that none of them used
761 the value set by the statement we want to insert on E. That
762 hardly seems worth the effort. */
764 if (single_pred_p (dest
)
765 && gimple_seq_empty_p (phi_nodes (dest
))
766 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
768 *gsi
= gsi_start_bb (dest
);
769 if (gsi_end_p (*gsi
))
772 /* Make sure we insert after any leading labels. */
773 tmp
= gsi_stmt (*gsi
);
774 while (gimple_code (tmp
) == GIMPLE_LABEL
)
777 if (gsi_end_p (*gsi
))
779 tmp
= gsi_stmt (*gsi
);
782 if (gsi_end_p (*gsi
))
784 *gsi
= gsi_last_bb (dest
);
791 /* If the source has one successor, the edge is not abnormal and
792 the last statement does not end a basic block, insert there.
793 Except for the entry block. */
795 if ((e
->flags
& EDGE_ABNORMAL
) == 0
796 && single_succ_p (src
)
797 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
799 *gsi
= gsi_last_bb (src
);
800 if (gsi_end_p (*gsi
))
803 tmp
= gsi_stmt (*gsi
);
804 if (!stmt_ends_bb_p (tmp
))
807 switch (gimple_code (tmp
))
817 /* Otherwise, create a new basic block, and split this edge. */
818 dest
= split_edge (e
);
821 e
= single_pred_edge (dest
);
826 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
827 block has to be created, it is returned. */
830 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
832 gimple_stmt_iterator gsi
;
833 basic_block new_bb
= NULL
;
836 gcc_assert (!PENDING_STMT (e
));
838 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
840 update_call_edge_frequencies (stmt
, gsi
.bb
);
843 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
845 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
850 /* Insert STMTS on edge E. If a new block has to be created, it
854 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
856 gimple_stmt_iterator gsi
;
857 basic_block new_bb
= NULL
;
860 gcc_assert (!PENDING_STMT (e
));
862 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
863 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
866 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
868 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
873 /* This routine will commit all pending edge insertions, creating any new
874 basic blocks which are necessary. */
877 gsi_commit_edge_inserts (void)
883 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
886 FOR_EACH_BB_FN (bb
, cfun
)
887 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
888 gsi_commit_one_edge_insert (e
, NULL
);
892 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
893 to this block, otherwise set it to NULL. */
896 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
901 if (PENDING_STMT (e
))
903 gimple_stmt_iterator gsi
;
904 gimple_seq seq
= PENDING_STMT (e
);
907 PENDING_STMT (e
) = NULL
;
909 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
910 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
913 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
915 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
919 /* Returns iterator at the start of the list of phi nodes of BB. */
922 gsi_start_phis (basic_block bb
)
924 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
926 /* Adapted from gsi_start_1. */
929 i
.ptr
= gimple_seq_first (*pseq
);
931 i
.bb
= i
.ptr
? gimple_bb (i
.ptr
) : NULL
;