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 "hard-reg-set.h"
31 #include "fold-const.h"
32 #include "internal-fn.h"
34 #include "gimple-iterator.h"
38 #include "value-prof.h"
41 /* Mark the statement STMT as modified, and update it. */
44 update_modified_stmt (gimple stmt
)
46 if (!ssa_operands_active (cfun
))
48 update_stmt_if_modified (stmt
);
52 /* Mark the statements in SEQ as modified, and update them. */
55 update_modified_stmts (gimple_seq seq
)
57 gimple_stmt_iterator gsi
;
59 if (!ssa_operands_active (cfun
))
61 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
62 update_stmt_if_modified (gsi_stmt (gsi
));
66 /* Set BB to be the basic block for all the statements in the list
67 starting at FIRST and LAST. */
70 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
75 for (n
= first
; n
; n
= n
->next
)
77 gimple_set_bb (n
, bb
);
83 /* Set the frequencies for the cgraph_edges for each of the calls
84 starting at FIRST for their new position within BB. */
87 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
89 struct cgraph_node
*cfun_node
= NULL
;
93 for (n
= first
; n
; n
= n
->next
)
94 if (is_gimple_call (n
))
96 struct cgraph_edge
*e
;
98 /* These function calls are expensive enough that we want
99 to avoid calling them if we never see any calls. */
100 if (cfun_node
== NULL
)
102 cfun_node
= cgraph_node::get (current_function_decl
);
103 bb_freq
= (compute_call_stmt_bb_frequency
104 (current_function_decl
, bb
));
107 e
= cfun_node
->get_edge (n
);
109 e
->frequency
= bb_freq
;
113 /* Insert the sequence delimited by nodes FIRST and LAST before
114 iterator I. M specifies how to update iterator I after insertion
115 (see enum gsi_iterator_update).
117 This routine assumes that there is a forward and backward path
118 between FIRST and LAST (i.e., they are linked in a doubly-linked
119 list). Additionally, if FIRST == LAST, this routine will properly
120 insert a single node. */
123 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
124 gimple_seq_node first
,
125 gimple_seq_node last
,
126 enum gsi_iterator_update mode
)
129 gimple_seq_node cur
= i
->ptr
;
131 gcc_assert (!cur
|| cur
->prev
);
133 if ((bb
= gsi_bb (*i
)) != NULL
)
134 update_bb_for_stmts (first
, last
, bb
);
136 /* Link SEQ before CUR in the sequence. */
139 first
->prev
= cur
->prev
;
140 if (first
->prev
->next
)
141 first
->prev
->next
= first
;
143 gimple_seq_set_first (i
->seq
, first
);
149 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
151 /* If CUR is NULL, we link at the end of the sequence (this case happens
152 when gsi_after_labels is called for a basic block that contains only
153 labels, so it returns an iterator after the end of the block, and
154 we need to insert before it; it might be cleaner to add a flag to the
155 iterator saying whether we are at the start or end of the list). */
159 first
->prev
= itlast
;
160 itlast
->next
= first
;
163 gimple_seq_set_first (i
->seq
, first
);
164 gimple_seq_set_last (i
->seq
, last
);
167 /* Update the iterator, if requested. */
171 case GSI_CONTINUE_LINKING
:
182 /* Inserts the sequence of statements SEQ before the statement pointed
183 by iterator I. MODE indicates what to do with the iterator after
184 insertion (see enum gsi_iterator_update).
186 This function does not scan for new operands. It is provided for
187 the use of the gimplifier, which manipulates statements for which
188 def/use information has not yet been constructed. Most callers
189 should use gsi_insert_seq_before. */
192 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
193 enum gsi_iterator_update mode
)
195 gimple_seq_node first
, last
;
200 /* Don't allow inserting a sequence into itself. */
201 gcc_assert (seq
!= *i
->seq
);
203 first
= gimple_seq_first (seq
);
204 last
= gimple_seq_last (seq
);
206 /* Empty sequences need no work. */
209 gcc_assert (first
== last
);
213 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
217 /* Inserts the sequence of statements SEQ before the statement pointed
218 by iterator I. MODE indicates what to do with the iterator after
219 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
223 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
224 enum gsi_iterator_update mode
)
226 update_modified_stmts (seq
);
227 gsi_insert_seq_before_without_update (i
, seq
, mode
);
231 /* Insert the sequence delimited by nodes FIRST and LAST after
232 iterator I. M specifies how to update iterator I after insertion
233 (see enum gsi_iterator_update).
235 This routine assumes that there is a forward and backward path
236 between FIRST and LAST (i.e., they are linked in a doubly-linked
237 list). Additionally, if FIRST == LAST, this routine will properly
238 insert a single node. */
241 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
242 gimple_seq_node first
,
243 gimple_seq_node last
,
244 enum gsi_iterator_update m
)
247 gimple_seq_node cur
= i
->ptr
;
249 gcc_assert (!cur
|| cur
->prev
);
251 /* If the iterator is inside a basic block, we need to update the
252 basic block information for all the nodes between FIRST and LAST. */
253 if ((bb
= gsi_bb (*i
)) != NULL
)
254 update_bb_for_stmts (first
, last
, bb
);
256 /* Link SEQ after CUR. */
259 last
->next
= cur
->next
;
262 last
->next
->prev
= last
;
265 gimple_seq_set_last (i
->seq
, last
);
271 gcc_assert (!gimple_seq_last (*i
->seq
));
273 gimple_seq_set_first (i
->seq
, first
);
274 gimple_seq_set_last (i
->seq
, last
);
277 /* Update the iterator, if requested. */
283 case GSI_CONTINUE_LINKING
:
295 /* Links sequence SEQ after the statement pointed-to by iterator I.
296 MODE is as in gsi_insert_after.
298 This function does not scan for new operands. It is provided for
299 the use of the gimplifier, which manipulates statements for which
300 def/use information has not yet been constructed. Most callers
301 should use gsi_insert_seq_after. */
304 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
305 enum gsi_iterator_update mode
)
307 gimple_seq_node first
, last
;
312 /* Don't allow inserting a sequence into itself. */
313 gcc_assert (seq
!= *i
->seq
);
315 first
= gimple_seq_first (seq
);
316 last
= gimple_seq_last (seq
);
318 /* Empty sequences need no work. */
321 gcc_assert (first
== last
);
325 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
329 /* Links sequence SEQ after the statement pointed-to by iterator I.
330 MODE is as in gsi_insert_after. Scan the statements in SEQ
334 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
335 enum gsi_iterator_update mode
)
337 update_modified_stmts (seq
);
338 gsi_insert_seq_after_without_update (i
, seq
, mode
);
342 /* Move all statements in the sequence after I to a new sequence.
343 Return this new sequence. */
346 gsi_split_seq_after (gimple_stmt_iterator i
)
348 gimple_seq_node cur
, next
;
349 gimple_seq
*pold_seq
, new_seq
;
353 /* How can we possibly split after the end, or before the beginning? */
354 gcc_assert (cur
&& cur
->next
);
359 gimple_seq_set_first (&new_seq
, next
);
360 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
361 gimple_seq_set_last (pold_seq
, cur
);
368 /* Set the statement to which GSI points to STMT. This only updates
369 the iterator and the gimple sequence, it doesn't do the bookkeeping
373 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
375 gimple orig_stmt
= gsi_stmt (*gsi
);
378 stmt
->next
= next
= orig_stmt
->next
;
379 stmt
->prev
= prev
= orig_stmt
->prev
;
380 /* Note how we don't clear next/prev of orig_stmt. This is so that
381 copies of *GSI our callers might still hold (to orig_stmt)
382 can be advanced as if they too were replaced. */
386 gimple_seq_set_first (gsi
->seq
, stmt
);
390 gimple_seq_set_last (gsi
->seq
, stmt
);
396 /* Move all statements in the sequence before I to a new sequence.
397 Return this new sequence. I is set to the head of the new list. */
400 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
402 gimple_seq_node cur
, prev
;
407 /* How can we possibly split after the end? */
416 /* Set the limits on NEW_SEQ. */
417 gimple_seq_set_first (pnew_seq
, cur
);
418 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
420 /* Cut OLD_SEQ before I. */
421 gimple_seq_set_last (&old_seq
, prev
);
427 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
428 is true, the exception handling information of the original
429 statement is moved to the new statement. Assignments must only be
430 replaced with assignments to the same LHS. Returns whether EH edge
431 cleanup is required. */
434 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
436 gimple orig_stmt
= gsi_stmt (*gsi
);
437 bool require_eh_edge_purge
= false;
439 if (stmt
== orig_stmt
)
442 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
443 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
445 gimple_set_location (stmt
, gimple_location (orig_stmt
));
446 gimple_set_bb (stmt
, gsi_bb (*gsi
));
448 /* Preserve EH region information from the original statement, if
449 requested by the caller. */
451 require_eh_edge_purge
= maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
453 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
455 /* Free all the data flow information for ORIG_STMT. */
456 gimple_set_bb (orig_stmt
, NULL
);
457 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
458 delink_stmt_imm_use (orig_stmt
);
460 gsi_set_stmt (gsi
, stmt
);
461 gimple_set_modified (stmt
, true);
462 update_modified_stmt (stmt
);
463 return require_eh_edge_purge
;
467 /* Replace the statement pointed-to by GSI with the sequence SEQ.
468 If UPDATE_EH_INFO is true, the exception handling information of
469 the original statement is moved to the last statement of the new
470 sequence. If the old statement is an assignment, then so must
471 be the last statement of the new sequence, and they must have the
475 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
478 gimple_stmt_iterator seqi
;
480 if (gimple_seq_empty_p (seq
))
482 gsi_remove (gsi
, true);
485 seqi
= gsi_last (seq
);
486 last
= gsi_stmt (seqi
);
487 gsi_remove (&seqi
, false);
488 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
489 gsi_replace (gsi
, last
, update_eh_info
);
493 /* Insert statement STMT before the statement pointed-to by iterator I.
494 M specifies how to update iterator I after insertion (see enum
495 gsi_iterator_update).
497 This function does not scan for new operands. It is provided for
498 the use of the gimplifier, which manipulates statements for which
499 def/use information has not yet been constructed. Most callers
500 should use gsi_insert_before. */
503 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
504 enum gsi_iterator_update m
)
506 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
509 /* Insert statement STMT before the statement pointed-to by iterator I.
510 Update STMT's basic block and scan it for new operands. M
511 specifies how to update iterator I after insertion (see enum
512 gsi_iterator_update). */
515 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
516 enum gsi_iterator_update m
)
518 update_modified_stmt (stmt
);
519 gsi_insert_before_without_update (i
, stmt
, m
);
523 /* Insert statement STMT after the statement pointed-to by iterator I.
524 M specifies how to update iterator I after insertion (see enum
525 gsi_iterator_update).
527 This function does not scan for new operands. It is provided for
528 the use of the gimplifier, which manipulates statements for which
529 def/use information has not yet been constructed. Most callers
530 should use gsi_insert_after. */
533 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
534 enum gsi_iterator_update m
)
536 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
540 /* Insert statement STMT after the statement pointed-to by iterator I.
541 Update STMT's basic block and scan it for new operands. M
542 specifies how to update iterator I after insertion (see enum
543 gsi_iterator_update). */
546 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
547 enum gsi_iterator_update m
)
549 update_modified_stmt (stmt
);
550 gsi_insert_after_without_update (i
, stmt
, m
);
554 /* Remove the current stmt from the sequence. The iterator is updated
555 to point to the next statement.
557 REMOVE_PERMANENTLY is true when the statement is going to be removed
558 from the IL and not reinserted elsewhere. In that case we remove the
559 statement pointed to by iterator I from the EH tables, and free its
560 operand caches. Otherwise we do not modify this information. Returns
561 true whether EH edge cleanup is required. */
564 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
566 gimple_seq_node cur
, next
, prev
;
567 gimple stmt
= gsi_stmt (*i
);
568 bool require_eh_edge_purge
= false;
570 if (gimple_code (stmt
) != GIMPLE_PHI
)
571 insert_debug_temps_for_defs (i
);
573 /* Free all the data flow information for STMT. */
574 gimple_set_bb (stmt
, NULL
);
575 delink_stmt_imm_use (stmt
);
576 gimple_set_modified (stmt
, true);
578 if (remove_permanently
)
580 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
581 gimple_remove_stmt_histograms (cfun
, stmt
);
584 /* Update the iterator and re-wire the links in I->SEQ. */
588 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
591 /* Cur is not last. */
594 /* Cur is last but not first. */
595 gimple_seq_set_last (i
->seq
, prev
);
598 /* Cur is not first. */
606 return require_eh_edge_purge
;
610 /* Finds iterator for STMT. */
613 gsi_for_stmt (gimple stmt
)
615 gimple_stmt_iterator i
;
616 basic_block bb
= gimple_bb (stmt
);
618 if (gimple_code (stmt
) == GIMPLE_PHI
)
619 i
= gsi_start_phis (bb
);
621 i
= gsi_start_bb (bb
);
627 /* Finds iterator for PHI. */
630 gsi_for_phi (gphi
*phi
)
633 basic_block bb
= gimple_bb (phi
);
635 i
= gsi_start_phis (bb
);
641 /* Move the statement at FROM so it comes right after the statement at TO. */
644 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
646 gimple stmt
= gsi_stmt (*from
);
647 gsi_remove (from
, false);
649 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
650 move statements to an empty block. */
651 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
655 /* Move the statement at FROM so it comes right before the statement
659 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
661 gimple stmt
= gsi_stmt (*from
);
662 gsi_remove (from
, false);
664 /* For consistency with gsi_move_after, it might be better to have
665 GSI_NEW_STMT here; however, that breaks several places that expect
666 that TO does not change. */
667 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
671 /* Move the statement at FROM to the end of basic block BB. */
674 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
676 gimple_stmt_iterator last
= gsi_last_bb (bb
);
677 gcc_checking_assert (gsi_bb (last
) == bb
);
679 /* Have to check gsi_end_p because it could be an empty block. */
680 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
681 gsi_move_before (from
, &last
);
683 gsi_move_after (from
, &last
);
687 /* Add STMT to the pending list of edge E. No actual insertion is
688 made until a call to gsi_commit_edge_inserts () is made. */
691 gsi_insert_on_edge (edge e
, gimple stmt
)
693 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
696 /* Add the sequence of statements SEQ to the pending list of edge E.
697 No actual insertion is made until a call to gsi_commit_edge_inserts
701 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
703 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
706 /* Return a new iterator pointing to the first statement in sequence of
707 statements on edge E. Such statements need to be subsequently moved into a
708 basic block by calling gsi_commit_edge_inserts. */
711 gsi_start_edge (edge e
)
713 return gsi_start (PENDING_STMT (e
));
716 /* Insert the statement pointed-to by GSI into edge E. Every attempt
717 is made to place the statement in an existing basic block, but
718 sometimes that isn't possible. When it isn't possible, the edge is
719 split and the statement is added to the new block.
721 In all cases, the returned *GSI points to the correct location. The
722 return value is true if insertion should be done after the location,
723 or false if it should be done before the location. If a new basic block
724 has to be created, it is stored in *NEW_BB. */
727 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
730 basic_block dest
, src
;
735 /* If the destination has one predecessor which has no PHI nodes,
736 insert there. Except for the exit block.
738 The requirement for no PHI nodes could be relaxed. Basically we
739 would have to examine the PHIs to prove that none of them used
740 the value set by the statement we want to insert on E. That
741 hardly seems worth the effort. */
743 if (single_pred_p (dest
)
744 && gimple_seq_empty_p (phi_nodes (dest
))
745 && dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
747 *gsi
= gsi_start_bb (dest
);
748 if (gsi_end_p (*gsi
))
751 /* Make sure we insert after any leading labels. */
752 tmp
= gsi_stmt (*gsi
);
753 while (gimple_code (tmp
) == GIMPLE_LABEL
)
756 if (gsi_end_p (*gsi
))
758 tmp
= gsi_stmt (*gsi
);
761 if (gsi_end_p (*gsi
))
763 *gsi
= gsi_last_bb (dest
);
770 /* If the source has one successor, the edge is not abnormal and
771 the last statement does not end a basic block, insert there.
772 Except for the entry block. */
774 if ((e
->flags
& EDGE_ABNORMAL
) == 0
775 && single_succ_p (src
)
776 && src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
778 *gsi
= gsi_last_bb (src
);
779 if (gsi_end_p (*gsi
))
782 tmp
= gsi_stmt (*gsi
);
783 if (!stmt_ends_bb_p (tmp
))
786 switch (gimple_code (tmp
))
796 /* Otherwise, create a new basic block, and split this edge. */
797 dest
= split_edge (e
);
800 e
= single_pred_edge (dest
);
805 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
806 block has to be created, it is returned. */
809 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
811 gimple_stmt_iterator gsi
;
812 basic_block new_bb
= NULL
;
815 gcc_assert (!PENDING_STMT (e
));
817 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
819 update_call_edge_frequencies (stmt
, gsi
.bb
);
822 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
824 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
829 /* Insert STMTS on edge E. If a new block has to be created, it
833 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
835 gimple_stmt_iterator gsi
;
836 basic_block new_bb
= NULL
;
839 gcc_assert (!PENDING_STMT (e
));
841 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
842 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
845 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
847 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
852 /* This routine will commit all pending edge insertions, creating any new
853 basic blocks which are necessary. */
856 gsi_commit_edge_inserts (void)
862 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
865 FOR_EACH_BB_FN (bb
, cfun
)
866 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
867 gsi_commit_one_edge_insert (e
, NULL
);
871 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
872 to this block, otherwise set it to NULL. */
875 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
880 if (PENDING_STMT (e
))
882 gimple_stmt_iterator gsi
;
883 gimple_seq seq
= PENDING_STMT (e
);
886 PENDING_STMT (e
) = NULL
;
888 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
889 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
892 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
894 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
898 /* Returns iterator at the start of the list of phi nodes of BB. */
901 gsi_start_phis (basic_block bb
)
903 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
905 /* Adapted from gsi_start_1. */
908 i
.ptr
= gimple_seq_first (*pseq
);
910 i
.bb
= i
.ptr
? gimple_bb (i
.ptr
) : NULL
;