1 /* Iterator routines for GIMPLE statements.
2 Copyright (C) 2007-2013 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"
27 #include "tree-flow.h"
28 #include "value-prof.h"
31 /* Mark the statement STMT as modified, and update it. */
34 update_modified_stmt (gimple stmt
)
36 if (!ssa_operands_active (cfun
))
38 update_stmt_if_modified (stmt
);
42 /* Mark the statements in SEQ as modified, and update them. */
45 update_modified_stmts (gimple_seq seq
)
47 gimple_stmt_iterator gsi
;
49 if (!ssa_operands_active (cfun
))
51 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
52 update_stmt_if_modified (gsi_stmt (gsi
));
56 /* Set BB to be the basic block for all the statements in the list
57 starting at FIRST and LAST. */
60 update_bb_for_stmts (gimple_seq_node first
, gimple_seq_node last
,
65 for (n
= first
; n
; n
= n
->gsbase
.next
)
67 gimple_set_bb (n
, bb
);
73 /* Set the frequencies for the cgraph_edges for each of the calls
74 starting at FIRST for their new position within BB. */
77 update_call_edge_frequencies (gimple_seq_node first
, basic_block bb
)
79 struct cgraph_node
*cfun_node
= NULL
;
83 for (n
= first
; n
; n
= n
->gsbase
.next
)
84 if (is_gimple_call (n
))
86 struct cgraph_edge
*e
;
88 /* These function calls are expensive enough that we want
89 to avoid calling them if we never see any calls. */
90 if (cfun_node
== NULL
)
92 cfun_node
= cgraph_get_node (current_function_decl
);
93 bb_freq
= (compute_call_stmt_bb_frequency
94 (current_function_decl
, bb
));
97 e
= cgraph_edge (cfun_node
, n
);
99 e
->frequency
= bb_freq
;
103 /* Insert the sequence delimited by nodes FIRST and LAST before
104 iterator I. M specifies how to update iterator I after insertion
105 (see enum gsi_iterator_update).
107 This routine assumes that there is a forward and backward path
108 between FIRST and LAST (i.e., they are linked in a doubly-linked
109 list). Additionally, if FIRST == LAST, this routine will properly
110 insert a single node. */
113 gsi_insert_seq_nodes_before (gimple_stmt_iterator
*i
,
114 gimple_seq_node first
,
115 gimple_seq_node last
,
116 enum gsi_iterator_update mode
)
119 gimple_seq_node cur
= i
->ptr
;
121 gcc_assert (!cur
|| cur
->gsbase
.prev
);
123 if ((bb
= gsi_bb (*i
)) != NULL
)
124 update_bb_for_stmts (first
, last
, bb
);
126 /* Link SEQ before CUR in the sequence. */
129 first
->gsbase
.prev
= cur
->gsbase
.prev
;
130 if (first
->gsbase
.prev
->gsbase
.next
)
131 first
->gsbase
.prev
->gsbase
.next
= first
;
133 gimple_seq_set_first (i
->seq
, first
);
134 last
->gsbase
.next
= cur
;
135 cur
->gsbase
.prev
= last
;
139 gimple_seq_node itlast
= gimple_seq_last (*i
->seq
);
141 /* If CUR is NULL, we link at the end of the sequence (this case happens
142 when gsi_after_labels is called for a basic block that contains only
143 labels, so it returns an iterator after the end of the block, and
144 we need to insert before it; it might be cleaner to add a flag to the
145 iterator saying whether we are at the start or end of the list). */
146 last
->gsbase
.next
= NULL
;
149 first
->gsbase
.prev
= itlast
;
150 itlast
->gsbase
.next
= first
;
153 gimple_seq_set_first (i
->seq
, first
);
154 gimple_seq_set_last (i
->seq
, last
);
157 /* Update the iterator, if requested. */
161 case GSI_CONTINUE_LINKING
:
172 /* Inserts the sequence of statements SEQ before the statement pointed
173 by iterator I. MODE indicates what to do with the iterator after
174 insertion (see enum gsi_iterator_update).
176 This function does not scan for new operands. It is provided for
177 the use of the gimplifier, which manipulates statements for which
178 def/use information has not yet been constructed. Most callers
179 should use gsi_insert_seq_before. */
182 gsi_insert_seq_before_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
183 enum gsi_iterator_update mode
)
185 gimple_seq_node first
, last
;
190 /* Don't allow inserting a sequence into itself. */
191 gcc_assert (seq
!= *i
->seq
);
193 first
= gimple_seq_first (seq
);
194 last
= gimple_seq_last (seq
);
196 /* Empty sequences need no work. */
199 gcc_assert (first
== last
);
203 gsi_insert_seq_nodes_before (i
, first
, last
, mode
);
207 /* Inserts the sequence of statements SEQ before the statement pointed
208 by iterator I. MODE indicates what to do with the iterator after
209 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
213 gsi_insert_seq_before (gimple_stmt_iterator
*i
, gimple_seq seq
,
214 enum gsi_iterator_update mode
)
216 update_modified_stmts (seq
);
217 gsi_insert_seq_before_without_update (i
, seq
, mode
);
221 /* Insert the sequence delimited by nodes FIRST and LAST after
222 iterator I. M specifies how to update iterator I after insertion
223 (see enum gsi_iterator_update).
225 This routine assumes that there is a forward and backward path
226 between FIRST and LAST (i.e., they are linked in a doubly-linked
227 list). Additionally, if FIRST == LAST, this routine will properly
228 insert a single node. */
231 gsi_insert_seq_nodes_after (gimple_stmt_iterator
*i
,
232 gimple_seq_node first
,
233 gimple_seq_node last
,
234 enum gsi_iterator_update m
)
237 gimple_seq_node cur
= i
->ptr
;
239 gcc_assert (!cur
|| cur
->gsbase
.prev
);
241 /* If the iterator is inside a basic block, we need to update the
242 basic block information for all the nodes between FIRST and LAST. */
243 if ((bb
= gsi_bb (*i
)) != NULL
)
244 update_bb_for_stmts (first
, last
, bb
);
246 /* Link SEQ after CUR. */
249 last
->gsbase
.next
= cur
->gsbase
.next
;
250 if (last
->gsbase
.next
)
252 last
->gsbase
.next
->gsbase
.prev
= last
;
255 gimple_seq_set_last (i
->seq
, last
);
256 first
->gsbase
.prev
= cur
;
257 cur
->gsbase
.next
= first
;
261 gcc_assert (!gimple_seq_last (*i
->seq
));
262 last
->gsbase
.next
= NULL
;
263 gimple_seq_set_first (i
->seq
, first
);
264 gimple_seq_set_last (i
->seq
, last
);
267 /* Update the iterator, if requested. */
273 case GSI_CONTINUE_LINKING
:
285 /* Links sequence SEQ after the statement pointed-to by iterator I.
286 MODE is as in gsi_insert_after.
288 This function does not scan for new operands. It is provided for
289 the use of the gimplifier, which manipulates statements for which
290 def/use information has not yet been constructed. Most callers
291 should use gsi_insert_seq_after. */
294 gsi_insert_seq_after_without_update (gimple_stmt_iterator
*i
, gimple_seq seq
,
295 enum gsi_iterator_update mode
)
297 gimple_seq_node first
, last
;
302 /* Don't allow inserting a sequence into itself. */
303 gcc_assert (seq
!= *i
->seq
);
305 first
= gimple_seq_first (seq
);
306 last
= gimple_seq_last (seq
);
308 /* Empty sequences need no work. */
311 gcc_assert (first
== last
);
315 gsi_insert_seq_nodes_after (i
, first
, last
, mode
);
319 /* Links sequence SEQ after the statement pointed-to by iterator I.
320 MODE is as in gsi_insert_after. Scan the statements in SEQ
324 gsi_insert_seq_after (gimple_stmt_iterator
*i
, gimple_seq seq
,
325 enum gsi_iterator_update mode
)
327 update_modified_stmts (seq
);
328 gsi_insert_seq_after_without_update (i
, seq
, mode
);
332 /* Move all statements in the sequence after I to a new sequence.
333 Return this new sequence. */
336 gsi_split_seq_after (gimple_stmt_iterator i
)
338 gimple_seq_node cur
, next
;
339 gimple_seq
*pold_seq
, new_seq
;
343 /* How can we possibly split after the end, or before the beginning? */
344 gcc_assert (cur
&& cur
->gsbase
.next
);
345 next
= cur
->gsbase
.next
;
349 gimple_seq_set_first (&new_seq
, next
);
350 gimple_seq_set_last (&new_seq
, gimple_seq_last (*pold_seq
));
351 gimple_seq_set_last (pold_seq
, cur
);
352 cur
->gsbase
.next
= NULL
;
358 /* Set the statement to which GSI points to STMT. This only updates
359 the iterator and the gimple sequence, it doesn't do the bookkeeping
363 gsi_set_stmt (gimple_stmt_iterator
*gsi
, gimple stmt
)
365 gimple orig_stmt
= gsi_stmt (*gsi
);
368 stmt
->gsbase
.next
= next
= orig_stmt
->gsbase
.next
;
369 stmt
->gsbase
.prev
= prev
= orig_stmt
->gsbase
.prev
;
370 /* Note how we don't clear next/prev of orig_stmt. This is so that
371 copies of *GSI our callers might still hold (to orig_stmt)
372 can be advanced as if they too were replaced. */
373 if (prev
->gsbase
.next
)
374 prev
->gsbase
.next
= stmt
;
376 gimple_seq_set_first (gsi
->seq
, stmt
);
378 next
->gsbase
.prev
= stmt
;
380 gimple_seq_set_last (gsi
->seq
, stmt
);
386 /* Move all statements in the sequence before I to a new sequence.
387 Return this new sequence. I is set to the head of the new list. */
390 gsi_split_seq_before (gimple_stmt_iterator
*i
, gimple_seq
*pnew_seq
)
392 gimple_seq_node cur
, prev
;
397 /* How can we possibly split after the end? */
399 prev
= cur
->gsbase
.prev
;
402 if (!prev
->gsbase
.next
)
406 /* Set the limits on NEW_SEQ. */
407 gimple_seq_set_first (pnew_seq
, cur
);
408 gimple_seq_set_last (pnew_seq
, gimple_seq_last (old_seq
));
410 /* Cut OLD_SEQ before I. */
411 gimple_seq_set_last (&old_seq
, prev
);
412 if (prev
->gsbase
.next
)
413 prev
->gsbase
.next
= NULL
;
417 /* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
418 is true, the exception handling information of the original
419 statement is moved to the new statement. Assignments must only be
420 replaced with assignments to the same LHS. */
423 gsi_replace (gimple_stmt_iterator
*gsi
, gimple stmt
, bool update_eh_info
)
425 gimple orig_stmt
= gsi_stmt (*gsi
);
427 if (stmt
== orig_stmt
)
430 gcc_assert (!gimple_has_lhs (orig_stmt
) || !gimple_has_lhs (stmt
)
431 || gimple_get_lhs (orig_stmt
) == gimple_get_lhs (stmt
));
433 gimple_set_location (stmt
, gimple_location (orig_stmt
));
434 gimple_set_bb (stmt
, gsi_bb (*gsi
));
436 /* Preserve EH region information from the original statement, if
437 requested by the caller. */
439 maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
);
441 gimple_duplicate_stmt_histograms (cfun
, stmt
, cfun
, orig_stmt
);
443 /* Free all the data flow information for ORIG_STMT. */
444 gimple_set_bb (orig_stmt
, NULL
);
445 gimple_remove_stmt_histograms (cfun
, orig_stmt
);
446 delink_stmt_imm_use (orig_stmt
);
448 gsi_set_stmt (gsi
, stmt
);
449 gimple_set_modified (stmt
, true);
450 update_modified_stmt (stmt
);
454 /* Replace the statement pointed-to by GSI with the sequence SEQ.
455 If UPDATE_EH_INFO is true, the exception handling information of
456 the original statement is moved to the last statement of the new
457 sequence. If the old statement is an assignment, then so must
458 be the last statement of the new sequence, and they must have the
462 gsi_replace_with_seq (gimple_stmt_iterator
*gsi
, gimple_seq seq
,
465 gimple_stmt_iterator seqi
;
467 if (gimple_seq_empty_p (seq
))
469 gsi_remove (gsi
, true);
472 seqi
= gsi_last (seq
);
473 last
= gsi_stmt (seqi
);
474 gsi_remove (&seqi
, false);
475 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
476 gsi_replace (gsi
, last
, update_eh_info
);
480 /* Insert statement STMT before the statement pointed-to by iterator I.
481 M specifies how to update iterator I after insertion (see enum
482 gsi_iterator_update).
484 This function does not scan for new operands. It is provided for
485 the use of the gimplifier, which manipulates statements for which
486 def/use information has not yet been constructed. Most callers
487 should use gsi_insert_before. */
490 gsi_insert_before_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
491 enum gsi_iterator_update m
)
493 gsi_insert_seq_nodes_before (i
, stmt
, stmt
, m
);
496 /* Insert statement STMT before the statement pointed-to by iterator I.
497 Update STMT's basic block and scan it for new operands. M
498 specifies how to update iterator I after insertion (see enum
499 gsi_iterator_update). */
502 gsi_insert_before (gimple_stmt_iterator
*i
, gimple stmt
,
503 enum gsi_iterator_update m
)
505 update_modified_stmt (stmt
);
506 gsi_insert_before_without_update (i
, stmt
, m
);
510 /* Insert statement STMT after the statement pointed-to by iterator I.
511 M specifies how to update iterator I after insertion (see enum
512 gsi_iterator_update).
514 This function does not scan for new operands. It is provided for
515 the use of the gimplifier, which manipulates statements for which
516 def/use information has not yet been constructed. Most callers
517 should use gsi_insert_after. */
520 gsi_insert_after_without_update (gimple_stmt_iterator
*i
, gimple stmt
,
521 enum gsi_iterator_update m
)
523 gsi_insert_seq_nodes_after (i
, stmt
, stmt
, m
);
527 /* Insert statement STMT after the statement pointed-to by iterator I.
528 Update STMT's basic block and scan it for new operands. M
529 specifies how to update iterator I after insertion (see enum
530 gsi_iterator_update). */
533 gsi_insert_after (gimple_stmt_iterator
*i
, gimple stmt
,
534 enum gsi_iterator_update m
)
536 update_modified_stmt (stmt
);
537 gsi_insert_after_without_update (i
, stmt
, m
);
541 /* Remove the current stmt from the sequence. The iterator is updated
542 to point to the next statement.
544 REMOVE_PERMANENTLY is true when the statement is going to be removed
545 from the IL and not reinserted elsewhere. In that case we remove the
546 statement pointed to by iterator I from the EH tables, and free its
547 operand caches. Otherwise we do not modify this information. Returns
548 true whether EH edge cleanup is required. */
551 gsi_remove (gimple_stmt_iterator
*i
, bool remove_permanently
)
553 gimple_seq_node cur
, next
, prev
;
554 gimple stmt
= gsi_stmt (*i
);
555 bool require_eh_edge_purge
= false;
557 if (gimple_code (stmt
) != GIMPLE_PHI
)
558 insert_debug_temps_for_defs (i
);
560 /* Free all the data flow information for STMT. */
561 gimple_set_bb (stmt
, NULL
);
562 delink_stmt_imm_use (stmt
);
563 gimple_set_modified (stmt
, true);
565 if (remove_permanently
)
567 require_eh_edge_purge
= remove_stmt_from_eh_lp (stmt
);
568 gimple_remove_stmt_histograms (cfun
, stmt
);
571 /* Update the iterator and re-wire the links in I->SEQ. */
573 next
= cur
->gsbase
.next
;
574 prev
= cur
->gsbase
.prev
;
575 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
578 /* Cur is not last. */
579 next
->gsbase
.prev
= prev
;
580 else if (prev
->gsbase
.next
)
581 /* Cur is last but not first. */
582 gimple_seq_set_last (i
->seq
, prev
);
584 if (prev
->gsbase
.next
)
585 /* Cur is not first. */
586 prev
->gsbase
.next
= next
;
593 return require_eh_edge_purge
;
597 /* Finds iterator for STMT. */
600 gsi_for_stmt (gimple stmt
)
602 gimple_stmt_iterator i
;
603 basic_block bb
= gimple_bb (stmt
);
605 if (gimple_code (stmt
) == GIMPLE_PHI
)
606 i
= gsi_start_phis (bb
);
608 i
= gsi_start_bb (bb
);
615 /* Move the statement at FROM so it comes right after the statement at TO. */
618 gsi_move_after (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
620 gimple stmt
= gsi_stmt (*from
);
621 gsi_remove (from
, false);
623 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
624 move statements to an empty block. */
625 gsi_insert_after (to
, stmt
, GSI_NEW_STMT
);
629 /* Move the statement at FROM so it comes right before the statement
633 gsi_move_before (gimple_stmt_iterator
*from
, gimple_stmt_iterator
*to
)
635 gimple stmt
= gsi_stmt (*from
);
636 gsi_remove (from
, false);
638 /* For consistency with gsi_move_after, it might be better to have
639 GSI_NEW_STMT here; however, that breaks several places that expect
640 that TO does not change. */
641 gsi_insert_before (to
, stmt
, GSI_SAME_STMT
);
645 /* Move the statement at FROM to the end of basic block BB. */
648 gsi_move_to_bb_end (gimple_stmt_iterator
*from
, basic_block bb
)
650 gimple_stmt_iterator last
= gsi_last_bb (bb
);
651 gcc_checking_assert (gsi_bb (last
) == bb
);
653 /* Have to check gsi_end_p because it could be an empty block. */
654 if (!gsi_end_p (last
) && is_ctrl_stmt (gsi_stmt (last
)))
655 gsi_move_before (from
, &last
);
657 gsi_move_after (from
, &last
);
661 /* Add STMT to the pending list of edge E. No actual insertion is
662 made until a call to gsi_commit_edge_inserts () is made. */
665 gsi_insert_on_edge (edge e
, gimple stmt
)
667 gimple_seq_add_stmt (&PENDING_STMT (e
), stmt
);
670 /* Add the sequence of statements SEQ to the pending list of edge E.
671 No actual insertion is made until a call to gsi_commit_edge_inserts
675 gsi_insert_seq_on_edge (edge e
, gimple_seq seq
)
677 gimple_seq_add_seq (&PENDING_STMT (e
), seq
);
681 /* Insert the statement pointed-to by GSI into edge E. Every attempt
682 is made to place the statement in an existing basic block, but
683 sometimes that isn't possible. When it isn't possible, the edge is
684 split and the statement is added to the new block.
686 In all cases, the returned *GSI points to the correct location. The
687 return value is true if insertion should be done after the location,
688 or false if it should be done before the location. If a new basic block
689 has to be created, it is stored in *NEW_BB. */
692 gimple_find_edge_insert_loc (edge e
, gimple_stmt_iterator
*gsi
,
695 basic_block dest
, src
;
700 /* If the destination has one predecessor which has no PHI nodes,
701 insert there. Except for the exit block.
703 The requirement for no PHI nodes could be relaxed. Basically we
704 would have to examine the PHIs to prove that none of them used
705 the value set by the statement we want to insert on E. That
706 hardly seems worth the effort. */
708 if (single_pred_p (dest
)
709 && gimple_seq_empty_p (phi_nodes (dest
))
710 && dest
!= EXIT_BLOCK_PTR
)
712 *gsi
= gsi_start_bb (dest
);
713 if (gsi_end_p (*gsi
))
716 /* Make sure we insert after any leading labels. */
717 tmp
= gsi_stmt (*gsi
);
718 while (gimple_code (tmp
) == GIMPLE_LABEL
)
721 if (gsi_end_p (*gsi
))
723 tmp
= gsi_stmt (*gsi
);
726 if (gsi_end_p (*gsi
))
728 *gsi
= gsi_last_bb (dest
);
735 /* If the source has one successor, the edge is not abnormal and
736 the last statement does not end a basic block, insert there.
737 Except for the entry block. */
739 if ((e
->flags
& EDGE_ABNORMAL
) == 0
740 && single_succ_p (src
)
741 && src
!= ENTRY_BLOCK_PTR
)
743 *gsi
= gsi_last_bb (src
);
744 if (gsi_end_p (*gsi
))
747 tmp
= gsi_stmt (*gsi
);
748 if (!stmt_ends_bb_p (tmp
))
751 switch (gimple_code (tmp
))
761 /* Otherwise, create a new basic block, and split this edge. */
762 dest
= split_edge (e
);
765 e
= single_pred_edge (dest
);
770 /* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
771 block has to be created, it is returned. */
774 gsi_insert_on_edge_immediate (edge e
, gimple stmt
)
776 gimple_stmt_iterator gsi
;
777 basic_block new_bb
= NULL
;
780 gcc_assert (!PENDING_STMT (e
));
782 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
784 update_call_edge_frequencies (stmt
, gsi
.bb
);
787 gsi_insert_after (&gsi
, stmt
, GSI_NEW_STMT
);
789 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
794 /* Insert STMTS on edge E. If a new block has to be created, it
798 gsi_insert_seq_on_edge_immediate (edge e
, gimple_seq stmts
)
800 gimple_stmt_iterator gsi
;
801 basic_block new_bb
= NULL
;
804 gcc_assert (!PENDING_STMT (e
));
806 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, &new_bb
);
807 update_call_edge_frequencies (gimple_seq_first (stmts
), gsi
.bb
);
810 gsi_insert_seq_after (&gsi
, stmts
, GSI_NEW_STMT
);
812 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
817 /* This routine will commit all pending edge insertions, creating any new
818 basic blocks which are necessary. */
821 gsi_commit_edge_inserts (void)
827 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR
), NULL
);
830 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
831 gsi_commit_one_edge_insert (e
, NULL
);
835 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
836 to this block, otherwise set it to NULL. */
839 gsi_commit_one_edge_insert (edge e
, basic_block
*new_bb
)
844 if (PENDING_STMT (e
))
846 gimple_stmt_iterator gsi
;
847 gimple_seq seq
= PENDING_STMT (e
);
850 PENDING_STMT (e
) = NULL
;
852 ins_after
= gimple_find_edge_insert_loc (e
, &gsi
, new_bb
);
853 update_call_edge_frequencies (gimple_seq_first (seq
), gsi
.bb
);
856 gsi_insert_seq_after (&gsi
, seq
, GSI_NEW_STMT
);
858 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
862 /* Returns iterator at the start of the list of phi nodes of BB. */
865 gsi_start_phis (basic_block bb
)
867 gimple_seq
*pseq
= phi_nodes_ptr (bb
);
868 return gsi_start_1 (pseq
);