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"
46 #include "plugin-api.h"
50 #include "tree-phinodes.h"
51 #include "ssa-iterators.h"
53 #include "value-prof.h"
56 /* Mark the statement STMT as modified, and update it. */
59 update_modified_stmt (gimple stmt
)
61 if (!ssa_operands_active (cfun
))
63 update_stmt_if_modified (stmt
);
67 /* Mark the statements in SEQ as modified, and update them. */
70 update_modified_stmts (gimple_seq seq
)
72 gimple_stmt_iterator gsi
;
74 if (!ssa_operands_active (cfun
))
76 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
77 update_stmt_if_modified (gsi_stmt (gsi
));
81 /* Set BB to be the basic block for all the statements in the list
82 starting at FIRST and LAST. */
85 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
90 for (n
= first
; n
; n
= n
->next
)
92 gimple_set_bb (n
, bb
);
98 /* Set the frequencies for the cgraph_edges for each of the calls
99 starting at FIRST for their new position within BB. */
102 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
104 struct cgraph_node
*cfun_node
= NULL
;
108 for (n
= first
; n
; n
= n
->next
)
109 if (is_gimple_call (n
))
111 struct cgraph_edge
*e
;
113 /* These function calls are expensive enough that we want
114 to avoid calling them if we never see any calls. */
115 if (cfun_node
== NULL
)
117 cfun_node
= cgraph_node::get (current_function_decl
);
118 bb_freq
= (compute_call_stmt_bb_frequency
119 (current_function_decl
, bb
));
122 e
= cfun_node
->get_edge (n
);
124 e
->frequency
= bb_freq
;
128 /* Insert the sequence delimited by nodes FIRST and LAST before
129 iterator I. M specifies how to update iterator I after insertion
130 (see enum gsi_iterator_update).
132 This routine assumes that there is a forward and backward path
133 between FIRST and LAST (i.e., they are linked in a doubly-linked
134 list). Additionally, if FIRST == LAST, this routine will properly
135 insert a single node. */
138 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
139 gimple_seq_node first
,
140 gimple_seq_node last
,
141 enum gsi_iterator_update mode
)
144 gimple_seq_node cur
= i
->ptr
;
146 gcc_assert (!cur
|| cur
->prev
);
148 if ((bb
= gsi_bb (*i
)) != NULL
)
149 update_bb_for_stmts (first
, last
, bb
);
151 /* Link SEQ before CUR in the sequence. */
154 first
->prev
= cur
->prev
;
155 if (first
->prev
->next
)
156 first
->prev
->next
= first
;
158 gimple_seq_set_first (i
->seq
, first
);
164 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
166 /* If CUR is NULL, we link at the end of the sequence (this case happens
167 when gsi_after_labels is called for a basic block that contains only
168 labels, so it returns an iterator after the end of the block, and
169 we need to insert before it; it might be cleaner to add a flag to the
170 iterator saying whether we are at the start or end of the list). */
174 first
->prev
= itlast
;
175 itlast
->next
= first
;
178 gimple_seq_set_first (i
->seq
, first
);
179 gimple_seq_set_last (i
->seq
, last
);
182 /* Update the iterator, if requested. */
186 case GSI_CONTINUE_LINKING
:
197 /* Inserts the sequence of statements SEQ before the statement pointed
198 by iterator I. MODE indicates what to do with the iterator after
199 insertion (see enum gsi_iterator_update).
201 This function does not scan for new operands. It is provided for
202 the use of the gimplifier, which manipulates statements for which
203 def/use information has not yet been constructed. Most callers
204 should use gsi_insert_seq_before. */
207 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
208 enum gsi_iterator_update mode
)
210 gimple_seq_node first
, last
;
215 /* Don't allow inserting a sequence into itself. */
216 gcc_assert (seq
!= *i
->seq
);
218 first
= gimple_seq_first (seq
);
219 last
= gimple_seq_last (seq
);
221 /* Empty sequences need no work. */
224 gcc_assert (first
== last
);
228 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
232 /* Inserts the sequence of statements SEQ before the statement pointed
233 by iterator I. MODE indicates what to do with the iterator after
234 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
238 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
239 enum gsi_iterator_update mode
)
241 update_modified_stmts (seq
);
242 gsi_insert_seq_before_without_update (i
, seq
, mode
);
246 /* Insert the sequence delimited by nodes FIRST and LAST after
247 iterator I. M specifies how to update iterator I after insertion
248 (see enum gsi_iterator_update).
250 This routine assumes that there is a forward and backward path
251 between FIRST and LAST (i.e., they are linked in a doubly-linked
252 list). Additionally, if FIRST == LAST, this routine will properly
253 insert a single node. */
256 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
257 gimple_seq_node first
,
258 gimple_seq_node last
,
259 enum gsi_iterator_update m
)
262 gimple_seq_node cur
= i
->ptr
;
264 gcc_assert (!cur
|| cur
->prev
);
266 /* If the iterator is inside a basic block, we need to update the
267 basic block information for all the nodes between FIRST and LAST. */
268 if ((bb
= gsi_bb (*i
)) != NULL
)
269 update_bb_for_stmts (first
, last
, bb
);
271 /* Link SEQ after CUR. */
274 last
->next
= cur
->next
;
277 last
->next
->prev
= last
;
280 gimple_seq_set_last (i
->seq
, last
);
286 gcc_assert (!gimple_seq_last (*i
->seq
));
288 gimple_seq_set_first (i
->seq
, first
);
289 gimple_seq_set_last (i
->seq
, last
);
292 /* Update the iterator, if requested. */
298 case GSI_CONTINUE_LINKING
:
310 /* Links sequence SEQ after the statement pointed-to by iterator I.
311 MODE is as in gsi_insert_after.
313 This function does not scan for new operands. It is provided for
314 the use of the gimplifier, which manipulates statements for which
315 def/use information has not yet been constructed. Most callers
316 should use gsi_insert_seq_after. */
319 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
320 enum gsi_iterator_update mode
)
322 gimple_seq_node first
, last
;
327 /* Don't allow inserting a sequence into itself. */
328 gcc_assert (seq
!= *i
->seq
);
330 first
= gimple_seq_first (seq
);
331 last
= gimple_seq_last (seq
);
333 /* Empty sequences need no work. */
336 gcc_assert (first
== last
);
340 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
344 /* Links sequence SEQ after the statement pointed-to by iterator I.
345 MODE is as in gsi_insert_after. Scan the statements in SEQ
349 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
350 enum gsi_iterator_update mode
)
352 update_modified_stmts (seq
);
353 gsi_insert_seq_after_without_update (i
, seq
, mode
);
357 /* Move all statements in the sequence after I to a new sequence.
358 Return this new sequence. */
361 gsi_split_seq_after (gimple_stmt_iterator i
)
363 gimple_seq_node cur
, next
;
364 gimple_seq
*pold_seq
, new_seq
;
368 /* How can we possibly split after the end, or before the beginning? */
369 gcc_assert (cur
&& cur
->next
);
374 gimple_seq_set_first (&new_seq
, next
);
375 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
376 gimple_seq_set_last (pold_seq
, cur
);
383 /* Set the statement to which GSI points to STMT. This only updates
384 the iterator and the gimple sequence, it doesn't do the bookkeeping
388 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
390 gimple orig_stmt
= gsi_stmt (*gsi
);
393 stmt
->next
= next
= orig_stmt
->next
;
394 stmt
->prev
= prev
= orig_stmt
->prev
;
395 /* Note how we don't clear next/prev of orig_stmt. This is so that
396 copies of *GSI our callers might still hold (to orig_stmt)
397 can be advanced as if they too were replaced. */
401 gimple_seq_set_first (gsi
->seq
, stmt
);
405 gimple_seq_set_last (gsi
->seq
, stmt
);
411 /* Move all statements in the sequence before I to a new sequence.
412 Return this new sequence. I is set to the head of the new list. */
415 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
417 gimple_seq_node cur
, prev
;
422 /* How can we possibly split after the end? */
431 /* Set the limits on NEW_SEQ. */
432 gimple_seq_set_first (pnew_seq
, cur
);
433 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
435 /* Cut OLD_SEQ before I. */
436 gimple_seq_set_last (&old_seq
, prev
);
442 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
443 is true, the exception handling information of the original
444 statement is moved to the new statement. Assignments must only be
445 replaced with assignments to the same LHS. Returns whether EH edge
446 cleanup is required. */
449 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
451 gimple orig_stmt
= gsi_stmt (*gsi
);
452 bool require_eh_edge_purge
= false;
454 if (stmt
== orig_stmt
)
457 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
458 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
460 gimple_set_location (stmt
, gimple_location (orig_stmt
));
461 gimple_set_bb (stmt
, gsi_bb (*gsi
));
463 /* Preserve EH region information from the original statement, if
464 requested by the caller. */
466 require_eh_edge_purge
= maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
468 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
470 /* Free all the data flow information for ORIG_STMT. */
471 gimple_set_bb (orig_stmt
, NULL
);
472 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
473 delink_stmt_imm_use (orig_stmt
);
475 gsi_set_stmt (gsi
, stmt
);
476 gimple_set_modified (stmt
, true);
477 update_modified_stmt (stmt
);
478 return require_eh_edge_purge
;
482 /* Replace the statement pointed-to by GSI with the sequence SEQ.
483 If UPDATE_EH_INFO is true, the exception handling information of
484 the original statement is moved to the last statement of the new
485 sequence. If the old statement is an assignment, then so must
486 be the last statement of the new sequence, and they must have the
490 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
493 gimple_stmt_iterator seqi
;
495 if (gimple_seq_empty_p (seq
))
497 gsi_remove (gsi
, true);
500 seqi
= gsi_last (seq
);
501 last
= gsi_stmt (seqi
);
502 gsi_remove (&seqi
, false);
503 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
504 gsi_replace (gsi
, last
, update_eh_info
);
508 /* Insert statement STMT before the statement pointed-to by iterator I.
509 M specifies how to update iterator I after insertion (see enum
510 gsi_iterator_update).
512 This function does not scan for new operands. It is provided for
513 the use of the gimplifier, which manipulates statements for which
514 def/use information has not yet been constructed. Most callers
515 should use gsi_insert_before. */
518 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
519 enum gsi_iterator_update m
)
521 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
524 /* Insert statement STMT before the statement pointed-to by iterator I.
525 Update STMT's basic block and scan it for new operands. M
526 specifies how to update iterator I after insertion (see enum
527 gsi_iterator_update). */
530 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
531 enum gsi_iterator_update m
)
533 update_modified_stmt (stmt
);
534 gsi_insert_before_without_update (i
, stmt
, m
);
538 /* Insert statement STMT after the statement pointed-to by iterator I.
539 M specifies how to update iterator I after insertion (see enum
540 gsi_iterator_update).
542 This function does not scan for new operands. It is provided for
543 the use of the gimplifier, which manipulates statements for which
544 def/use information has not yet been constructed. Most callers
545 should use gsi_insert_after. */
548 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
549 enum gsi_iterator_update m
)
551 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
555 /* Insert statement STMT after the statement pointed-to by iterator I.
556 Update STMT's basic block and scan it for new operands. M
557 specifies how to update iterator I after insertion (see enum
558 gsi_iterator_update). */
561 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
562 enum gsi_iterator_update m
)
564 update_modified_stmt (stmt
);
565 gsi_insert_after_without_update (i
, stmt
, m
);
569 /* Remove the current stmt from the sequence. The iterator is updated
570 to point to the next statement.
572 REMOVE_PERMANENTLY is true when the statement is going to be removed
573 from the IL and not reinserted elsewhere. In that case we remove the
574 statement pointed to by iterator I from the EH tables, and free its
575 operand caches. Otherwise we do not modify this information. Returns
576 true whether EH edge cleanup is required. */
579 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
581 gimple_seq_node cur
, next
, prev
;
582 gimple stmt
= gsi_stmt (*i
);
583 bool require_eh_edge_purge
= false;
585 if (gimple_code (stmt
) != GIMPLE_PHI
)
586 insert_debug_temps_for_defs (i
);
588 /* Free all the data flow information for STMT. */
589 gimple_set_bb (stmt
, NULL
);
590 delink_stmt_imm_use (stmt
);
591 gimple_set_modified (stmt
, true);
593 if (remove_permanently
)
595 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
596 gimple_remove_stmt_histograms (cfun
, stmt
);
599 /* Update the iterator and re-wire the links in I->SEQ. */
603 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
606 /* Cur is not last. */
609 /* Cur is last but not first. */
610 gimple_seq_set_last (i
->seq
, prev
);
613 /* Cur is not first. */
621 return require_eh_edge_purge
;
625 /* Finds iterator for STMT. */
628 gsi_for_stmt (gimple stmt
)
630 gimple_stmt_iterator i
;
631 basic_block bb
= gimple_bb (stmt
);
633 if (gimple_code (stmt
) == GIMPLE_PHI
)
634 i
= gsi_start_phis (bb
);
636 i
= gsi_start_bb (bb
);
643 /* Move the statement at FROM so it comes right after the statement at TO. */
646 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
648 gimple stmt
= gsi_stmt (*from
);
649 gsi_remove (from
, false);
651 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
652 move statements to an empty block. */
653 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
657 /* Move the statement at FROM so it comes right before the statement
661 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
663 gimple stmt
= gsi_stmt (*from
);
664 gsi_remove (from
, false);
666 /* For consistency with gsi_move_after, it might be better to have
667 GSI_NEW_STMT here; however, that breaks several places that expect
668 that TO does not change. */
669 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
673 /* Move the statement at FROM to the end of basic block BB. */
676 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
678 gimple_stmt_iterator last
= gsi_last_bb (bb
);
679 gcc_checking_assert (gsi_bb (last
) == bb
);
681 /* Have to check gsi_end_p because it could be an empty block. */
682 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
683 gsi_move_before (from
, &last
);
685 gsi_move_after (from
, &last
);
689 /* Add STMT to the pending list of edge E. No actual insertion is
690 made until a call to gsi_commit_edge_inserts () is made. */
693 gsi_insert_on_edge (edge e
, gimple stmt
)
695 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
698 /* Add the sequence of statements SEQ to the pending list of edge E.
699 No actual insertion is made until a call to gsi_commit_edge_inserts
703 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
705 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
708 /* Return a new iterator pointing to the first statement in sequence of
709 statements on edge E. Such statements need to be subsequently moved into a
710 basic block by calling gsi_commit_edge_inserts. */
713 gsi_start_edge (edge e
)
715 return gsi_start (PENDING_STMT (e
));
718 /* Insert the statement pointed-to by GSI into edge E. Every attempt
719 is made to place the statement in an existing basic block, but
720 sometimes that isn't possible. When it isn't possible, the edge is
721 split and the statement is added to the new block.
723 In all cases, the returned *GSI points to the correct location. The
724 return value is true if insertion should be done after the location,
725 or false if it should be done before the location. If a new basic block
726 has to be created, it is stored in *NEW_BB. */
729 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
732 basic_block dest
, src
;
737 /* If the destination has one predecessor which has no PHI nodes,
738 insert there. Except for the exit block.
740 The requirement for no PHI nodes could be relaxed. Basically we
741 would have to examine the PHIs to prove that none of them used
742 the value set by the statement we want to insert on E. That
743 hardly seems worth the effort. */
745 if (single_pred_p (dest
)
746 && gimple_seq_empty_p (phi_nodes (dest
))
747 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
749 *gsi
= gsi_start_bb (dest
);
750 if (gsi_end_p (*gsi
))
753 /* Make sure we insert after any leading labels. */
754 tmp
= gsi_stmt (*gsi
);
755 while (gimple_code (tmp
) == GIMPLE_LABEL
)
758 if (gsi_end_p (*gsi
))
760 tmp
= gsi_stmt (*gsi
);
763 if (gsi_end_p (*gsi
))
765 *gsi
= gsi_last_bb (dest
);
772 /* If the source has one successor, the edge is not abnormal and
773 the last statement does not end a basic block, insert there.
774 Except for the entry block. */
776 if ((e
->flags
& EDGE_ABNORMAL
) == 0
777 && single_succ_p (src
)
778 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
780 *gsi
= gsi_last_bb (src
);
781 if (gsi_end_p (*gsi
))
784 tmp
= gsi_stmt (*gsi
);
785 if (!stmt_ends_bb_p (tmp
))
788 switch (gimple_code (tmp
))
798 /* Otherwise, create a new basic block, and split this edge. */
799 dest
= split_edge (e
);
802 e
= single_pred_edge (dest
);
807 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
808 block has to be created, it is returned. */
811 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
813 gimple_stmt_iterator gsi
;
814 basic_block new_bb
= NULL
;
817 gcc_assert (!PENDING_STMT (e
));
819 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
821 update_call_edge_frequencies (stmt
, gsi
.bb
);
824 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
826 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
831 /* Insert STMTS on edge E. If a new block has to be created, it
835 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
837 gimple_stmt_iterator gsi
;
838 basic_block new_bb
= NULL
;
841 gcc_assert (!PENDING_STMT (e
));
843 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
844 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
847 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
849 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
854 /* This routine will commit all pending edge insertions, creating any new
855 basic blocks which are necessary. */
858 gsi_commit_edge_inserts (void)
864 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
867 FOR_EACH_BB_FN (bb
, cfun
)
868 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
869 gsi_commit_one_edge_insert (e
, NULL
);
873 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
874 to this block, otherwise set it to NULL. */
877 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
882 if (PENDING_STMT (e
))
884 gimple_stmt_iterator gsi
;
885 gimple_seq seq
= PENDING_STMT (e
);
888 PENDING_STMT (e
) = NULL
;
890 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
891 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
894 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
896 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
900 /* Returns iterator at the start of the list of phi nodes of BB. */
903 gsi_start_phis (basic_block bb
)
905 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
906 return gsi_start_1 (pseq
);