]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple-iterator.cc
Fix profile update in tree_transform_and_unroll_loop
[thirdparty/gcc.git] / gcc / gimple-iterator.cc
CommitLineData
726a989a 1/* Iterator routines for GIMPLE statements.
aeee4812 2 Copyright (C) 2007-2023 Free Software Foundation, Inc.
726a989a
RB
3 Contributed by Aldy Hernandez <aldy@quesejoda.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
c7131fb2 24#include "backend.h"
40e23961 25#include "tree.h"
c7131fb2 26#include "gimple.h"
957060b5 27#include "cfghooks.h"
c7131fb2 28#include "ssa.h"
957060b5 29#include "cgraph.h"
2fb9a547 30#include "tree-eh.h"
5be5c238 31#include "gimple-iterator.h"
442b4905 32#include "tree-cfg.h"
7a300452 33#include "tree-ssa.h"
726a989a
RB
34#include "value-prof.h"
35
36
37/* Mark the statement STMT as modified, and update it. */
38
39static inline void
355fe088 40update_modified_stmt (gimple *stmt)
726a989a 41{
2eb712b4 42 if (!ssa_operands_active (cfun))
726a989a
RB
43 return;
44 update_stmt_if_modified (stmt);
45}
46
47
48/* Mark the statements in SEQ as modified, and update them. */
49
f8e89441 50void
726a989a
RB
51update_modified_stmts (gimple_seq seq)
52{
53 gimple_stmt_iterator gsi;
b8698a0f 54
2eb712b4 55 if (!ssa_operands_active (cfun))
b8698a0f 56 return;
726a989a
RB
57 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
58 update_stmt_if_modified (gsi_stmt (gsi));
59}
60
61
62/* Set BB to be the basic block for all the statements in the list
63 starting at FIRST and LAST. */
64
65static void
355a7673
MM
66update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last,
67 basic_block bb)
726a989a
RB
68{
69 gimple_seq_node n;
b8698a0f 70
daa6e488 71 for (n = first; n; n = n->next)
355a7673
MM
72 {
73 gimple_set_bb (n, bb);
74 if (n == last)
75 break;
76 }
726a989a
RB
77}
78
8b84c596
RH
79/* Set the frequencies for the cgraph_edges for each of the calls
80 starting at FIRST for their new position within BB. */
81
82static void
83update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
84{
85 struct cgraph_node *cfun_node = NULL;
8b84c596
RH
86 gimple_seq_node n;
87
daa6e488 88 for (n = first; n ; n = n->next)
355a7673 89 if (is_gimple_call (n))
8b84c596
RH
90 {
91 struct cgraph_edge *e;
92
93 /* These function calls are expensive enough that we want
94 to avoid calling them if we never see any calls. */
95 if (cfun_node == NULL)
1bad9c18 96 cfun_node = cgraph_node::get (current_function_decl);
8b84c596 97
d52f5295 98 e = cfun_node->get_edge (n);
8b84c596 99 if (e != NULL)
1bad9c18 100 e->count = bb->count;
8b84c596
RH
101 }
102}
726a989a
RB
103
104/* Insert the sequence delimited by nodes FIRST and LAST before
105 iterator I. M specifies how to update iterator I after insertion
106 (see enum gsi_iterator_update).
107
108 This routine assumes that there is a forward and backward path
109 between FIRST and LAST (i.e., they are linked in a doubly-linked
110 list). Additionally, if FIRST == LAST, this routine will properly
111 insert a single node. */
112
113static void
114gsi_insert_seq_nodes_before (gimple_stmt_iterator *i,
115 gimple_seq_node first,
116 gimple_seq_node last,
117 enum gsi_iterator_update mode)
118{
119 basic_block bb;
120 gimple_seq_node cur = i->ptr;
121
daa6e488 122 gcc_assert (!cur || cur->prev);
355a7673 123
726a989a 124 if ((bb = gsi_bb (*i)) != NULL)
355a7673 125 update_bb_for_stmts (first, last, bb);
726a989a
RB
126
127 /* Link SEQ before CUR in the sequence. */
128 if (cur)
129 {
daa6e488
DM
130 first->prev = cur->prev;
131 if (first->prev->next)
132 first->prev->next = first;
726a989a
RB
133 else
134 gimple_seq_set_first (i->seq, first);
daa6e488
DM
135 last->next = cur;
136 cur->prev = last;
726a989a
RB
137 }
138 else
139 {
355a7673 140 gimple_seq_node itlast = gimple_seq_last (*i->seq);
726a989a
RB
141
142 /* If CUR is NULL, we link at the end of the sequence (this case happens
143 when gsi_after_labels is called for a basic block that contains only
144 labels, so it returns an iterator after the end of the block, and
145 we need to insert before it; it might be cleaner to add a flag to the
146 iterator saying whether we are at the start or end of the list). */
daa6e488 147 last->next = NULL;
726a989a 148 if (itlast)
355a7673 149 {
daa6e488
DM
150 first->prev = itlast;
151 itlast->next = first;
355a7673 152 }
726a989a
RB
153 else
154 gimple_seq_set_first (i->seq, first);
155 gimple_seq_set_last (i->seq, last);
156 }
157
158 /* Update the iterator, if requested. */
159 switch (mode)
160 {
161 case GSI_NEW_STMT:
162 case GSI_CONTINUE_LINKING:
163 i->ptr = first;
164 break;
489c8f27
RB
165 case GSI_LAST_NEW_STMT:
166 i->ptr = last;
167 break;
726a989a
RB
168 case GSI_SAME_STMT:
169 break;
170 default:
171 gcc_unreachable ();
172 }
173}
174
175
176/* Inserts the sequence of statements SEQ before the statement pointed
177 by iterator I. MODE indicates what to do with the iterator after
178 insertion (see enum gsi_iterator_update).
179
180 This function does not scan for new operands. It is provided for
181 the use of the gimplifier, which manipulates statements for which
182 def/use information has not yet been constructed. Most callers
183 should use gsi_insert_seq_before. */
184
185void
186gsi_insert_seq_before_without_update (gimple_stmt_iterator *i, gimple_seq seq,
187 enum gsi_iterator_update mode)
188{
189 gimple_seq_node first, last;
190
191 if (seq == NULL)
192 return;
193
194 /* Don't allow inserting a sequence into itself. */
355a7673 195 gcc_assert (seq != *i->seq);
726a989a
RB
196
197 first = gimple_seq_first (seq);
198 last = gimple_seq_last (seq);
199
726a989a
RB
200 /* Empty sequences need no work. */
201 if (!first || !last)
202 {
203 gcc_assert (first == last);
204 return;
205 }
206
207 gsi_insert_seq_nodes_before (i, first, last, mode);
208}
209
210
211/* Inserts the sequence of statements SEQ before the statement pointed
212 by iterator I. MODE indicates what to do with the iterator after
213 insertion (see enum gsi_iterator_update). Scan the statements in SEQ
214 for new operands. */
215
216void
217gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq,
218 enum gsi_iterator_update mode)
219{
220 update_modified_stmts (seq);
221 gsi_insert_seq_before_without_update (i, seq, mode);
222}
223
224
225/* Insert the sequence delimited by nodes FIRST and LAST after
226 iterator I. M specifies how to update iterator I after insertion
227 (see enum gsi_iterator_update).
228
229 This routine assumes that there is a forward and backward path
230 between FIRST and LAST (i.e., they are linked in a doubly-linked
231 list). Additionally, if FIRST == LAST, this routine will properly
232 insert a single node. */
233
234static void
235gsi_insert_seq_nodes_after (gimple_stmt_iterator *i,
236 gimple_seq_node first,
237 gimple_seq_node last,
238 enum gsi_iterator_update m)
239{
240 basic_block bb;
241 gimple_seq_node cur = i->ptr;
242
daa6e488 243 gcc_assert (!cur || cur->prev);
355a7673 244
726a989a
RB
245 /* If the iterator is inside a basic block, we need to update the
246 basic block information for all the nodes between FIRST and LAST. */
247 if ((bb = gsi_bb (*i)) != NULL)
355a7673 248 update_bb_for_stmts (first, last, bb);
726a989a
RB
249
250 /* Link SEQ after CUR. */
251 if (cur)
252 {
daa6e488
DM
253 last->next = cur->next;
254 if (last->next)
355a7673 255 {
daa6e488 256 last->next->prev = last;
355a7673 257 }
726a989a
RB
258 else
259 gimple_seq_set_last (i->seq, last);
daa6e488
DM
260 first->prev = cur;
261 cur->next = first;
726a989a
RB
262 }
263 else
264 {
355a7673 265 gcc_assert (!gimple_seq_last (*i->seq));
daa6e488 266 last->next = NULL;
726a989a
RB
267 gimple_seq_set_first (i->seq, first);
268 gimple_seq_set_last (i->seq, last);
269 }
270
271 /* Update the iterator, if requested. */
272 switch (m)
273 {
274 case GSI_NEW_STMT:
275 i->ptr = first;
276 break;
489c8f27 277 case GSI_LAST_NEW_STMT:
726a989a
RB
278 case GSI_CONTINUE_LINKING:
279 i->ptr = last;
280 break;
281 case GSI_SAME_STMT:
282 gcc_assert (cur);
283 break;
284 default:
285 gcc_unreachable ();
286 }
287}
288
289
290/* Links sequence SEQ after the statement pointed-to by iterator I.
291 MODE is as in gsi_insert_after.
292
293 This function does not scan for new operands. It is provided for
294 the use of the gimplifier, which manipulates statements for which
295 def/use information has not yet been constructed. Most callers
296 should use gsi_insert_seq_after. */
297
298void
299gsi_insert_seq_after_without_update (gimple_stmt_iterator *i, gimple_seq seq,
300 enum gsi_iterator_update mode)
301{
302 gimple_seq_node first, last;
303
304 if (seq == NULL)
305 return;
306
307 /* Don't allow inserting a sequence into itself. */
355a7673 308 gcc_assert (seq != *i->seq);
726a989a
RB
309
310 first = gimple_seq_first (seq);
311 last = gimple_seq_last (seq);
312
726a989a
RB
313 /* Empty sequences need no work. */
314 if (!first || !last)
315 {
316 gcc_assert (first == last);
317 return;
318 }
319
320 gsi_insert_seq_nodes_after (i, first, last, mode);
321}
322
323
324/* Links sequence SEQ after the statement pointed-to by iterator I.
325 MODE is as in gsi_insert_after. Scan the statements in SEQ
326 for new operands. */
327
328void
329gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq,
330 enum gsi_iterator_update mode)
331{
332 update_modified_stmts (seq);
333 gsi_insert_seq_after_without_update (i, seq, mode);
334}
335
336
337/* Move all statements in the sequence after I to a new sequence.
338 Return this new sequence. */
339
340gimple_seq
341gsi_split_seq_after (gimple_stmt_iterator i)
342{
343 gimple_seq_node cur, next;
355a7673 344 gimple_seq *pold_seq, new_seq;
726a989a
RB
345
346 cur = i.ptr;
347
348 /* How can we possibly split after the end, or before the beginning? */
daa6e488
DM
349 gcc_assert (cur && cur->next);
350 next = cur->next;
726a989a 351
355a7673 352 pold_seq = i.seq;
726a989a 353
355a7673
MM
354 gimple_seq_set_first (&new_seq, next);
355 gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
356 gimple_seq_set_last (pold_seq, cur);
daa6e488 357 cur->next = NULL;
726a989a
RB
358
359 return new_seq;
360}
361
362
355a7673
MM
363/* Set the statement to which GSI points to STMT. This only updates
364 the iterator and the gimple sequence, it doesn't do the bookkeeping
365 of gsi_replace. */
366
367void
355fe088 368gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt)
355a7673 369{
355fe088
TS
370 gimple *orig_stmt = gsi_stmt (*gsi);
371 gimple *prev, *next;
355a7673 372
daa6e488
DM
373 stmt->next = next = orig_stmt->next;
374 stmt->prev = prev = orig_stmt->prev;
355a7673
MM
375 /* Note how we don't clear next/prev of orig_stmt. This is so that
376 copies of *GSI our callers might still hold (to orig_stmt)
377 can be advanced as if they too were replaced. */
daa6e488
DM
378 if (prev->next)
379 prev->next = stmt;
355a7673
MM
380 else
381 gimple_seq_set_first (gsi->seq, stmt);
382 if (next)
daa6e488 383 next->prev = stmt;
355a7673
MM
384 else
385 gimple_seq_set_last (gsi->seq, stmt);
386
387 gsi->ptr = stmt;
388}
389
390
726a989a
RB
391/* Move all statements in the sequence before I to a new sequence.
392 Return this new sequence. I is set to the head of the new list. */
393
355a7673
MM
394void
395gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq)
726a989a
RB
396{
397 gimple_seq_node cur, prev;
355a7673 398 gimple_seq old_seq;
726a989a
RB
399
400 cur = i->ptr;
401
402 /* How can we possibly split after the end? */
403 gcc_assert (cur);
daa6e488 404 prev = cur->prev;
726a989a 405
355a7673 406 old_seq = *i->seq;
daa6e488 407 if (!prev->next)
355a7673
MM
408 *i->seq = NULL;
409 i->seq = pnew_seq;
726a989a
RB
410
411 /* Set the limits on NEW_SEQ. */
355a7673
MM
412 gimple_seq_set_first (pnew_seq, cur);
413 gimple_seq_set_last (pnew_seq, gimple_seq_last (old_seq));
726a989a
RB
414
415 /* Cut OLD_SEQ before I. */
355a7673 416 gimple_seq_set_last (&old_seq, prev);
daa6e488
DM
417 if (prev->next)
418 prev->next = NULL;
726a989a
RB
419}
420
421
422/* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
423 is true, the exception handling information of the original
0ca5af51 424 statement is moved to the new statement. Assignments must only be
8cb65b37
MG
425 replaced with assignments to the same LHS. Returns whether EH edge
426 cleanup is required. */
726a989a 427
8cb65b37 428bool
355fe088 429gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info)
726a989a 430{
355fe088 431 gimple *orig_stmt = gsi_stmt (*gsi);
8cb65b37 432 bool require_eh_edge_purge = false;
726a989a
RB
433
434 if (stmt == orig_stmt)
8cb65b37 435 return false;
726a989a 436
5f33a4fc 437 gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
0ca5af51
AO
438 || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
439
726a989a
RB
440 gimple_set_location (stmt, gimple_location (orig_stmt));
441 gimple_set_bb (stmt, gsi_bb (*gsi));
442
443 /* Preserve EH region information from the original statement, if
444 requested by the caller. */
445 if (update_eh_info)
8cb65b37 446 require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
726a989a
RB
447
448 gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
8d2adc24
EB
449
450 /* Free all the data flow information for ORIG_STMT. */
451 gimple_set_bb (orig_stmt, NULL);
726a989a
RB
452 gimple_remove_stmt_histograms (cfun, orig_stmt);
453 delink_stmt_imm_use (orig_stmt);
8d2adc24 454
355a7673 455 gsi_set_stmt (gsi, stmt);
726a989a
RB
456 gimple_set_modified (stmt, true);
457 update_modified_stmt (stmt);
8cb65b37 458 return require_eh_edge_purge;
726a989a
RB
459}
460
461
355a7673
MM
462/* Replace the statement pointed-to by GSI with the sequence SEQ.
463 If UPDATE_EH_INFO is true, the exception handling information of
464 the original statement is moved to the last statement of the new
465 sequence. If the old statement is an assignment, then so must
466 be the last statement of the new sequence, and they must have the
467 same LHS. */
468
469void
470gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq,
471 bool update_eh_info)
472{
473 gimple_stmt_iterator seqi;
355fe088 474 gimple *last;
355a7673
MM
475 if (gimple_seq_empty_p (seq))
476 {
477 gsi_remove (gsi, true);
478 return;
479 }
480 seqi = gsi_last (seq);
481 last = gsi_stmt (seqi);
482 gsi_remove (&seqi, false);
483 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
484 gsi_replace (gsi, last, update_eh_info);
485}
486
487
726a989a
RB
488/* Insert statement STMT before the statement pointed-to by iterator I.
489 M specifies how to update iterator I after insertion (see enum
490 gsi_iterator_update).
491
492 This function does not scan for new operands. It is provided for
493 the use of the gimplifier, which manipulates statements for which
494 def/use information has not yet been constructed. Most callers
495 should use gsi_insert_before. */
496
497void
355fe088 498gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt,
726a989a
RB
499 enum gsi_iterator_update m)
500{
355a7673 501 gsi_insert_seq_nodes_before (i, stmt, stmt, m);
726a989a
RB
502}
503
504/* Insert statement STMT before the statement pointed-to by iterator I.
505 Update STMT's basic block and scan it for new operands. M
506 specifies how to update iterator I after insertion (see enum
507 gsi_iterator_update). */
508
509void
355fe088 510gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt,
726a989a
RB
511 enum gsi_iterator_update m)
512{
513 update_modified_stmt (stmt);
514 gsi_insert_before_without_update (i, stmt, m);
515}
516
517
518/* Insert statement STMT after the statement pointed-to by iterator I.
519 M specifies how to update iterator I after insertion (see enum
520 gsi_iterator_update).
521
522 This function does not scan for new operands. It is provided for
523 the use of the gimplifier, which manipulates statements for which
524 def/use information has not yet been constructed. Most callers
525 should use gsi_insert_after. */
526
527void
355fe088 528gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt,
726a989a
RB
529 enum gsi_iterator_update m)
530{
355a7673 531 gsi_insert_seq_nodes_after (i, stmt, stmt, m);
726a989a
RB
532}
533
534
535/* Insert statement STMT after the statement pointed-to by iterator I.
536 Update STMT's basic block and scan it for new operands. M
537 specifies how to update iterator I after insertion (see enum
538 gsi_iterator_update). */
539
540void
355fe088 541gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt,
726a989a
RB
542 enum gsi_iterator_update m)
543{
544 update_modified_stmt (stmt);
545 gsi_insert_after_without_update (i, stmt, m);
546}
547
548
549/* Remove the current stmt from the sequence. The iterator is updated
550 to point to the next statement.
551
552 REMOVE_PERMANENTLY is true when the statement is going to be removed
553 from the IL and not reinserted elsewhere. In that case we remove the
554 statement pointed to by iterator I from the EH tables, and free its
b5b3ec3e
RG
555 operand caches. Otherwise we do not modify this information. Returns
556 true whether EH edge cleanup is required. */
726a989a 557
b5b3ec3e 558bool
726a989a
RB
559gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
560{
561 gimple_seq_node cur, next, prev;
355fe088 562 gimple *stmt = gsi_stmt (*i);
b5b3ec3e 563 bool require_eh_edge_purge = false;
726a989a 564
f74c4b2c 565 /* ??? Do we want to do this for non-permanent operation? */
cd6549e8
AO
566 if (gimple_code (stmt) != GIMPLE_PHI)
567 insert_debug_temps_for_defs (i);
0ca5af51 568
726a989a 569 gimple_set_bb (stmt, NULL);
726a989a
RB
570
571 if (remove_permanently)
572 {
f74c4b2c
RB
573 /* Free all the data flow information for STMT. */
574 delink_stmt_imm_use (stmt);
575 gimple_set_modified (stmt, true);
576
96a95ac1
AO
577 if (gimple_debug_nonbind_marker_p (stmt))
578 /* We don't need this to be exact, but try to keep it at least
579 close. */
580 cfun->debug_marker_count--;
b5b3ec3e 581 require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
726a989a
RB
582 gimple_remove_stmt_histograms (cfun, stmt);
583 }
584
585 /* Update the iterator and re-wire the links in I->SEQ. */
586 cur = i->ptr;
daa6e488
DM
587 next = cur->next;
588 prev = cur->prev;
355a7673 589 /* See gsi_set_stmt for why we don't reset prev/next of STMT. */
726a989a
RB
590
591 if (next)
355a7673 592 /* Cur is not last. */
daa6e488
DM
593 next->prev = prev;
594 else if (prev->next)
355a7673 595 /* Cur is last but not first. */
726a989a
RB
596 gimple_seq_set_last (i->seq, prev);
597
daa6e488 598 if (prev->next)
355a7673 599 /* Cur is not first. */
daa6e488 600 prev->next = next;
355a7673
MM
601 else
602 /* Cur is first. */
603 *i->seq = next;
604
726a989a 605 i->ptr = next;
b5b3ec3e
RG
606
607 return require_eh_edge_purge;
726a989a
RB
608}
609
610
611/* Finds iterator for STMT. */
612
613gimple_stmt_iterator
355fe088 614gsi_for_stmt (gimple *stmt)
726a989a
RB
615{
616 gimple_stmt_iterator i;
617 basic_block bb = gimple_bb (stmt);
618
619 if (gimple_code (stmt) == GIMPLE_PHI)
620 i = gsi_start_phis (bb);
621 else
622 i = gsi_start_bb (bb);
623
355a7673
MM
624 i.ptr = stmt;
625 return i;
726a989a
RB
626}
627
41949de9
RS
628/* Get an iterator for STMT, which is known to belong to SEQ. This is
629 equivalent to starting at the beginning of SEQ and searching forward
630 until STMT is found. */
631
632gimple_stmt_iterator
633gsi_for_stmt (gimple *stmt, gimple_seq *seq)
634{
4aa61e08 635 gimple_stmt_iterator i = gsi_start (*seq);
41949de9
RS
636 i.ptr = stmt;
637 return i;
638}
639
538dd0b7
DM
640/* Finds iterator for PHI. */
641
642gphi_iterator
643gsi_for_phi (gphi *phi)
644{
645 gphi_iterator i;
646 basic_block bb = gimple_bb (phi);
647
648 i = gsi_start_phis (bb);
649 i.ptr = phi;
650
651 return i;
652}
726a989a
RB
653
654/* Move the statement at FROM so it comes right after the statement at TO. */
655
656void
657gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
658{
355fe088 659 gimple *stmt = gsi_stmt (*from);
726a989a
RB
660 gsi_remove (from, false);
661
662 /* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
663 move statements to an empty block. */
664 gsi_insert_after (to, stmt, GSI_NEW_STMT);
665}
666
667
668/* Move the statement at FROM so it comes right before the statement
669 at TO. */
670
671void
672gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
673{
355fe088 674 gimple *stmt = gsi_stmt (*from);
726a989a
RB
675 gsi_remove (from, false);
676
677 /* For consistency with gsi_move_after, it might be better to have
678 GSI_NEW_STMT here; however, that breaks several places that expect
679 that TO does not change. */
680 gsi_insert_before (to, stmt, GSI_SAME_STMT);
681}
682
683
684/* Move the statement at FROM to the end of basic block BB. */
685
686void
687gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
688{
689 gimple_stmt_iterator last = gsi_last_bb (bb);
77a74ed7 690 gcc_checking_assert (gsi_bb (last) == bb);
726a989a
RB
691
692 /* Have to check gsi_end_p because it could be an empty block. */
693 if (!gsi_end_p (last) && is_ctrl_stmt (gsi_stmt (last)))
694 gsi_move_before (from, &last);
695 else
696 gsi_move_after (from, &last);
697}
698
699
700/* Add STMT to the pending list of edge E. No actual insertion is
701 made until a call to gsi_commit_edge_inserts () is made. */
702
703void
355fe088 704gsi_insert_on_edge (edge e, gimple *stmt)
726a989a
RB
705{
706 gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
707}
708
709/* Add the sequence of statements SEQ to the pending list of edge E.
710 No actual insertion is made until a call to gsi_commit_edge_inserts
711 is made. */
712
713void
714gsi_insert_seq_on_edge (edge e, gimple_seq seq)
715{
716 gimple_seq_add_seq (&PENDING_STMT (e), seq);
717}
718
104cb50b
MJ
719/* Return a new iterator pointing to the first statement in sequence of
720 statements on edge E. Such statements need to be subsequently moved into a
721 basic block by calling gsi_commit_edge_inserts. */
722
723gimple_stmt_iterator
724gsi_start_edge (edge e)
725{
726 return gsi_start (PENDING_STMT (e));
727}
726a989a
RB
728
729/* Insert the statement pointed-to by GSI into edge E. Every attempt
730 is made to place the statement in an existing basic block, but
731 sometimes that isn't possible. When it isn't possible, the edge is
732 split and the statement is added to the new block.
733
734 In all cases, the returned *GSI points to the correct location. The
735 return value is true if insertion should be done after the location,
249eb506 736 or false if it should be done before the location. If a new basic block
726a989a
RB
737 has to be created, it is stored in *NEW_BB. */
738
739static bool
740gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi,
741 basic_block *new_bb)
742{
743 basic_block dest, src;
355fe088 744 gimple *tmp;
726a989a
RB
745
746 dest = e->dest;
747
748 /* If the destination has one predecessor which has no PHI nodes,
749 insert there. Except for the exit block.
750
751 The requirement for no PHI nodes could be relaxed. Basically we
752 would have to examine the PHIs to prove that none of them used
753 the value set by the statement we want to insert on E. That
754 hardly seems worth the effort. */
671f9f30 755 restart:
726a989a 756 if (single_pred_p (dest)
671f9f30 757 && gimple_seq_empty_p (phi_nodes (dest))
fefa31b5 758 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
726a989a
RB
759 {
760 *gsi = gsi_start_bb (dest);
761 if (gsi_end_p (*gsi))
762 return true;
763
67a8d719 764 /* Make sure we insert after any leading labels. */
726a989a 765 tmp = gsi_stmt (*gsi);
67a8d719 766 while (gimple_code (tmp) == GIMPLE_LABEL)
726a989a
RB
767 {
768 gsi_next (gsi);
769 if (gsi_end_p (*gsi))
770 break;
771 tmp = gsi_stmt (*gsi);
772 }
773
774 if (gsi_end_p (*gsi))
775 {
776 *gsi = gsi_last_bb (dest);
777 return true;
778 }
779 else
780 return false;
781 }
782
783 /* If the source has one successor, the edge is not abnormal and
784 the last statement does not end a basic block, insert there.
785 Except for the entry block. */
786 src = e->src;
787 if ((e->flags & EDGE_ABNORMAL) == 0
608c0f63
RB
788 && (single_succ_p (src)
789 /* Do not count a fake edge as successor as added to infinite
790 loops by connect_infinite_loops_to_exit. */
791 || (EDGE_COUNT (src->succs) == 2
792 && (EDGE_SUCC (src, 0)->flags & EDGE_FAKE
793 || EDGE_SUCC (src, 1)->flags & EDGE_FAKE)))
fefa31b5 794 && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
726a989a
RB
795 {
796 *gsi = gsi_last_bb (src);
797 if (gsi_end_p (*gsi))
798 return true;
799
800 tmp = gsi_stmt (*gsi);
65f4b875
AO
801 if (is_gimple_debug (tmp))
802 {
803 gimple_stmt_iterator si = *gsi;
804 gsi_prev_nondebug (&si);
805 if (!gsi_end_p (si))
806 tmp = gsi_stmt (si);
807 /* If we don't have a BB-ending nondebug stmt, we want to
808 insert after the trailing debug stmts. Otherwise, we may
809 insert before the BB-ending nondebug stmt, or split the
810 edge. */
811 if (!stmt_ends_bb_p (tmp))
812 return true;
813 *gsi = si;
814 }
815 else if (!stmt_ends_bb_p (tmp))
726a989a
RB
816 return true;
817
07c358c6
RH
818 switch (gimple_code (tmp))
819 {
820 case GIMPLE_RETURN:
821 case GIMPLE_RESX:
822 return false;
823 default:
824 break;
726a989a
RB
825 }
826 }
827
828 /* Otherwise, create a new basic block, and split this edge. */
829 dest = split_edge (e);
830 if (new_bb)
831 *new_bb = dest;
832 e = single_pred_edge (dest);
833 goto restart;
834}
835
836
837/* Similar to gsi_insert_on_edge+gsi_commit_edge_inserts. If a new
838 block has to be created, it is returned. */
839
840basic_block
355fe088 841gsi_insert_on_edge_immediate (edge e, gimple *stmt)
726a989a
RB
842{
843 gimple_stmt_iterator gsi;
844 basic_block new_bb = NULL;
8b84c596 845 bool ins_after;
726a989a
RB
846
847 gcc_assert (!PENDING_STMT (e));
848
8b84c596
RH
849 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
850
355a7673 851 update_call_edge_frequencies (stmt, gsi.bb);
8b84c596
RH
852
853 if (ins_after)
726a989a
RB
854 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
855 else
856 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
857
858 return new_bb;
859}
860
861/* Insert STMTS on edge E. If a new block has to be created, it
862 is returned. */
863
864basic_block
865gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts)
866{
867 gimple_stmt_iterator gsi;
868 basic_block new_bb = NULL;
8b84c596 869 bool ins_after;
726a989a
RB
870
871 gcc_assert (!PENDING_STMT (e));
872
8b84c596
RH
873 ins_after = gimple_find_edge_insert_loc (e, &gsi, &new_bb);
874 update_call_edge_frequencies (gimple_seq_first (stmts), gsi.bb);
875
876 if (ins_after)
726a989a
RB
877 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
878 else
879 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
880
881 return new_bb;
882}
883
884/* This routine will commit all pending edge insertions, creating any new
885 basic blocks which are necessary. */
886
887void
888gsi_commit_edge_inserts (void)
889{
890 basic_block bb;
891 edge e;
892 edge_iterator ei;
893
fefa31b5
DM
894 gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
895 NULL);
726a989a 896
11cd3bed 897 FOR_EACH_BB_FN (bb, cfun)
726a989a
RB
898 FOR_EACH_EDGE (e, ei, bb->succs)
899 gsi_commit_one_edge_insert (e, NULL);
900}
901
902
903/* Commit insertions pending at edge E. If a new block is created, set NEW_BB
904 to this block, otherwise set it to NULL. */
905
906void
907gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
908{
909 if (new_bb)
910 *new_bb = NULL;
911
912 if (PENDING_STMT (e))
913 {
914 gimple_stmt_iterator gsi;
915 gimple_seq seq = PENDING_STMT (e);
8b84c596 916 bool ins_after;
726a989a
RB
917
918 PENDING_STMT (e) = NULL;
919
8b84c596
RH
920 ins_after = gimple_find_edge_insert_loc (e, &gsi, new_bb);
921 update_call_edge_frequencies (gimple_seq_first (seq), gsi.bb);
922
923 if (ins_after)
726a989a
RB
924 gsi_insert_seq_after (&gsi, seq, GSI_NEW_STMT);
925 else
926 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
927 }
928}
929
930/* Returns iterator at the start of the list of phi nodes of BB. */
931
538dd0b7 932gphi_iterator
726a989a
RB
933gsi_start_phis (basic_block bb)
934{
355a7673 935 gimple_seq *pseq = phi_nodes_ptr (bb);
538dd0b7 936
4aa61e08 937 /* Adapted from gsi_start. */
538dd0b7
DM
938 gphi_iterator i;
939
940 i.ptr = gimple_seq_first (*pseq);
941 i.seq = pseq;
942 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
943
944 return i;
726a989a 945}