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