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