1 | /* Iterator routines for GIMPLE statements. |
2 | Copyright (C) 2007-2023 Free Software Foundation, Inc. |
3 | Contributed by Aldy Hernandez <aldy@quesejoda.com> |
4 | |
5 | This file is part of GCC. |
6 | |
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 |
10 | version. |
11 | |
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 |
15 | for more details. |
16 | |
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/>. */ |
20 | |
21 | #include "config.h" |
22 | #include "system.h" |
23 | #include "coretypes.h" |
24 | #include "backend.h" |
25 | #include "tree.h" |
26 | #include "gimple.h" |
27 | #include "cfghooks.h" |
28 | #include "ssa.h" |
29 | #include "cgraph.h" |
30 | #include "tree-eh.h" |
31 | #include "gimple-iterator.h" |
32 | #include "tree-cfg.h" |
33 | #include "tree-ssa.h" |
34 | #include "value-prof.h" |
35 | |
36 | |
37 | /* Mark the statement STMT as modified, and update it. */ |
38 | |
39 | static inline void |
40 | update_modified_stmt (gimple *stmt) |
41 | { |
42 | if (!ssa_operands_active (cfun)) |
43 | return; |
44 | update_stmt_if_modified (s: stmt); |
45 | } |
46 | |
47 | |
48 | /* Mark the statements in SEQ as modified, and update them. */ |
49 | |
50 | void |
51 | update_modified_stmts (gimple_seq seq) |
52 | { |
53 | gimple_stmt_iterator gsi; |
54 | |
55 | if (!ssa_operands_active (cfun)) |
56 | return; |
57 | for (gsi = gsi_start (seq); !gsi_end_p (i: gsi); gsi_next (i: &gsi)) |
58 | update_stmt_if_modified (s: gsi_stmt (i: 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 | |
65 | static void |
66 | update_bb_for_stmts (gimple_seq_node first, gimple_seq_node last, |
67 | basic_block bb) |
68 | { |
69 | gimple_seq_node n; |
70 | |
71 | for (n = first; n; n = n->next) |
72 | { |
73 | gimple_set_bb (n, bb); |
74 | if (n == last) |
75 | break; |
76 | } |
77 | } |
78 | |
79 | /* Set the frequencies for the cgraph_edges for each of the calls |
80 | starting at FIRST for their new position within BB. */ |
81 | |
82 | static void |
83 | update_call_edge_frequencies (gimple_seq_node first, basic_block bb) |
84 | { |
85 | struct cgraph_node *cfun_node = NULL; |
86 | gimple_seq_node n; |
87 | |
88 | for (n = first; n ; n = n->next) |
89 | if (is_gimple_call (gs: n)) |
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) |
96 | cfun_node = cgraph_node::get (decl: current_function_decl); |
97 | |
98 | e = cfun_node->get_edge (call_stmt: n); |
99 | if (e != NULL) |
100 | e->count = bb->count; |
101 | } |
102 | } |
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 | |
113 | static void |
114 | gsi_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 | |
122 | gcc_assert (!cur || cur->prev); |
123 | |
124 | if ((bb = gsi_bb (i: *i)) != NULL) |
125 | update_bb_for_stmts (first, last, bb); |
126 | |
127 | /* Link SEQ before CUR in the sequence. */ |
128 | if (cur) |
129 | { |
130 | first->prev = cur->prev; |
131 | if (first->prev->next) |
132 | first->prev->next = first; |
133 | else |
134 | gimple_seq_set_first (ps: i->seq, first); |
135 | last->next = cur; |
136 | cur->prev = last; |
137 | } |
138 | else |
139 | { |
140 | gimple_seq_node itlast = gimple_seq_last (s: *i->seq); |
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). */ |
147 | last->next = NULL; |
148 | if (itlast) |
149 | { |
150 | first->prev = itlast; |
151 | itlast->next = first; |
152 | } |
153 | else |
154 | gimple_seq_set_first (ps: i->seq, first); |
155 | gimple_seq_set_last (ps: 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; |
165 | case GSI_LAST_NEW_STMT: |
166 | i->ptr = last; |
167 | break; |
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 | |
185 | void |
186 | gsi_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. */ |
195 | gcc_assert (seq != *i->seq); |
196 | |
197 | first = gimple_seq_first (s: seq); |
198 | last = gimple_seq_last (s: seq); |
199 | |
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 | |
216 | void |
217 | gsi_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 | |
234 | static void |
235 | gsi_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 | |
243 | gcc_assert (!cur || cur->prev); |
244 | |
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: *i)) != NULL) |
248 | update_bb_for_stmts (first, last, bb); |
249 | |
250 | /* Link SEQ after CUR. */ |
251 | if (cur) |
252 | { |
253 | last->next = cur->next; |
254 | if (last->next) |
255 | { |
256 | last->next->prev = last; |
257 | } |
258 | else |
259 | gimple_seq_set_last (ps: i->seq, last); |
260 | first->prev = cur; |
261 | cur->next = first; |
262 | } |
263 | else |
264 | { |
265 | gcc_assert (!gimple_seq_last (*i->seq)); |
266 | last->next = NULL; |
267 | gimple_seq_set_first (ps: i->seq, first); |
268 | gimple_seq_set_last (ps: 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; |
277 | case GSI_LAST_NEW_STMT: |
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 | |
298 | void |
299 | gsi_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. */ |
308 | gcc_assert (seq != *i->seq); |
309 | |
310 | first = gimple_seq_first (s: seq); |
311 | last = gimple_seq_last (s: seq); |
312 | |
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, m: 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 | |
328 | void |
329 | gsi_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 | |
340 | gimple_seq |
341 | gsi_split_seq_after (gimple_stmt_iterator i) |
342 | { |
343 | gimple_seq_node cur, next; |
344 | gimple_seq *pold_seq, new_seq; |
345 | |
346 | cur = i.ptr; |
347 | |
348 | /* How can we possibly split after the end, or before the beginning? */ |
349 | gcc_assert (cur && cur->next); |
350 | next = cur->next; |
351 | |
352 | pold_seq = i.seq; |
353 | |
354 | gimple_seq_set_first (ps: &new_seq, first: next); |
355 | gimple_seq_set_last (ps: &new_seq, last: gimple_seq_last (s: *pold_seq)); |
356 | gimple_seq_set_last (ps: pold_seq, last: cur); |
357 | cur->next = NULL; |
358 | |
359 | return new_seq; |
360 | } |
361 | |
362 | |
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 | |
367 | void |
368 | gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt) |
369 | { |
370 | gimple *orig_stmt = gsi_stmt (i: *gsi); |
371 | gimple *prev, *next; |
372 | |
373 | stmt->next = next = orig_stmt->next; |
374 | stmt->prev = prev = orig_stmt->prev; |
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. */ |
378 | if (prev->next) |
379 | prev->next = stmt; |
380 | else |
381 | gimple_seq_set_first (ps: gsi->seq, first: stmt); |
382 | if (next) |
383 | next->prev = stmt; |
384 | else |
385 | gimple_seq_set_last (ps: gsi->seq, last: stmt); |
386 | |
387 | gsi->ptr = stmt; |
388 | } |
389 | |
390 | |
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 | |
394 | void |
395 | gsi_split_seq_before (gimple_stmt_iterator *i, gimple_seq *pnew_seq) |
396 | { |
397 | gimple_seq_node cur, prev; |
398 | gimple_seq old_seq; |
399 | |
400 | cur = i->ptr; |
401 | |
402 | /* How can we possibly split after the end? */ |
403 | gcc_assert (cur); |
404 | prev = cur->prev; |
405 | |
406 | old_seq = *i->seq; |
407 | if (!prev->next) |
408 | *i->seq = NULL; |
409 | i->seq = pnew_seq; |
410 | |
411 | /* Set the limits on NEW_SEQ. */ |
412 | gimple_seq_set_first (ps: pnew_seq, first: cur); |
413 | gimple_seq_set_last (ps: pnew_seq, last: gimple_seq_last (s: old_seq)); |
414 | |
415 | /* Cut OLD_SEQ before I. */ |
416 | gimple_seq_set_last (ps: &old_seq, last: prev); |
417 | if (prev->next) |
418 | prev->next = NULL; |
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 |
424 | statement is moved to the new statement. Assignments must only be |
425 | replaced with assignments to the same LHS. Returns whether EH edge |
426 | cleanup is required. */ |
427 | |
428 | bool |
429 | gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info) |
430 | { |
431 | gimple *orig_stmt = gsi_stmt (i: *gsi); |
432 | bool require_eh_edge_purge = false; |
433 | |
434 | if (stmt == orig_stmt) |
435 | return false; |
436 | |
437 | gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt) |
438 | || gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt)); |
439 | |
440 | gimple_set_location (g: stmt, location: gimple_location (g: orig_stmt)); |
441 | gimple_set_bb (stmt, gsi_bb (i: *gsi)); |
442 | |
443 | /* Preserve EH region information from the original statement, if |
444 | requested by the caller. */ |
445 | if (update_eh_info) |
446 | require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt); |
447 | |
448 | gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt); |
449 | |
450 | /* Free all the data flow information for ORIG_STMT. */ |
451 | gimple_set_bb (orig_stmt, NULL); |
452 | gimple_remove_stmt_histograms (cfun, orig_stmt); |
453 | delink_stmt_imm_use (stmt: orig_stmt); |
454 | |
455 | gsi_set_stmt (gsi, stmt); |
456 | gimple_set_modified (s: stmt, modifiedp: true); |
457 | update_modified_stmt (stmt); |
458 | return require_eh_edge_purge; |
459 | } |
460 | |
461 | |
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 | |
469 | void |
470 | gsi_replace_with_seq (gimple_stmt_iterator *gsi, gimple_seq seq, |
471 | bool update_eh_info) |
472 | { |
473 | gimple_stmt_iterator seqi; |
474 | gimple *last; |
475 | if (gimple_seq_empty_p (s: seq)) |
476 | { |
477 | gsi_remove (gsi, true); |
478 | return; |
479 | } |
480 | seqi = gsi_last (seq); |
481 | last = gsi_stmt (i: seqi); |
482 | gsi_remove (&seqi, false); |
483 | gsi_insert_seq_before (i: gsi, seq, mode: GSI_SAME_STMT); |
484 | gsi_replace (gsi, stmt: last, update_eh_info); |
485 | } |
486 | |
487 | |
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 | |
497 | void |
498 | gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt, |
499 | enum gsi_iterator_update m) |
500 | { |
501 | gsi_insert_seq_nodes_before (i, first: stmt, last: stmt, mode: m); |
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 | |
509 | void |
510 | gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt, |
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 | |
527 | void |
528 | gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt, |
529 | enum gsi_iterator_update m) |
530 | { |
531 | gsi_insert_seq_nodes_after (i, first: stmt, last: stmt, m); |
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 | |
540 | void |
541 | gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt, |
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 |
555 | operand caches. Otherwise we do not modify this information. Returns |
556 | true whether EH edge cleanup is required. */ |
557 | |
558 | bool |
559 | gsi_remove (gimple_stmt_iterator *i, bool remove_permanently) |
560 | { |
561 | gimple_seq_node cur, next, prev; |
562 | gimple *stmt = gsi_stmt (i: *i); |
563 | bool require_eh_edge_purge = false; |
564 | |
565 | /* ??? Do we want to do this for non-permanent operation? */ |
566 | if (gimple_code (g: stmt) != GIMPLE_PHI) |
567 | insert_debug_temps_for_defs (i); |
568 | |
569 | gimple_set_bb (stmt, NULL); |
570 | |
571 | if (remove_permanently) |
572 | { |
573 | /* Free all the data flow information for STMT. */ |
574 | delink_stmt_imm_use (stmt); |
575 | gimple_set_modified (s: stmt, modifiedp: true); |
576 | |
577 | if (gimple_debug_nonbind_marker_p (s: stmt)) |
578 | /* We don't need this to be exact, but try to keep it at least |
579 | close. */ |
580 | cfun->debug_marker_count--; |
581 | require_eh_edge_purge = remove_stmt_from_eh_lp (stmt); |
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; |
587 | next = cur->next; |
588 | prev = cur->prev; |
589 | /* See gsi_set_stmt for why we don't reset prev/next of STMT. */ |
590 | |
591 | if (next) |
592 | /* Cur is not last. */ |
593 | next->prev = prev; |
594 | else if (prev->next) |
595 | /* Cur is last but not first. */ |
596 | gimple_seq_set_last (ps: i->seq, last: prev); |
597 | |
598 | if (prev->next) |
599 | /* Cur is not first. */ |
600 | prev->next = next; |
601 | else |
602 | /* Cur is first. */ |
603 | *i->seq = next; |
604 | |
605 | i->ptr = next; |
606 | |
607 | return require_eh_edge_purge; |
608 | } |
609 | |
610 | |
611 | /* Finds iterator for STMT. */ |
612 | |
613 | gimple_stmt_iterator |
614 | gsi_for_stmt (gimple *stmt) |
615 | { |
616 | gimple_stmt_iterator i; |
617 | basic_block bb = gimple_bb (g: stmt); |
618 | |
619 | if (gimple_code (g: stmt) == GIMPLE_PHI) |
620 | i = gsi_start_phis (bb); |
621 | else |
622 | i = gsi_start_bb (bb); |
623 | |
624 | i.ptr = stmt; |
625 | return i; |
626 | } |
627 | |
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 | |
632 | gimple_stmt_iterator |
633 | gsi_for_stmt (gimple *stmt, gimple_seq *seq) |
634 | { |
635 | gimple_stmt_iterator i = gsi_start (seq&: *seq); |
636 | i.ptr = stmt; |
637 | return i; |
638 | } |
639 | |
640 | /* Finds iterator for PHI. */ |
641 | |
642 | gphi_iterator |
643 | gsi_for_phi (gphi *phi) |
644 | { |
645 | gphi_iterator i; |
646 | basic_block bb = gimple_bb (g: phi); |
647 | |
648 | i = gsi_start_phis (bb); |
649 | i.ptr = phi; |
650 | |
651 | return i; |
652 | } |
653 | |
654 | /* Move the statement at FROM so it comes right after the statement at TO. */ |
655 | |
656 | void |
657 | gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to) |
658 | { |
659 | gimple *stmt = gsi_stmt (i: *from); |
660 | gsi_remove (i: from, remove_permanently: 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 (i: to, stmt, m: GSI_NEW_STMT); |
665 | } |
666 | |
667 | |
668 | /* Move the statement at FROM so it comes right before the statement |
669 | at TO. */ |
670 | |
671 | void |
672 | gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to) |
673 | { |
674 | gimple *stmt = gsi_stmt (i: *from); |
675 | gsi_remove (i: from, remove_permanently: 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 (i: to, stmt, m: GSI_SAME_STMT); |
681 | } |
682 | |
683 | |
684 | /* Move the statement at FROM to the end of basic block BB. */ |
685 | |
686 | void |
687 | gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb) |
688 | { |
689 | gimple_stmt_iterator last = gsi_last_bb (bb); |
690 | gcc_checking_assert (gsi_bb (last) == bb); |
691 | |
692 | /* Have to check gsi_end_p because it could be an empty block. */ |
693 | if (!gsi_end_p (i: last) && is_ctrl_stmt (gsi_stmt (i: last))) |
694 | gsi_move_before (from, to: &last); |
695 | else |
696 | gsi_move_after (from, to: &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 | |
703 | void |
704 | gsi_insert_on_edge (edge e, gimple *stmt) |
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 | |
713 | void |
714 | gsi_insert_seq_on_edge (edge e, gimple_seq seq) |
715 | { |
716 | gimple_seq_add_seq (&PENDING_STMT (e), seq); |
717 | } |
718 | |
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 | |
723 | gimple_stmt_iterator |
724 | gsi_start_edge (edge e) |
725 | { |
726 | return gsi_start (PENDING_STMT (e)); |
727 | } |
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, |
736 | or false if it should be done before the location. If a new basic block |
737 | has to be created, it is stored in *NEW_BB. */ |
738 | |
739 | static bool |
740 | gimple_find_edge_insert_loc (edge e, gimple_stmt_iterator *gsi, |
741 | basic_block *new_bb) |
742 | { |
743 | basic_block dest, src; |
744 | gimple *tmp; |
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. */ |
755 | restart: |
756 | if (single_pred_p (bb: dest) |
757 | && gimple_seq_empty_p (s: phi_nodes (bb: dest)) |
758 | && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
759 | { |
760 | *gsi = gsi_start_bb (bb: dest); |
761 | if (gsi_end_p (i: *gsi)) |
762 | return true; |
763 | |
764 | /* Make sure we insert after any leading labels. */ |
765 | tmp = gsi_stmt (i: *gsi); |
766 | while (gimple_code (g: tmp) == GIMPLE_LABEL) |
767 | { |
768 | gsi_next (i: gsi); |
769 | if (gsi_end_p (i: *gsi)) |
770 | break; |
771 | tmp = gsi_stmt (i: *gsi); |
772 | } |
773 | |
774 | if (gsi_end_p (i: *gsi)) |
775 | { |
776 | *gsi = gsi_last_bb (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 |
788 | && (single_succ_p (bb: 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))) |
794 | && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
795 | { |
796 | *gsi = gsi_last_bb (bb: src); |
797 | if (gsi_end_p (i: *gsi)) |
798 | return true; |
799 | |
800 | tmp = gsi_stmt (i: *gsi); |
801 | if (is_gimple_debug (gs: tmp)) |
802 | { |
803 | gimple_stmt_iterator si = *gsi; |
804 | gsi_prev_nondebug (i: &si); |
805 | if (!gsi_end_p (i: si)) |
806 | tmp = gsi_stmt (i: 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)) |
816 | return true; |
817 | |
818 | switch (gimple_code (g: tmp)) |
819 | { |
820 | case GIMPLE_RETURN: |
821 | case GIMPLE_RESX: |
822 | return false; |
823 | default: |
824 | break; |
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 (bb: 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 | |
840 | basic_block |
841 | gsi_insert_on_edge_immediate (edge e, gimple *stmt) |
842 | { |
843 | gimple_stmt_iterator gsi; |
844 | basic_block new_bb = NULL; |
845 | bool ins_after; |
846 | |
847 | gcc_assert (!PENDING_STMT (e)); |
848 | |
849 | ins_after = gimple_find_edge_insert_loc (e, gsi: &gsi, new_bb: &new_bb); |
850 | |
851 | update_call_edge_frequencies (first: stmt, bb: gsi.bb); |
852 | |
853 | if (ins_after) |
854 | gsi_insert_after (i: &gsi, stmt, m: GSI_NEW_STMT); |
855 | else |
856 | gsi_insert_before (i: &gsi, stmt, m: 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 | |
864 | basic_block |
865 | gsi_insert_seq_on_edge_immediate (edge e, gimple_seq stmts) |
866 | { |
867 | gimple_stmt_iterator gsi; |
868 | basic_block new_bb = NULL; |
869 | bool ins_after; |
870 | |
871 | gcc_assert (!PENDING_STMT (e)); |
872 | |
873 | ins_after = gimple_find_edge_insert_loc (e, gsi: &gsi, new_bb: &new_bb); |
874 | update_call_edge_frequencies (first: gimple_seq_first (s: stmts), bb: gsi.bb); |
875 | |
876 | if (ins_after) |
877 | gsi_insert_seq_after (i: &gsi, seq: stmts, mode: GSI_NEW_STMT); |
878 | else |
879 | gsi_insert_seq_before (i: &gsi, seq: stmts, mode: 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 | |
887 | void |
888 | gsi_commit_edge_inserts (void) |
889 | { |
890 | basic_block bb; |
891 | edge e; |
892 | edge_iterator ei; |
893 | |
894 | gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), |
895 | NULL); |
896 | |
897 | FOR_EACH_BB_FN (bb, cfun) |
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 | |
906 | void |
907 | gsi_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); |
916 | bool ins_after; |
917 | |
918 | PENDING_STMT (e) = NULL; |
919 | |
920 | ins_after = gimple_find_edge_insert_loc (e, gsi: &gsi, new_bb); |
921 | update_call_edge_frequencies (first: gimple_seq_first (s: seq), bb: gsi.bb); |
922 | |
923 | if (ins_after) |
924 | gsi_insert_seq_after (i: &gsi, seq, mode: GSI_NEW_STMT); |
925 | else |
926 | gsi_insert_seq_before (i: &gsi, seq, mode: GSI_NEW_STMT); |
927 | } |
928 | } |
929 | |
930 | /* Returns iterator at the start of the list of phi nodes of BB. */ |
931 | |
932 | gphi_iterator |
933 | gsi_start_phis (basic_block bb) |
934 | { |
935 | gimple_seq *pseq = phi_nodes_ptr (bb); |
936 | |
937 | /* Adapted from gsi_start. */ |
938 | gphi_iterator i; |
939 | |
940 | i.ptr = gimple_seq_first (s: *pseq); |
941 | i.seq = pseq; |
942 | i.bb = i.ptr ? gimple_bb (g: i.ptr) : NULL; |
943 | |
944 | return i; |
945 | } |
946 | |