1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2014 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"
31 #include "hard-reg-set.h"
34 #include "dominance.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
40 #include "gimple-expr.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
47 #include "tree-phinodes.h"
48 #include "ssa-iterators.h"
50 #include "value-prof.h"
53 /* Mark the statement STMT as modified, and update it. */
56 update_modified_stmt (gimple stmt
)
58 if (!ssa_operands_active (cfun
))
60 update_stmt_if_modified (stmt
);
64 /* Mark the statements in SEQ as modified, and update them. */
67 update_modified_stmts (gimple_seq seq
)
69 gimple_stmt_iterator gsi
;
71 if (!ssa_operands_active (cfun
))
73 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
74 update_stmt_if_modified (gsi_stmt (gsi
));
78 /* Set BB to be the basic block for all the statements in the list
79 starting at FIRST and LAST. */
82 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
87 for (n
= first
; n
; n
= n
->next
)
89 gimple_set_bb (n
, bb
);
95 /* Set the frequencies for the cgraph_edges for each of the calls
96 starting at FIRST for their new position within BB. */
99 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
101 struct cgraph_node
*cfun_node
= NULL
;
105 for (n
= first
; n
; n
= n
->next
)
106 if (is_gimple_call (n
))
108 struct cgraph_edge
*e
;
110 /* These function calls are expensive enough that we want
111 to avoid calling them if we never see any calls. */
112 if (cfun_node
== NULL
)
114 cfun_node
= cgraph_node::get (current_function_decl
);
115 bb_freq
= (compute_call_stmt_bb_frequency
116 (current_function_decl
, bb
));
119 e
= cfun_node
->get_edge (n
);
121 e
->frequency
= bb_freq
;
125 /* Insert the sequence delimited by nodes FIRST and LAST before
126 iterator I. M specifies how to update iterator I after insertion
127 (see enum gsi_iterator_update).
129 This routine assumes that there is a forward and backward path
130 between FIRST and LAST (i.e., they are linked in a doubly-linked
131 list). Additionally, if FIRST == LAST, this routine will properly
132 insert a single node. */
135 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
136 gimple_seq_node first
,
137 gimple_seq_node last
,
138 enum gsi_iterator_update mode
)
141 gimple_seq_node cur
= i
->ptr
;
143 gcc_assert (!cur
|| cur
->prev
);
145 if ((bb
= gsi_bb (*i
)) != NULL
)
146 update_bb_for_stmts (first
, last
, bb
);
148 /* Link SEQ before CUR in the sequence. */
151 first
->prev
= cur
->prev
;
152 if (first
->prev
->next
)
153 first
->prev
->next
= first
;
155 gimple_seq_set_first (i
->seq
, first
);
161 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
163 /* If CUR is NULL, we link at the end of the sequence (this case happens
164 when gsi_after_labels is called for a basic block that contains only
165 labels, so it returns an iterator after the end of the block, and
166 we need to insert before it; it might be cleaner to add a flag to the
167 iterator saying whether we are at the start or end of the list). */
171 first
->prev
= itlast
;
172 itlast
->next
= first
;
175 gimple_seq_set_first (i
->seq
, first
);
176 gimple_seq_set_last (i
->seq
, last
);
179 /* Update the iterator, if requested. */
183 case GSI_CONTINUE_LINKING
:
194 /* Inserts the sequence of statements SEQ before the statement pointed
195 by iterator I. MODE indicates what to do with the iterator after
196 insertion (see enum gsi_iterator_update).
198 This function does not scan for new operands. It is provided for
199 the use of the gimplifier, which manipulates statements for which
200 def/use information has not yet been constructed. Most callers
201 should use gsi_insert_seq_before. */
204 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
205 enum gsi_iterator_update mode
)
207 gimple_seq_node first
, last
;
212 /* Don't allow inserting a sequence into itself. */
213 gcc_assert (seq
!= *i
->seq
);
215 first
= gimple_seq_first (seq
);
216 last
= gimple_seq_last (seq
);
218 /* Empty sequences need no work. */
221 gcc_assert (first
== last
);
225 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
229 /* Inserts the sequence of statements SEQ before the statement pointed
230 by iterator I. MODE indicates what to do with the iterator after
231 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
235 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
236 enum gsi_iterator_update mode
)
238 update_modified_stmts (seq
);
239 gsi_insert_seq_before_without_update (i
, seq
, mode
);
243 /* Insert the sequence delimited by nodes FIRST and LAST after
244 iterator I. M specifies how to update iterator I after insertion
245 (see enum gsi_iterator_update).
247 This routine assumes that there is a forward and backward path
248 between FIRST and LAST (i.e., they are linked in a doubly-linked
249 list). Additionally, if FIRST == LAST, this routine will properly
250 insert a single node. */
253 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
254 gimple_seq_node first
,
255 gimple_seq_node last
,
256 enum gsi_iterator_update m
)
259 gimple_seq_node cur
= i
->ptr
;
261 gcc_assert (!cur
|| cur
->prev
);
263 /* If the iterator is inside a basic block, we need to update the
264 basic block information for all the nodes between FIRST and LAST. */
265 if ((bb
= gsi_bb (*i
)) != NULL
)
266 update_bb_for_stmts (first
, last
, bb
);
268 /* Link SEQ after CUR. */
271 last
->next
= cur
->next
;
274 last
->next
->prev
= last
;
277 gimple_seq_set_last (i
->seq
, last
);
283 gcc_assert (!gimple_seq_last (*i
->seq
));
285 gimple_seq_set_first (i
->seq
, first
);
286 gimple_seq_set_last (i
->seq
, last
);
289 /* Update the iterator, if requested. */
295 case GSI_CONTINUE_LINKING
:
307 /* Links sequence SEQ after the statement pointed-to by iterator I.
308 MODE is as in gsi_insert_after.
310 This function does not scan for new operands. It is provided for
311 the use of the gimplifier, which manipulates statements for which
312 def/use information has not yet been constructed. Most callers
313 should use gsi_insert_seq_after. */
316 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
317 enum gsi_iterator_update mode
)
319 gimple_seq_node first
, last
;
324 /* Don't allow inserting a sequence into itself. */
325 gcc_assert (seq
!= *i
->seq
);
327 first
= gimple_seq_first (seq
);
328 last
= gimple_seq_last (seq
);
330 /* Empty sequences need no work. */
333 gcc_assert (first
== last
);
337 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
341 /* Links sequence SEQ after the statement pointed-to by iterator I.
342 MODE is as in gsi_insert_after. Scan the statements in SEQ
346 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
347 enum gsi_iterator_update mode
)
349 update_modified_stmts (seq
);
350 gsi_insert_seq_after_without_update (i
, seq
, mode
);
354 /* Move all statements in the sequence after I to a new sequence.
355 Return this new sequence. */
358 gsi_split_seq_after (gimple_stmt_iterator i
)
360 gimple_seq_node cur
, next
;
361 gimple_seq
*pold_seq
, new_seq
;
365 /* How can we possibly split after the end, or before the beginning? */
366 gcc_assert (cur
&& cur
->next
);
371 gimple_seq_set_first (&new_seq
, next
);
372 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
373 gimple_seq_set_last (pold_seq
, cur
);
380 /* Set the statement to which GSI points to STMT. This only updates
381 the iterator and the gimple sequence, it doesn't do the bookkeeping
385 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
387 gimple orig_stmt
= gsi_stmt (*gsi
);
390 stmt
->next
= next
= orig_stmt
->next
;
391 stmt
->prev
= prev
= orig_stmt
->prev
;
392 /* Note how we don't clear next/prev of orig_stmt. This is so that
393 copies of *GSI our callers might still hold (to orig_stmt)
394 can be advanced as if they too were replaced. */
398 gimple_seq_set_first (gsi
->seq
, stmt
);
402 gimple_seq_set_last (gsi
->seq
, stmt
);
408 /* Move all statements in the sequence before I to a new sequence.
409 Return this new sequence. I is set to the head of the new list. */
412 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
414 gimple_seq_node cur
, prev
;
419 /* How can we possibly split after the end? */
428 /* Set the limits on NEW_SEQ. */
429 gimple_seq_set_first (pnew_seq
, cur
);
430 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
432 /* Cut OLD_SEQ before I. */
433 gimple_seq_set_last (&old_seq
, prev
);
439 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
440 is true, the exception handling information of the original
441 statement is moved to the new statement. Assignments must only be
442 replaced with assignments to the same LHS. Returns whether EH edge
443 cleanup is required. */
446 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
448 gimple orig_stmt
= gsi_stmt (*gsi
);
449 bool require_eh_edge_purge
= false;
451 if (stmt
== orig_stmt
)
454 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
455 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
457 gimple_set_location (stmt
, gimple_location (orig_stmt
));
458 gimple_set_bb (stmt
, gsi_bb (*gsi
));
460 /* Preserve EH region information from the original statement, if
461 requested by the caller. */
463 require_eh_edge_purge
= maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
465 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
467 /* Free all the data flow information for ORIG_STMT. */
468 gimple_set_bb (orig_stmt
, NULL
);
469 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
470 delink_stmt_imm_use (orig_stmt
);
472 gsi_set_stmt (gsi
, stmt
);
473 gimple_set_modified (stmt
, true);
474 update_modified_stmt (stmt
);
475 return require_eh_edge_purge
;
479 /* Replace the statement pointed-to by GSI with the sequence SEQ.
480 If UPDATE_EH_INFO is true, the exception handling information of
481 the original statement is moved to the last statement of the new
482 sequence. If the old statement is an assignment, then so must
483 be the last statement of the new sequence, and they must have the
487 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
490 gimple_stmt_iterator seqi
;
492 if (gimple_seq_empty_p (seq
))
494 gsi_remove (gsi
, true);
497 seqi
= gsi_last (seq
);
498 last
= gsi_stmt (seqi
);
499 gsi_remove (&seqi
, false);
500 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
501 gsi_replace (gsi
, last
, update_eh_info
);
505 /* Insert statement STMT before the statement pointed-to by iterator I.
506 M specifies how to update iterator I after insertion (see enum
507 gsi_iterator_update).
509 This function does not scan for new operands. It is provided for
510 the use of the gimplifier, which manipulates statements for which
511 def/use information has not yet been constructed. Most callers
512 should use gsi_insert_before. */
515 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
516 enum gsi_iterator_update m
)
518 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
521 /* Insert statement STMT before the statement pointed-to by iterator I.
522 Update STMT's basic block and scan it for new operands. M
523 specifies how to update iterator I after insertion (see enum
524 gsi_iterator_update). */
527 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
528 enum gsi_iterator_update m
)
530 update_modified_stmt (stmt
);
531 gsi_insert_before_without_update (i
, stmt
, m
);
535 /* Insert statement STMT after the statement pointed-to by iterator I.
536 M specifies how to update iterator I after insertion (see enum
537 gsi_iterator_update).
539 This function does not scan for new operands. It is provided for
540 the use of the gimplifier, which manipulates statements for which
541 def/use information has not yet been constructed. Most callers
542 should use gsi_insert_after. */
545 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
546 enum gsi_iterator_update m
)
548 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
552 /* Insert statement STMT after the statement pointed-to by iterator I.
553 Update STMT's basic block and scan it for new operands. M
554 specifies how to update iterator I after insertion (see enum
555 gsi_iterator_update). */
558 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
559 enum gsi_iterator_update m
)
561 update_modified_stmt (stmt
);
562 gsi_insert_after_without_update (i
, stmt
, m
);
566 /* Remove the current stmt from the sequence. The iterator is updated
567 to point to the next statement.
569 REMOVE_PERMANENTLY is true when the statement is going to be removed
570 from the IL and not reinserted elsewhere. In that case we remove the
571 statement pointed to by iterator I from the EH tables, and free its
572 operand caches. Otherwise we do not modify this information. Returns
573 true whether EH edge cleanup is required. */
576 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
578 gimple_seq_node cur
, next
, prev
;
579 gimple stmt
= gsi_stmt (*i
);
580 bool require_eh_edge_purge
= false;
582 if (gimple_code (stmt
) != GIMPLE_PHI
)
583 insert_debug_temps_for_defs (i
);
585 /* Free all the data flow information for STMT. */
586 gimple_set_bb (stmt
, NULL
);
587 delink_stmt_imm_use (stmt
);
588 gimple_set_modified (stmt
, true);
590 if (remove_permanently
)
592 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
593 gimple_remove_stmt_histograms (cfun
, stmt
);
596 /* Update the iterator and re-wire the links in I->SEQ. */
600 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
603 /* Cur is not last. */
606 /* Cur is last but not first. */
607 gimple_seq_set_last (i
->seq
, prev
);
610 /* Cur is not first. */
618 return require_eh_edge_purge
;
622 /* Finds iterator for STMT. */
625 gsi_for_stmt (gimple stmt
)
627 gimple_stmt_iterator i
;
628 basic_block bb
= gimple_bb (stmt
);
630 if (gimple_code (stmt
) == GIMPLE_PHI
)
631 i
= gsi_start_phis (bb
);
633 i
= gsi_start_bb (bb
);
640 /* Move the statement at FROM so it comes right after the statement at TO. */
643 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
645 gimple stmt
= gsi_stmt (*from
);
646 gsi_remove (from
, false);
648 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
649 move statements to an empty block. */
650 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
654 /* Move the statement at FROM so it comes right before the statement
658 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
660 gimple stmt
= gsi_stmt (*from
);
661 gsi_remove (from
, false);
663 /* For consistency with gsi_move_after, it might be better to have
664 GSI_NEW_STMT here; however, that breaks several places that expect
665 that TO does not change. */
666 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
670 /* Move the statement at FROM to the end of basic block BB. */
673 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
675 gimple_stmt_iterator last
= gsi_last_bb (bb
);
676 gcc_checking_assert (gsi_bb (last
) == bb
);
678 /* Have to check gsi_end_p because it could be an empty block. */
679 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
680 gsi_move_before (from
, &last
);
682 gsi_move_after (from
, &last
);
686 /* Add STMT to the pending list of edge E. No actual insertion is
687 made until a call to gsi_commit_edge_inserts () is made. */
690 gsi_insert_on_edge (edge e
, gimple stmt
)
692 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
695 /* Add the sequence of statements SEQ to the pending list of edge E.
696 No actual insertion is made until a call to gsi_commit_edge_inserts
700 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
702 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
705 /* Return a new iterator pointing to the first statement in sequence of
706 statements on edge E. Such statements need to be subsequently moved into a
707 basic block by calling gsi_commit_edge_inserts. */
710 gsi_start_edge (edge e
)
712 return gsi_start (PENDING_STMT (e
));
715 /* Insert the statement pointed-to by GSI into edge E. Every attempt
716 is made to place the statement in an existing basic block, but
717 sometimes that isn't possible. When it isn't possible, the edge is
718 split and the statement is added to the new block.
720 In all cases, the returned *GSI points to the correct location. The
721 return value is true if insertion should be done after the location,
722 or false if it should be done before the location. If a new basic block
723 has to be created, it is stored in *NEW_BB. */
726 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
729 basic_block dest
, src
;
734 /* If the destination has one predecessor which has no PHI nodes,
735 insert there. Except for the exit block.
737 The requirement for no PHI nodes could be relaxed. Basically we
738 would have to examine the PHIs to prove that none of them used
739 the value set by the statement we want to insert on E. That
740 hardly seems worth the effort. */
742 if (single_pred_p (dest
)
743 && gimple_seq_empty_p (phi_nodes (dest
))
744 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
746 *gsi
= gsi_start_bb (dest
);
747 if (gsi_end_p (*gsi
))
750 /* Make sure we insert after any leading labels. */
751 tmp
= gsi_stmt (*gsi
);
752 while (gimple_code (tmp
) == GIMPLE_LABEL
)
755 if (gsi_end_p (*gsi
))
757 tmp
= gsi_stmt (*gsi
);
760 if (gsi_end_p (*gsi
))
762 *gsi
= gsi_last_bb (dest
);
769 /* If the source has one successor, the edge is not abnormal and
770 the last statement does not end a basic block, insert there.
771 Except for the entry block. */
773 if ((e
->flags
& EDGE_ABNORMAL
) == 0
774 && single_succ_p (src
)
775 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
777 *gsi
= gsi_last_bb (src
);
778 if (gsi_end_p (*gsi
))
781 tmp
= gsi_stmt (*gsi
);
782 if (!stmt_ends_bb_p (tmp
))
785 switch (gimple_code (tmp
))
795 /* Otherwise, create a new basic block, and split this edge. */
796 dest
= split_edge (e
);
799 e
= single_pred_edge (dest
);
804 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
805 block has to be created, it is returned. */
808 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
810 gimple_stmt_iterator gsi
;
811 basic_block new_bb
= NULL
;
814 gcc_assert (!PENDING_STMT (e
));
816 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
818 update_call_edge_frequencies (stmt
, gsi
.bb
);
821 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
823 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
828 /* Insert STMTS on edge E. If a new block has to be created, it
832 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
834 gimple_stmt_iterator gsi
;
835 basic_block new_bb
= NULL
;
838 gcc_assert (!PENDING_STMT (e
));
840 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
841 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
844 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
846 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
851 /* This routine will commit all pending edge insertions, creating any new
852 basic blocks which are necessary. */
855 gsi_commit_edge_inserts (void)
861 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
864 FOR_EACH_BB_FN (bb
, cfun
)
865 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
866 gsi_commit_one_edge_insert (e
, NULL
);
870 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
871 to this block, otherwise set it to NULL. */
874 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
879 if (PENDING_STMT (e
))
881 gimple_stmt_iterator gsi
;
882 gimple_seq seq
= PENDING_STMT (e
);
885 PENDING_STMT (e
) = NULL
;
887 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
888 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
891 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
893 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
897 /* Returns iterator at the start of the list of phi nodes of BB. */
900 gsi_start_phis (basic_block bb
)
902 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
904 /* Adapted from gsi_start_1. */
907 i
.ptr
= gimple_seq_first (*pseq
);
909 i
.bb
= i
.ptr
? gimple_bb (i
.ptr
) : NULL
;