]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Control flow functions for trees. |
43e05e45 | 2 | Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Diego Novillo <dnovillo@redhat.com> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "tree.h" | |
27 | #include "rtl.h" | |
28 | #include "tm_p.h" | |
29 | #include "hard-reg-set.h" | |
30 | #include "basic-block.h" | |
31 | #include "output.h" | |
32 | #include "errors.h" | |
33 | #include "flags.h" | |
34 | #include "function.h" | |
35 | #include "expr.h" | |
36 | #include "ggc.h" | |
37 | #include "langhooks.h" | |
38 | #include "diagnostic.h" | |
39 | #include "tree-flow.h" | |
40 | #include "timevar.h" | |
41 | #include "tree-dump.h" | |
42 | #include "tree-pass.h" | |
43 | #include "toplev.h" | |
44 | #include "except.h" | |
45 | #include "cfgloop.h" | |
42759f1e | 46 | #include "cfglayout.h" |
92b6dff3 | 47 | #include "hashtab.h" |
6de9cd9a DN |
48 | |
49 | /* This file contains functions for building the Control Flow Graph (CFG) | |
50 | for a function tree. */ | |
51 | ||
52 | /* Local declarations. */ | |
53 | ||
54 | /* Initial capacity for the basic block array. */ | |
55 | static const int initial_cfg_capacity = 20; | |
56 | ||
57 | /* Mapping of labels to their associated blocks. This can greatly speed up | |
58 | building of the CFG in code with lots of gotos. */ | |
59 | static GTY(()) varray_type label_to_block_map; | |
60 | ||
d6be0d7f JL |
61 | /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs |
62 | which use a particular edge. The CASE_LABEL_EXPRs are chained together | |
63 | via their TREE_CHAIN field, which we clear after we're done with the | |
64 | hash table to prevent problems with duplication of SWITCH_EXPRs. | |
92b6dff3 | 65 | |
d6be0d7f JL |
66 | Access to this list of CASE_LABEL_EXPRs allows us to efficiently |
67 | update the case vector in response to edge redirections. | |
92b6dff3 | 68 | |
d6be0d7f JL |
69 | Right now this table is set up and torn down at key points in the |
70 | compilation process. It would be nice if we could make the table | |
71 | more persistent. The key is getting notification of changes to | |
72 | the CFG (particularly edge removal, creation and redirection). */ | |
73 | ||
74 | struct edge_to_cases_elt | |
92b6dff3 JL |
75 | { |
76 | /* The edge itself. Necessary for hashing and equality tests. */ | |
77 | edge e; | |
78 | ||
d6be0d7f JL |
79 | /* The case labels associated with this edge. We link these up via |
80 | their TREE_CHAIN field, then we wipe out the TREE_CHAIN fields | |
81 | when we destroy the hash table. This prevents problems when copying | |
82 | SWITCH_EXPRs. */ | |
83 | tree case_labels; | |
92b6dff3 JL |
84 | }; |
85 | ||
d6be0d7f | 86 | static htab_t edge_to_cases; |
92b6dff3 | 87 | |
6de9cd9a DN |
88 | /* CFG statistics. */ |
89 | struct cfg_stats_d | |
90 | { | |
91 | long num_merged_labels; | |
92 | }; | |
93 | ||
94 | static struct cfg_stats_d cfg_stats; | |
95 | ||
96 | /* Nonzero if we found a computed goto while building basic blocks. */ | |
97 | static bool found_computed_goto; | |
98 | ||
99 | /* Basic blocks and flowgraphs. */ | |
100 | static basic_block create_bb (void *, void *, basic_block); | |
101 | static void create_block_annotation (basic_block); | |
102 | static void free_blocks_annotations (void); | |
103 | static void clear_blocks_annotations (void); | |
104 | static void make_blocks (tree); | |
105 | static void factor_computed_gotos (void); | |
6de9cd9a DN |
106 | |
107 | /* Edges. */ | |
108 | static void make_edges (void); | |
109 | static void make_ctrl_stmt_edges (basic_block); | |
110 | static void make_exit_edges (basic_block); | |
111 | static void make_cond_expr_edges (basic_block); | |
112 | static void make_switch_expr_edges (basic_block); | |
113 | static void make_goto_expr_edges (basic_block); | |
114 | static edge tree_redirect_edge_and_branch (edge, basic_block); | |
115 | static edge tree_try_redirect_by_replacing_jump (edge, basic_block); | |
116 | static void split_critical_edges (void); | |
d7f3fc19 | 117 | static bool remove_fallthru_edge (VEC(edge) *); |
6de9cd9a DN |
118 | |
119 | /* Various helpers. */ | |
120 | static inline bool stmt_starts_bb_p (tree, tree); | |
121 | static int tree_verify_flow_info (void); | |
122 | static void tree_make_forwarder_block (edge); | |
23ab2e4e | 123 | static bool tree_forwarder_block_p (basic_block, bool); |
6de9cd9a DN |
124 | static void tree_cfg2vcg (FILE *); |
125 | ||
126 | /* Flowgraph optimization and cleanup. */ | |
127 | static void tree_merge_blocks (basic_block, basic_block); | |
128 | static bool tree_can_merge_blocks_p (basic_block, basic_block); | |
129 | static void remove_bb (basic_block); | |
6de9cd9a DN |
130 | static bool cleanup_control_flow (void); |
131 | static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator); | |
132 | static edge find_taken_edge_cond_expr (basic_block, tree); | |
133 | static edge find_taken_edge_switch_expr (basic_block, tree); | |
134 | static tree find_case_label_for_value (tree, tree); | |
135 | static bool phi_alternatives_equal (basic_block, edge, edge); | |
63bb59a3 | 136 | static bool cleanup_forwarder_blocks (void); |
6de9cd9a DN |
137 | |
138 | ||
139 | /*--------------------------------------------------------------------------- | |
140 | Create basic blocks | |
141 | ---------------------------------------------------------------------------*/ | |
142 | ||
143 | /* Entry point to the CFG builder for trees. TP points to the list of | |
144 | statements to be added to the flowgraph. */ | |
145 | ||
146 | static void | |
147 | build_tree_cfg (tree *tp) | |
148 | { | |
149 | /* Register specific tree functions. */ | |
150 | tree_register_cfg_hooks (); | |
151 | ||
152 | /* Initialize rbi_pool. */ | |
153 | alloc_rbi_pool (); | |
154 | ||
155 | /* Initialize the basic block array. */ | |
156 | init_flow (); | |
878f99d2 | 157 | profile_status = PROFILE_ABSENT; |
6de9cd9a DN |
158 | n_basic_blocks = 0; |
159 | last_basic_block = 0; | |
160 | VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info"); | |
161 | memset ((void *) &cfg_stats, 0, sizeof (cfg_stats)); | |
162 | ||
163 | /* Build a mapping of labels to their associated blocks. */ | |
164 | VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity, | |
165 | "label to block map"); | |
166 | ||
167 | ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR; | |
168 | EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR; | |
169 | ||
170 | found_computed_goto = 0; | |
171 | make_blocks (*tp); | |
172 | ||
173 | /* Computed gotos are hell to deal with, especially if there are | |
174 | lots of them with a large number of destinations. So we factor | |
175 | them to a common computed goto location before we build the | |
176 | edge list. After we convert back to normal form, we will un-factor | |
177 | the computed gotos since factoring introduces an unwanted jump. */ | |
178 | if (found_computed_goto) | |
179 | factor_computed_gotos (); | |
180 | ||
f0b698c1 | 181 | /* Make sure there is always at least one block, even if it's empty. */ |
6de9cd9a DN |
182 | if (n_basic_blocks == 0) |
183 | create_empty_bb (ENTRY_BLOCK_PTR); | |
184 | ||
185 | create_block_annotation (ENTRY_BLOCK_PTR); | |
186 | create_block_annotation (EXIT_BLOCK_PTR); | |
187 | ||
188 | /* Adjust the size of the array. */ | |
189 | VARRAY_GROW (basic_block_info, n_basic_blocks); | |
190 | ||
f667741c SB |
191 | /* To speed up statement iterator walks, we first purge dead labels. */ |
192 | cleanup_dead_labels (); | |
193 | ||
194 | /* Group case nodes to reduce the number of edges. | |
195 | We do this after cleaning up dead labels because otherwise we miss | |
196 | a lot of obvious case merging opportunities. */ | |
197 | group_case_labels (); | |
198 | ||
6de9cd9a DN |
199 | /* Create the edges of the flowgraph. */ |
200 | make_edges (); | |
201 | ||
202 | /* Debugging dumps. */ | |
203 | ||
204 | /* Write the flowgraph to a VCG file. */ | |
205 | { | |
206 | int local_dump_flags; | |
207 | FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags); | |
208 | if (dump_file) | |
209 | { | |
210 | tree_cfg2vcg (dump_file); | |
211 | dump_end (TDI_vcg, dump_file); | |
212 | } | |
213 | } | |
214 | ||
215 | /* Dump a textual representation of the flowgraph. */ | |
216 | if (dump_file) | |
217 | dump_tree_cfg (dump_file, dump_flags); | |
218 | } | |
219 | ||
220 | static void | |
221 | execute_build_cfg (void) | |
222 | { | |
223 | build_tree_cfg (&DECL_SAVED_TREE (current_function_decl)); | |
224 | } | |
225 | ||
226 | struct tree_opt_pass pass_build_cfg = | |
227 | { | |
228 | "cfg", /* name */ | |
229 | NULL, /* gate */ | |
230 | execute_build_cfg, /* execute */ | |
231 | NULL, /* sub */ | |
232 | NULL, /* next */ | |
233 | 0, /* static_pass_number */ | |
234 | TV_TREE_CFG, /* tv_id */ | |
235 | PROP_gimple_leh, /* properties_required */ | |
236 | PROP_cfg, /* properties_provided */ | |
237 | 0, /* properties_destroyed */ | |
238 | 0, /* todo_flags_start */ | |
9f8628ba PB |
239 | TODO_verify_stmts, /* todo_flags_finish */ |
240 | 0 /* letter */ | |
6de9cd9a DN |
241 | }; |
242 | ||
243 | /* Search the CFG for any computed gotos. If found, factor them to a | |
244 | common computed goto site. Also record the location of that site so | |
245 | that we can un-factor the gotos after we have converted back to | |
246 | normal form. */ | |
247 | ||
248 | static void | |
249 | factor_computed_gotos (void) | |
250 | { | |
251 | basic_block bb; | |
252 | tree factored_label_decl = NULL; | |
253 | tree var = NULL; | |
254 | tree factored_computed_goto_label = NULL; | |
255 | tree factored_computed_goto = NULL; | |
256 | ||
257 | /* We know there are one or more computed gotos in this function. | |
258 | Examine the last statement in each basic block to see if the block | |
259 | ends with a computed goto. */ | |
260 | ||
261 | FOR_EACH_BB (bb) | |
262 | { | |
263 | block_stmt_iterator bsi = bsi_last (bb); | |
264 | tree last; | |
265 | ||
266 | if (bsi_end_p (bsi)) | |
267 | continue; | |
268 | last = bsi_stmt (bsi); | |
269 | ||
270 | /* Ignore the computed goto we create when we factor the original | |
271 | computed gotos. */ | |
272 | if (last == factored_computed_goto) | |
273 | continue; | |
274 | ||
275 | /* If the last statement is a computed goto, factor it. */ | |
276 | if (computed_goto_p (last)) | |
277 | { | |
278 | tree assignment; | |
279 | ||
280 | /* The first time we find a computed goto we need to create | |
281 | the factored goto block and the variable each original | |
282 | computed goto will use for their goto destination. */ | |
283 | if (! factored_computed_goto) | |
284 | { | |
285 | basic_block new_bb = create_empty_bb (bb); | |
286 | block_stmt_iterator new_bsi = bsi_start (new_bb); | |
287 | ||
288 | /* Create the destination of the factored goto. Each original | |
289 | computed goto will put its desired destination into this | |
290 | variable and jump to the label we create immediately | |
291 | below. */ | |
292 | var = create_tmp_var (ptr_type_node, "gotovar"); | |
293 | ||
294 | /* Build a label for the new block which will contain the | |
295 | factored computed goto. */ | |
296 | factored_label_decl = create_artificial_label (); | |
297 | factored_computed_goto_label | |
298 | = build1 (LABEL_EXPR, void_type_node, factored_label_decl); | |
299 | bsi_insert_after (&new_bsi, factored_computed_goto_label, | |
300 | BSI_NEW_STMT); | |
301 | ||
302 | /* Build our new computed goto. */ | |
303 | factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var); | |
304 | bsi_insert_after (&new_bsi, factored_computed_goto, | |
305 | BSI_NEW_STMT); | |
306 | } | |
307 | ||
308 | /* Copy the original computed goto's destination into VAR. */ | |
309 | assignment = build (MODIFY_EXPR, ptr_type_node, | |
310 | var, GOTO_DESTINATION (last)); | |
311 | bsi_insert_before (&bsi, assignment, BSI_SAME_STMT); | |
312 | ||
313 | /* And re-vector the computed goto to the new destination. */ | |
314 | GOTO_DESTINATION (last) = factored_label_decl; | |
315 | } | |
316 | } | |
317 | } | |
318 | ||
319 | ||
320 | /* Create annotations for a single basic block. */ | |
321 | ||
322 | static void | |
323 | create_block_annotation (basic_block bb) | |
324 | { | |
325 | /* Verify that the tree_annotations field is clear. */ | |
1e128c5f | 326 | gcc_assert (!bb->tree_annotations); |
6de9cd9a DN |
327 | bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d)); |
328 | } | |
329 | ||
330 | ||
331 | /* Free the annotations for all the basic blocks. */ | |
332 | ||
333 | static void free_blocks_annotations (void) | |
334 | { | |
335 | clear_blocks_annotations (); | |
336 | } | |
337 | ||
338 | ||
339 | /* Clear the annotations for all the basic blocks. */ | |
340 | ||
341 | static void | |
342 | clear_blocks_annotations (void) | |
343 | { | |
344 | basic_block bb; | |
345 | ||
346 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) | |
347 | bb->tree_annotations = NULL; | |
348 | } | |
349 | ||
350 | ||
351 | /* Build a flowgraph for the statement_list STMT_LIST. */ | |
352 | ||
353 | static void | |
354 | make_blocks (tree stmt_list) | |
355 | { | |
356 | tree_stmt_iterator i = tsi_start (stmt_list); | |
357 | tree stmt = NULL; | |
358 | bool start_new_block = true; | |
359 | bool first_stmt_of_list = true; | |
360 | basic_block bb = ENTRY_BLOCK_PTR; | |
361 | ||
362 | while (!tsi_end_p (i)) | |
363 | { | |
364 | tree prev_stmt; | |
365 | ||
366 | prev_stmt = stmt; | |
367 | stmt = tsi_stmt (i); | |
368 | ||
369 | /* If the statement starts a new basic block or if we have determined | |
370 | in a previous pass that we need to create a new block for STMT, do | |
371 | so now. */ | |
372 | if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt)) | |
373 | { | |
374 | if (!first_stmt_of_list) | |
375 | stmt_list = tsi_split_statement_list_before (&i); | |
376 | bb = create_basic_block (stmt_list, NULL, bb); | |
377 | start_new_block = false; | |
378 | } | |
379 | ||
380 | /* Now add STMT to BB and create the subgraphs for special statement | |
381 | codes. */ | |
382 | set_bb_for_stmt (stmt, bb); | |
383 | ||
384 | if (computed_goto_p (stmt)) | |
385 | found_computed_goto = true; | |
386 | ||
387 | /* If STMT is a basic block terminator, set START_NEW_BLOCK for the | |
388 | next iteration. */ | |
389 | if (stmt_ends_bb_p (stmt)) | |
390 | start_new_block = true; | |
391 | ||
392 | tsi_next (&i); | |
393 | first_stmt_of_list = false; | |
394 | } | |
395 | } | |
396 | ||
397 | ||
398 | /* Create and return a new empty basic block after bb AFTER. */ | |
399 | ||
400 | static basic_block | |
401 | create_bb (void *h, void *e, basic_block after) | |
402 | { | |
403 | basic_block bb; | |
404 | ||
1e128c5f | 405 | gcc_assert (!e); |
6de9cd9a | 406 | |
27fd69fa KH |
407 | /* Create and initialize a new basic block. Since alloc_block uses |
408 | ggc_alloc_cleared to allocate a basic block, we do not have to | |
409 | clear the newly allocated basic block here. */ | |
6de9cd9a | 410 | bb = alloc_block (); |
6de9cd9a DN |
411 | |
412 | bb->index = last_basic_block; | |
413 | bb->flags = BB_NEW; | |
414 | bb->stmt_list = h ? h : alloc_stmt_list (); | |
415 | ||
416 | /* Add the new block to the linked list of blocks. */ | |
417 | link_block (bb, after); | |
418 | ||
419 | /* Grow the basic block array if needed. */ | |
420 | if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info)) | |
421 | { | |
422 | size_t new_size = last_basic_block + (last_basic_block + 3) / 4; | |
423 | VARRAY_GROW (basic_block_info, new_size); | |
424 | } | |
425 | ||
426 | /* Add the newly created block to the array. */ | |
427 | BASIC_BLOCK (last_basic_block) = bb; | |
428 | ||
429 | create_block_annotation (bb); | |
430 | ||
431 | n_basic_blocks++; | |
432 | last_basic_block++; | |
433 | ||
434 | initialize_bb_rbi (bb); | |
435 | return bb; | |
436 | } | |
437 | ||
438 | ||
439 | /*--------------------------------------------------------------------------- | |
440 | Edge creation | |
441 | ---------------------------------------------------------------------------*/ | |
442 | ||
443 | /* Join all the blocks in the flowgraph. */ | |
444 | ||
445 | static void | |
446 | make_edges (void) | |
447 | { | |
448 | basic_block bb; | |
6de9cd9a DN |
449 | |
450 | /* Create an edge from entry to the first block with executable | |
451 | statements in it. */ | |
452 | make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU); | |
453 | ||
adb35797 | 454 | /* Traverse the basic block array placing edges. */ |
6de9cd9a DN |
455 | FOR_EACH_BB (bb) |
456 | { | |
457 | tree first = first_stmt (bb); | |
458 | tree last = last_stmt (bb); | |
459 | ||
460 | if (first) | |
461 | { | |
462 | /* Edges for statements that always alter flow control. */ | |
463 | if (is_ctrl_stmt (last)) | |
464 | make_ctrl_stmt_edges (bb); | |
465 | ||
466 | /* Edges for statements that sometimes alter flow control. */ | |
467 | if (is_ctrl_altering_stmt (last)) | |
468 | make_exit_edges (bb); | |
469 | } | |
470 | ||
471 | /* Finally, if no edges were created above, this is a regular | |
472 | basic block that only needs a fallthru edge. */ | |
628f6a4e | 473 | if (EDGE_COUNT (bb->succs) == 0) |
6de9cd9a DN |
474 | make_edge (bb, bb->next_bb, EDGE_FALLTHRU); |
475 | } | |
476 | ||
6de9cd9a DN |
477 | /* We do not care about fake edges, so remove any that the CFG |
478 | builder inserted for completeness. */ | |
6809cbf9 | 479 | remove_fake_exit_edges (); |
6de9cd9a | 480 | |
6de9cd9a DN |
481 | /* Clean up the graph and warn for unreachable code. */ |
482 | cleanup_tree_cfg (); | |
483 | } | |
484 | ||
485 | ||
486 | /* Create edges for control statement at basic block BB. */ | |
487 | ||
488 | static void | |
489 | make_ctrl_stmt_edges (basic_block bb) | |
490 | { | |
491 | tree last = last_stmt (bb); | |
6de9cd9a | 492 | |
1e128c5f | 493 | gcc_assert (last); |
6de9cd9a DN |
494 | switch (TREE_CODE (last)) |
495 | { | |
496 | case GOTO_EXPR: | |
497 | make_goto_expr_edges (bb); | |
498 | break; | |
499 | ||
500 | case RETURN_EXPR: | |
501 | make_edge (bb, EXIT_BLOCK_PTR, 0); | |
502 | break; | |
503 | ||
504 | case COND_EXPR: | |
505 | make_cond_expr_edges (bb); | |
506 | break; | |
507 | ||
508 | case SWITCH_EXPR: | |
509 | make_switch_expr_edges (bb); | |
510 | break; | |
511 | ||
512 | case RESX_EXPR: | |
513 | make_eh_edges (last); | |
514 | /* Yet another NORETURN hack. */ | |
628f6a4e | 515 | if (EDGE_COUNT (bb->succs) == 0) |
6de9cd9a DN |
516 | make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE); |
517 | break; | |
518 | ||
519 | default: | |
1e128c5f | 520 | gcc_unreachable (); |
6de9cd9a DN |
521 | } |
522 | } | |
523 | ||
524 | ||
525 | /* Create exit edges for statements in block BB that alter the flow of | |
526 | control. Statements that alter the control flow are 'goto', 'return' | |
527 | and calls to non-returning functions. */ | |
528 | ||
529 | static void | |
530 | make_exit_edges (basic_block bb) | |
531 | { | |
cd709752 | 532 | tree last = last_stmt (bb), op; |
6de9cd9a | 533 | |
1e128c5f | 534 | gcc_assert (last); |
6de9cd9a DN |
535 | switch (TREE_CODE (last)) |
536 | { | |
537 | case CALL_EXPR: | |
538 | /* If this function receives a nonlocal goto, then we need to | |
539 | make edges from this call site to all the nonlocal goto | |
540 | handlers. */ | |
541 | if (TREE_SIDE_EFFECTS (last) | |
542 | && current_function_has_nonlocal_label) | |
543 | make_goto_expr_edges (bb); | |
544 | ||
545 | /* If this statement has reachable exception handlers, then | |
546 | create abnormal edges to them. */ | |
547 | make_eh_edges (last); | |
548 | ||
549 | /* Some calls are known not to return. For such calls we create | |
550 | a fake edge. | |
551 | ||
552 | We really need to revamp how we build edges so that it's not | |
553 | such a bloody pain to avoid creating edges for this case since | |
554 | all we do is remove these edges when we're done building the | |
555 | CFG. */ | |
6e14af16 | 556 | if (call_expr_flags (last) & ECF_NORETURN) |
6de9cd9a DN |
557 | { |
558 | make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE); | |
559 | return; | |
560 | } | |
561 | ||
562 | /* Don't forget the fall-thru edge. */ | |
563 | make_edge (bb, bb->next_bb, EDGE_FALLTHRU); | |
564 | break; | |
565 | ||
566 | case MODIFY_EXPR: | |
567 | /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR | |
568 | may have an abnormal edge. Search the RHS for this case and | |
569 | create any required edges. */ | |
cd709752 RH |
570 | op = get_call_expr_in (last); |
571 | if (op && TREE_SIDE_EFFECTS (op) | |
6de9cd9a DN |
572 | && current_function_has_nonlocal_label) |
573 | make_goto_expr_edges (bb); | |
574 | ||
575 | make_eh_edges (last); | |
576 | make_edge (bb, bb->next_bb, EDGE_FALLTHRU); | |
577 | break; | |
578 | ||
579 | default: | |
1e128c5f | 580 | gcc_unreachable (); |
6de9cd9a DN |
581 | } |
582 | } | |
583 | ||
584 | ||
585 | /* Create the edges for a COND_EXPR starting at block BB. | |
586 | At this point, both clauses must contain only simple gotos. */ | |
587 | ||
588 | static void | |
589 | make_cond_expr_edges (basic_block bb) | |
590 | { | |
591 | tree entry = last_stmt (bb); | |
592 | basic_block then_bb, else_bb; | |
593 | tree then_label, else_label; | |
594 | ||
1e128c5f GB |
595 | gcc_assert (entry); |
596 | gcc_assert (TREE_CODE (entry) == COND_EXPR); | |
6de9cd9a DN |
597 | |
598 | /* Entry basic blocks for each component. */ | |
599 | then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry)); | |
600 | else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry)); | |
601 | then_bb = label_to_block (then_label); | |
602 | else_bb = label_to_block (else_label); | |
603 | ||
604 | make_edge (bb, then_bb, EDGE_TRUE_VALUE); | |
605 | make_edge (bb, else_bb, EDGE_FALSE_VALUE); | |
606 | } | |
607 | ||
d6be0d7f | 608 | /* Hashing routine for EDGE_TO_CASES. */ |
92b6dff3 JL |
609 | |
610 | static hashval_t | |
d6be0d7f | 611 | edge_to_cases_hash (const void *p) |
92b6dff3 | 612 | { |
d6be0d7f | 613 | edge e = ((struct edge_to_cases_elt *)p)->e; |
92b6dff3 JL |
614 | |
615 | /* Hash on the edge itself (which is a pointer). */ | |
616 | return htab_hash_pointer (e); | |
617 | } | |
618 | ||
d6be0d7f | 619 | /* Equality routine for EDGE_TO_CASES, edges are unique, so testing |
92b6dff3 JL |
620 | for equality is just a pointer comparison. */ |
621 | ||
622 | static int | |
d6be0d7f | 623 | edge_to_cases_eq (const void *p1, const void *p2) |
92b6dff3 | 624 | { |
d6be0d7f JL |
625 | edge e1 = ((struct edge_to_cases_elt *)p1)->e; |
626 | edge e2 = ((struct edge_to_cases_elt *)p2)->e; | |
92b6dff3 JL |
627 | |
628 | return e1 == e2; | |
629 | } | |
630 | ||
d6be0d7f JL |
631 | /* Called for each element in the hash table (P) as we delete the |
632 | edge to cases hash table. | |
633 | ||
634 | Clear all the TREE_CHAINs to prevent problems with copying of | |
635 | SWITCH_EXPRs and structure sharing rules, then free the hash table | |
636 | element. */ | |
637 | ||
638 | static void | |
639 | edge_to_cases_cleanup (void *p) | |
640 | { | |
641 | struct edge_to_cases_elt *elt = p; | |
642 | tree t, next; | |
643 | ||
644 | for (t = elt->case_labels; t; t = next) | |
645 | { | |
646 | next = TREE_CHAIN (t); | |
647 | TREE_CHAIN (t) = NULL; | |
648 | } | |
649 | free (p); | |
650 | } | |
651 | ||
652 | /* Start recording information mapping edges to case labels. */ | |
653 | ||
654 | static void | |
655 | start_recording_case_labels (void) | |
656 | { | |
657 | gcc_assert (edge_to_cases == NULL); | |
658 | ||
659 | edge_to_cases = htab_create (37, | |
660 | edge_to_cases_hash, | |
661 | edge_to_cases_eq, | |
662 | edge_to_cases_cleanup); | |
663 | } | |
664 | ||
665 | /* Return nonzero if we are recording information for case labels. */ | |
666 | ||
667 | static bool | |
668 | recording_case_labels_p (void) | |
669 | { | |
670 | return (edge_to_cases != NULL); | |
671 | } | |
672 | ||
673 | /* Stop recording information mapping edges to case labels and | |
674 | remove any information we have recorded. */ | |
675 | static void | |
676 | end_recording_case_labels (void) | |
677 | { | |
678 | htab_delete (edge_to_cases); | |
679 | edge_to_cases = NULL; | |
680 | } | |
681 | ||
92b6dff3 JL |
682 | /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */ |
683 | ||
684 | static void | |
685 | record_switch_edge (edge e, tree case_label) | |
686 | { | |
d6be0d7f | 687 | struct edge_to_cases_elt *elt; |
92b6dff3 JL |
688 | void **slot; |
689 | ||
690 | /* Build a hash table element so we can see if E is already | |
691 | in the table. */ | |
d6be0d7f | 692 | elt = xmalloc (sizeof (struct edge_to_cases_elt)); |
92b6dff3 | 693 | elt->e = e; |
d6be0d7f | 694 | elt->case_labels = case_label; |
92b6dff3 | 695 | |
d6be0d7f | 696 | slot = htab_find_slot (edge_to_cases, elt, INSERT); |
92b6dff3 JL |
697 | |
698 | if (*slot == NULL) | |
699 | { | |
700 | /* E was not in the hash table. Install E into the hash table. */ | |
701 | *slot = (void *)elt; | |
702 | } | |
703 | else | |
704 | { | |
705 | /* E was already in the hash table. Free ELT as we do not need it | |
706 | anymore. */ | |
707 | free (elt); | |
708 | ||
709 | /* Get the entry stored in the hash table. */ | |
d6be0d7f | 710 | elt = (struct edge_to_cases_elt *) *slot; |
92b6dff3 | 711 | |
d6be0d7f JL |
712 | /* Add it to the chain of CASE_LABEL_EXPRs referencing E. */ |
713 | TREE_CHAIN (case_label) = elt->case_labels; | |
714 | elt->case_labels = case_label; | |
92b6dff3 JL |
715 | } |
716 | } | |
717 | ||
d6be0d7f JL |
718 | /* If we are inside a {start,end}_recording_cases block, then return |
719 | a chain of CASE_LABEL_EXPRs from T which reference E. | |
720 | ||
721 | Otherwise return NULL. */ | |
92b6dff3 JL |
722 | |
723 | static tree | |
d6be0d7f | 724 | get_cases_for_edge (edge e, tree t) |
92b6dff3 | 725 | { |
d6be0d7f | 726 | struct edge_to_cases_elt elt, *elt_p; |
92b6dff3 | 727 | void **slot; |
d6be0d7f JL |
728 | size_t i, n; |
729 | tree vec; | |
92b6dff3 | 730 | |
d6be0d7f JL |
731 | /* If we are not recording cases, then we do not have CASE_LABEL_EXPR |
732 | chains available. Return NULL so the caller can detect this case. */ | |
733 | if (!recording_case_labels_p ()) | |
734 | return NULL; | |
735 | ||
736 | restart: | |
92b6dff3 | 737 | elt.e = e; |
d6be0d7f JL |
738 | elt.case_labels = NULL; |
739 | slot = htab_find_slot (edge_to_cases, &elt, NO_INSERT); | |
92b6dff3 JL |
740 | |
741 | if (slot) | |
742 | { | |
d6be0d7f JL |
743 | elt_p = (struct edge_to_cases_elt *)*slot; |
744 | return elt_p->case_labels; | |
92b6dff3 JL |
745 | } |
746 | ||
d6be0d7f JL |
747 | /* If we did not find E in the hash table, then this must be the first |
748 | time we have been queried for information about E & T. Add all the | |
749 | elements from T to the hash table then perform the query again. */ | |
92b6dff3 | 750 | |
d6be0d7f | 751 | vec = SWITCH_LABELS (t); |
92b6dff3 | 752 | n = TREE_VEC_LENGTH (vec); |
92b6dff3 JL |
753 | for (i = 0; i < n; i++) |
754 | { | |
d6be0d7f JL |
755 | tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i)); |
756 | basic_block label_bb = label_to_block (lab); | |
757 | record_switch_edge (find_edge (e->src, label_bb), TREE_VEC_ELT (vec, i)); | |
92b6dff3 | 758 | } |
d6be0d7f | 759 | goto restart; |
92b6dff3 | 760 | } |
6de9cd9a DN |
761 | |
762 | /* Create the edges for a SWITCH_EXPR starting at block BB. | |
763 | At this point, the switch body has been lowered and the | |
764 | SWITCH_LABELS filled in, so this is in effect a multi-way branch. */ | |
765 | ||
766 | static void | |
767 | make_switch_expr_edges (basic_block bb) | |
768 | { | |
769 | tree entry = last_stmt (bb); | |
770 | size_t i, n; | |
771 | tree vec; | |
772 | ||
773 | vec = SWITCH_LABELS (entry); | |
774 | n = TREE_VEC_LENGTH (vec); | |
775 | ||
776 | for (i = 0; i < n; ++i) | |
777 | { | |
778 | tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i)); | |
779 | basic_block label_bb = label_to_block (lab); | |
d6be0d7f | 780 | make_edge (bb, label_bb, 0); |
6de9cd9a DN |
781 | } |
782 | } | |
783 | ||
784 | ||
785 | /* Return the basic block holding label DEST. */ | |
786 | ||
787 | basic_block | |
788 | label_to_block (tree dest) | |
789 | { | |
242229bb JH |
790 | int uid = LABEL_DECL_UID (dest); |
791 | ||
f0b698c1 KH |
792 | /* We would die hard when faced by an undefined label. Emit a label to |
793 | the very first basic block. This will hopefully make even the dataflow | |
242229bb JH |
794 | and undefined variable warnings quite right. */ |
795 | if ((errorcount || sorrycount) && uid < 0) | |
796 | { | |
797 | block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0)); | |
798 | tree stmt; | |
799 | ||
800 | stmt = build1 (LABEL_EXPR, void_type_node, dest); | |
801 | bsi_insert_before (&bsi, stmt, BSI_NEW_STMT); | |
802 | uid = LABEL_DECL_UID (dest); | |
803 | } | |
804 | return VARRAY_BB (label_to_block_map, uid); | |
6de9cd9a DN |
805 | } |
806 | ||
807 | ||
808 | /* Create edges for a goto statement at block BB. */ | |
809 | ||
810 | static void | |
811 | make_goto_expr_edges (basic_block bb) | |
812 | { | |
813 | tree goto_t, dest; | |
814 | basic_block target_bb; | |
815 | int for_call; | |
816 | block_stmt_iterator last = bsi_last (bb); | |
817 | ||
818 | goto_t = bsi_stmt (last); | |
819 | ||
820 | /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR, | |
821 | CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting | |
822 | from a nonlocal goto. */ | |
823 | if (TREE_CODE (goto_t) != GOTO_EXPR) | |
824 | { | |
825 | dest = error_mark_node; | |
826 | for_call = 1; | |
827 | } | |
828 | else | |
829 | { | |
830 | dest = GOTO_DESTINATION (goto_t); | |
831 | for_call = 0; | |
832 | ||
833 | /* A GOTO to a local label creates normal edges. */ | |
834 | if (simple_goto_p (goto_t)) | |
835 | { | |
62b857ea | 836 | edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU); |
9506ac2b PB |
837 | #ifdef USE_MAPPED_LOCATION |
838 | e->goto_locus = EXPR_LOCATION (goto_t); | |
839 | #else | |
62b857ea | 840 | e->goto_locus = EXPR_LOCUS (goto_t); |
9506ac2b | 841 | #endif |
6de9cd9a DN |
842 | bsi_remove (&last); |
843 | return; | |
844 | } | |
845 | ||
9cf737f8 | 846 | /* Nothing more to do for nonlocal gotos. */ |
6de9cd9a DN |
847 | if (TREE_CODE (dest) == LABEL_DECL) |
848 | return; | |
849 | ||
850 | /* Computed gotos remain. */ | |
851 | } | |
852 | ||
853 | /* Look for the block starting with the destination label. In the | |
854 | case of a computed goto, make an edge to any label block we find | |
855 | in the CFG. */ | |
856 | FOR_EACH_BB (target_bb) | |
857 | { | |
858 | block_stmt_iterator bsi; | |
859 | ||
860 | for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
861 | { | |
862 | tree target = bsi_stmt (bsi); | |
863 | ||
864 | if (TREE_CODE (target) != LABEL_EXPR) | |
865 | break; | |
866 | ||
867 | if ( | |
868 | /* Computed GOTOs. Make an edge to every label block that has | |
869 | been marked as a potential target for a computed goto. */ | |
870 | (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0) | |
871 | /* Nonlocal GOTO target. Make an edge to every label block | |
872 | that has been marked as a potential target for a nonlocal | |
873 | goto. */ | |
874 | || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1)) | |
875 | { | |
876 | make_edge (bb, target_bb, EDGE_ABNORMAL); | |
877 | break; | |
878 | } | |
879 | } | |
880 | } | |
881 | ||
882 | /* Degenerate case of computed goto with no labels. */ | |
628f6a4e | 883 | if (!for_call && EDGE_COUNT (bb->succs) == 0) |
6de9cd9a DN |
884 | make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE); |
885 | } | |
886 | ||
887 | ||
888 | /*--------------------------------------------------------------------------- | |
889 | Flowgraph analysis | |
890 | ---------------------------------------------------------------------------*/ | |
891 | ||
892 | /* Remove unreachable blocks and other miscellaneous clean up work. */ | |
893 | ||
56b043c8 | 894 | bool |
6de9cd9a DN |
895 | cleanup_tree_cfg (void) |
896 | { | |
56b043c8 | 897 | bool retval = false; |
6de9cd9a DN |
898 | |
899 | timevar_push (TV_TREE_CLEANUP_CFG); | |
900 | ||
8f28be81 | 901 | retval = cleanup_control_flow (); |
26d4492f | 902 | retval |= delete_unreachable_blocks (); |
d6be0d7f | 903 | |
63bb59a3 ZD |
904 | /* cleanup_forwarder_blocks can redirect edges out of SWITCH_EXPRs, |
905 | which can get expensive. So we want to enable recording of edge | |
906 | to CASE_LABEL_EXPR mappings around the call to | |
907 | cleanup_forwarder_blocks. */ | |
d6be0d7f | 908 | start_recording_case_labels (); |
63bb59a3 | 909 | retval |= cleanup_forwarder_blocks (); |
d6be0d7f | 910 | end_recording_case_labels (); |
6de9cd9a | 911 | |
8f28be81 KH |
912 | #ifdef ENABLE_CHECKING |
913 | if (retval) | |
26d4492f KH |
914 | { |
915 | gcc_assert (!cleanup_control_flow ()); | |
916 | gcc_assert (!delete_unreachable_blocks ()); | |
63bb59a3 | 917 | gcc_assert (!cleanup_forwarder_blocks ()); |
26d4492f | 918 | } |
8f28be81 KH |
919 | #endif |
920 | ||
6de9cd9a DN |
921 | /* Merging the blocks creates no new opportunities for the other |
922 | optimizations, so do it here. */ | |
086aa12e | 923 | retval |= merge_seq_blocks (); |
6de9cd9a DN |
924 | |
925 | compact_blocks (); | |
926 | ||
927 | #ifdef ENABLE_CHECKING | |
928 | verify_flow_info (); | |
929 | #endif | |
930 | timevar_pop (TV_TREE_CLEANUP_CFG); | |
56b043c8 | 931 | return retval; |
6de9cd9a DN |
932 | } |
933 | ||
934 | ||
f698d217 SB |
935 | /* Cleanup useless labels in basic blocks. This is something we wish |
936 | to do early because it allows us to group case labels before creating | |
937 | the edges for the CFG, and it speeds up block statement iterators in | |
938 | all passes later on. | |
939 | We only run this pass once, running it more than once is probably not | |
940 | profitable. */ | |
941 | ||
942 | /* A map from basic block index to the leading label of that block. */ | |
943 | static tree *label_for_bb; | |
944 | ||
945 | /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */ | |
946 | static void | |
947 | update_eh_label (struct eh_region *region) | |
948 | { | |
949 | tree old_label = get_eh_region_tree_label (region); | |
950 | if (old_label) | |
951 | { | |
165b54c3 SB |
952 | tree new_label; |
953 | basic_block bb = label_to_block (old_label); | |
954 | ||
955 | /* ??? After optimizing, there may be EH regions with labels | |
956 | that have already been removed from the function body, so | |
957 | there is no basic block for them. */ | |
958 | if (! bb) | |
959 | return; | |
960 | ||
961 | new_label = label_for_bb[bb->index]; | |
f698d217 SB |
962 | set_eh_region_tree_label (region, new_label); |
963 | } | |
964 | } | |
965 | ||
242229bb JH |
966 | /* Given LABEL return the first label in the same basic block. */ |
967 | static tree | |
968 | main_block_label (tree label) | |
969 | { | |
970 | basic_block bb = label_to_block (label); | |
971 | ||
972 | /* label_to_block possibly inserted undefined label into the chain. */ | |
973 | if (!label_for_bb[bb->index]) | |
974 | label_for_bb[bb->index] = label; | |
975 | return label_for_bb[bb->index]; | |
976 | } | |
977 | ||
b986ebf3 | 978 | /* Cleanup redundant labels. This is a three-step process: |
f698d217 SB |
979 | 1) Find the leading label for each block. |
980 | 2) Redirect all references to labels to the leading labels. | |
981 | 3) Cleanup all useless labels. */ | |
6de9cd9a | 982 | |
165b54c3 | 983 | void |
6de9cd9a DN |
984 | cleanup_dead_labels (void) |
985 | { | |
986 | basic_block bb; | |
f698d217 | 987 | label_for_bb = xcalloc (last_basic_block, sizeof (tree)); |
6de9cd9a DN |
988 | |
989 | /* Find a suitable label for each block. We use the first user-defined | |
f0b698c1 | 990 | label if there is one, or otherwise just the first label we see. */ |
6de9cd9a DN |
991 | FOR_EACH_BB (bb) |
992 | { | |
993 | block_stmt_iterator i; | |
994 | ||
995 | for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i)) | |
996 | { | |
997 | tree label, stmt = bsi_stmt (i); | |
998 | ||
999 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
1000 | break; | |
1001 | ||
1002 | label = LABEL_EXPR_LABEL (stmt); | |
1003 | ||
1004 | /* If we have not yet seen a label for the current block, | |
1005 | remember this one and see if there are more labels. */ | |
1006 | if (! label_for_bb[bb->index]) | |
1007 | { | |
1008 | label_for_bb[bb->index] = label; | |
1009 | continue; | |
1010 | } | |
1011 | ||
1012 | /* If we did see a label for the current block already, but it | |
1013 | is an artificially created label, replace it if the current | |
1014 | label is a user defined label. */ | |
1015 | if (! DECL_ARTIFICIAL (label) | |
1016 | && DECL_ARTIFICIAL (label_for_bb[bb->index])) | |
1017 | { | |
1018 | label_for_bb[bb->index] = label; | |
1019 | break; | |
1020 | } | |
1021 | } | |
1022 | } | |
1023 | ||
f698d217 SB |
1024 | /* Now redirect all jumps/branches to the selected label. |
1025 | First do so for each block ending in a control statement. */ | |
6de9cd9a DN |
1026 | FOR_EACH_BB (bb) |
1027 | { | |
1028 | tree stmt = last_stmt (bb); | |
1029 | if (!stmt) | |
1030 | continue; | |
1031 | ||
1032 | switch (TREE_CODE (stmt)) | |
1033 | { | |
1034 | case COND_EXPR: | |
1035 | { | |
1036 | tree true_branch, false_branch; | |
6de9cd9a DN |
1037 | |
1038 | true_branch = COND_EXPR_THEN (stmt); | |
1039 | false_branch = COND_EXPR_ELSE (stmt); | |
6de9cd9a | 1040 | |
242229bb JH |
1041 | GOTO_DESTINATION (true_branch) |
1042 | = main_block_label (GOTO_DESTINATION (true_branch)); | |
1043 | GOTO_DESTINATION (false_branch) | |
1044 | = main_block_label (GOTO_DESTINATION (false_branch)); | |
6de9cd9a DN |
1045 | |
1046 | break; | |
1047 | } | |
1048 | ||
1049 | case SWITCH_EXPR: | |
1050 | { | |
1051 | size_t i; | |
1052 | tree vec = SWITCH_LABELS (stmt); | |
1053 | size_t n = TREE_VEC_LENGTH (vec); | |
1054 | ||
1055 | /* Replace all destination labels. */ | |
1056 | for (i = 0; i < n; ++i) | |
92b6dff3 JL |
1057 | { |
1058 | tree elt = TREE_VEC_ELT (vec, i); | |
1059 | tree label = main_block_label (CASE_LABEL (elt)); | |
d6be0d7f | 1060 | CASE_LABEL (elt) = label; |
92b6dff3 | 1061 | } |
6de9cd9a DN |
1062 | break; |
1063 | } | |
1064 | ||
f667741c SB |
1065 | /* We have to handle GOTO_EXPRs until they're removed, and we don't |
1066 | remove them until after we've created the CFG edges. */ | |
1067 | case GOTO_EXPR: | |
242229bb JH |
1068 | if (! computed_goto_p (stmt)) |
1069 | { | |
1070 | GOTO_DESTINATION (stmt) | |
1071 | = main_block_label (GOTO_DESTINATION (stmt)); | |
1072 | break; | |
1073 | } | |
f667741c | 1074 | |
6de9cd9a DN |
1075 | default: |
1076 | break; | |
1077 | } | |
1078 | } | |
1079 | ||
f698d217 SB |
1080 | for_each_eh_region (update_eh_label); |
1081 | ||
6de9cd9a DN |
1082 | /* Finally, purge dead labels. All user-defined labels and labels that |
1083 | can be the target of non-local gotos are preserved. */ | |
1084 | FOR_EACH_BB (bb) | |
1085 | { | |
1086 | block_stmt_iterator i; | |
1087 | tree label_for_this_bb = label_for_bb[bb->index]; | |
1088 | ||
1089 | if (! label_for_this_bb) | |
1090 | continue; | |
1091 | ||
1092 | for (i = bsi_start (bb); !bsi_end_p (i); ) | |
1093 | { | |
1094 | tree label, stmt = bsi_stmt (i); | |
1095 | ||
1096 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
1097 | break; | |
1098 | ||
1099 | label = LABEL_EXPR_LABEL (stmt); | |
1100 | ||
1101 | if (label == label_for_this_bb | |
1102 | || ! DECL_ARTIFICIAL (label) | |
1103 | || DECL_NONLOCAL (label)) | |
1104 | bsi_next (&i); | |
1105 | else | |
1106 | bsi_remove (&i); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | free (label_for_bb); | |
1111 | } | |
1112 | ||
f667741c SB |
1113 | /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE), |
1114 | and scan the sorted vector of cases. Combine the ones jumping to the | |
1115 | same label. | |
1116 | Eg. three separate entries 1: 2: 3: become one entry 1..3: */ | |
1117 | ||
165b54c3 | 1118 | void |
f667741c SB |
1119 | group_case_labels (void) |
1120 | { | |
1121 | basic_block bb; | |
1122 | ||
1123 | FOR_EACH_BB (bb) | |
1124 | { | |
1125 | tree stmt = last_stmt (bb); | |
1126 | if (stmt && TREE_CODE (stmt) == SWITCH_EXPR) | |
1127 | { | |
1128 | tree labels = SWITCH_LABELS (stmt); | |
1129 | int old_size = TREE_VEC_LENGTH (labels); | |
1130 | int i, j, new_size = old_size; | |
29c4d22b AP |
1131 | tree default_case = TREE_VEC_ELT (labels, old_size - 1); |
1132 | tree default_label; | |
1133 | ||
66efeafc | 1134 | /* The default label is always the last case in a switch |
29c4d22b AP |
1135 | statement after gimplification. */ |
1136 | default_label = CASE_LABEL (default_case); | |
f667741c SB |
1137 | |
1138 | /* Look for possible opportunities to merge cases. | |
1139 | Ignore the last element of the label vector because it | |
1140 | must be the default case. */ | |
1141 | i = 0; | |
d717e500 | 1142 | while (i < old_size - 1) |
f667741c SB |
1143 | { |
1144 | tree base_case, base_label, base_high, type; | |
1145 | base_case = TREE_VEC_ELT (labels, i); | |
1146 | ||
1e128c5f | 1147 | gcc_assert (base_case); |
f667741c | 1148 | base_label = CASE_LABEL (base_case); |
31e9eea2 SB |
1149 | |
1150 | /* Discard cases that have the same destination as the | |
1151 | default case. */ | |
1152 | if (base_label == default_label) | |
1153 | { | |
1154 | TREE_VEC_ELT (labels, i) = NULL_TREE; | |
1155 | i++; | |
29c4d22b | 1156 | new_size--; |
31e9eea2 SB |
1157 | continue; |
1158 | } | |
1159 | ||
1160 | type = TREE_TYPE (CASE_LOW (base_case)); | |
f667741c SB |
1161 | base_high = CASE_HIGH (base_case) ? |
1162 | CASE_HIGH (base_case) : CASE_LOW (base_case); | |
d717e500 | 1163 | i++; |
f667741c SB |
1164 | /* Try to merge case labels. Break out when we reach the end |
1165 | of the label vector or when we cannot merge the next case | |
1166 | label with the current one. */ | |
d717e500 | 1167 | while (i < old_size - 1) |
f667741c | 1168 | { |
d717e500 | 1169 | tree merge_case = TREE_VEC_ELT (labels, i); |
f667741c SB |
1170 | tree merge_label = CASE_LABEL (merge_case); |
1171 | tree t = int_const_binop (PLUS_EXPR, base_high, | |
1172 | integer_one_node, 1); | |
1173 | ||
1174 | /* Merge the cases if they jump to the same place, | |
1175 | and their ranges are consecutive. */ | |
1176 | if (merge_label == base_label | |
1177 | && tree_int_cst_equal (CASE_LOW (merge_case), t)) | |
1178 | { | |
1179 | base_high = CASE_HIGH (merge_case) ? | |
1180 | CASE_HIGH (merge_case) : CASE_LOW (merge_case); | |
1181 | CASE_HIGH (base_case) = base_high; | |
1182 | TREE_VEC_ELT (labels, i) = NULL_TREE; | |
1183 | new_size--; | |
d717e500 | 1184 | i++; |
f667741c SB |
1185 | } |
1186 | else | |
1187 | break; | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | /* Compress the case labels in the label vector, and adjust the | |
1192 | length of the vector. */ | |
1193 | for (i = 0, j = 0; i < new_size; i++) | |
1194 | { | |
1195 | while (! TREE_VEC_ELT (labels, j)) | |
1196 | j++; | |
1197 | TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++); | |
1198 | } | |
1199 | TREE_VEC_LENGTH (labels) = new_size; | |
1200 | } | |
1201 | } | |
1202 | } | |
6de9cd9a DN |
1203 | |
1204 | /* Checks whether we can merge block B into block A. */ | |
1205 | ||
1206 | static bool | |
1207 | tree_can_merge_blocks_p (basic_block a, basic_block b) | |
1208 | { | |
1209 | tree stmt; | |
1210 | block_stmt_iterator bsi; | |
1211 | ||
628f6a4e | 1212 | if (EDGE_COUNT (a->succs) != 1) |
6de9cd9a DN |
1213 | return false; |
1214 | ||
628f6a4e | 1215 | if (EDGE_SUCC (a, 0)->flags & EDGE_ABNORMAL) |
6de9cd9a DN |
1216 | return false; |
1217 | ||
628f6a4e | 1218 | if (EDGE_SUCC (a, 0)->dest != b) |
6de9cd9a DN |
1219 | return false; |
1220 | ||
628f6a4e | 1221 | if (EDGE_COUNT (b->preds) > 1) |
6de9cd9a DN |
1222 | return false; |
1223 | ||
26e75214 KH |
1224 | if (b == EXIT_BLOCK_PTR) |
1225 | return false; | |
1226 | ||
6de9cd9a DN |
1227 | /* If A ends by a statement causing exceptions or something similar, we |
1228 | cannot merge the blocks. */ | |
1229 | stmt = last_stmt (a); | |
1230 | if (stmt && stmt_ends_bb_p (stmt)) | |
1231 | return false; | |
1232 | ||
1233 | /* Do not allow a block with only a non-local label to be merged. */ | |
1234 | if (stmt && TREE_CODE (stmt) == LABEL_EXPR | |
1235 | && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt))) | |
1236 | return false; | |
1237 | ||
1238 | /* There may be no phi nodes at the start of b. Most of these degenerate | |
1239 | phi nodes should be cleaned up by kill_redundant_phi_nodes. */ | |
1240 | if (phi_nodes (b)) | |
1241 | return false; | |
1242 | ||
1243 | /* Do not remove user labels. */ | |
1244 | for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi)) | |
1245 | { | |
1246 | stmt = bsi_stmt (bsi); | |
1247 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
1248 | break; | |
1249 | if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt))) | |
1250 | return false; | |
1251 | } | |
1252 | ||
1253 | return true; | |
1254 | } | |
1255 | ||
1256 | ||
1257 | /* Merge block B into block A. */ | |
1258 | ||
1259 | static void | |
1260 | tree_merge_blocks (basic_block a, basic_block b) | |
1261 | { | |
1262 | block_stmt_iterator bsi; | |
1263 | tree_stmt_iterator last; | |
1264 | ||
1265 | if (dump_file) | |
1266 | fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index); | |
1267 | ||
1268 | /* Ensure that B follows A. */ | |
1269 | move_block_after (b, a); | |
1270 | ||
628f6a4e | 1271 | gcc_assert (EDGE_SUCC (a, 0)->flags & EDGE_FALLTHRU); |
1e128c5f | 1272 | gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a))); |
6de9cd9a DN |
1273 | |
1274 | /* Remove labels from B and set bb_for_stmt to A for other statements. */ | |
1275 | for (bsi = bsi_start (b); !bsi_end_p (bsi);) | |
1276 | { | |
1277 | if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR) | |
1278 | bsi_remove (&bsi); | |
1279 | else | |
1280 | { | |
1281 | set_bb_for_stmt (bsi_stmt (bsi), a); | |
1282 | bsi_next (&bsi); | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* Merge the chains. */ | |
1287 | last = tsi_last (a->stmt_list); | |
1288 | tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT); | |
1289 | b->stmt_list = NULL; | |
1290 | } | |
1291 | ||
1292 | ||
1293 | /* Walk the function tree removing unnecessary statements. | |
1294 | ||
1295 | * Empty statement nodes are removed | |
1296 | ||
1297 | * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed | |
1298 | ||
1299 | * Unnecessary COND_EXPRs are removed | |
1300 | ||
1301 | * Some unnecessary BIND_EXPRs are removed | |
1302 | ||
1303 | Clearly more work could be done. The trick is doing the analysis | |
1304 | and removal fast enough to be a net improvement in compile times. | |
1305 | ||
1306 | Note that when we remove a control structure such as a COND_EXPR | |
1307 | BIND_EXPR, or TRY block, we will need to repeat this optimization pass | |
1308 | to ensure we eliminate all the useless code. */ | |
1309 | ||
1310 | struct rus_data | |
1311 | { | |
1312 | tree *last_goto; | |
1313 | bool repeat; | |
1314 | bool may_throw; | |
1315 | bool may_branch; | |
1316 | bool has_label; | |
1317 | }; | |
1318 | ||
1319 | static void remove_useless_stmts_1 (tree *, struct rus_data *); | |
1320 | ||
1321 | static bool | |
1322 | remove_useless_stmts_warn_notreached (tree stmt) | |
1323 | { | |
9506ac2b | 1324 | if (EXPR_HAS_LOCATION (stmt)) |
6de9cd9a | 1325 | { |
9506ac2b | 1326 | location_t loc = EXPR_LOCATION (stmt); |
43e05e45 SB |
1327 | if (LOCATION_LINE (loc) > 0) |
1328 | { | |
1329 | warning ("%Hwill never be executed", &loc); | |
1330 | return true; | |
1331 | } | |
6de9cd9a DN |
1332 | } |
1333 | ||
1334 | switch (TREE_CODE (stmt)) | |
1335 | { | |
1336 | case STATEMENT_LIST: | |
1337 | { | |
1338 | tree_stmt_iterator i; | |
1339 | for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i)) | |
1340 | if (remove_useless_stmts_warn_notreached (tsi_stmt (i))) | |
1341 | return true; | |
1342 | } | |
1343 | break; | |
1344 | ||
1345 | case COND_EXPR: | |
1346 | if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt))) | |
1347 | return true; | |
1348 | if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt))) | |
1349 | return true; | |
1350 | if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt))) | |
1351 | return true; | |
1352 | break; | |
1353 | ||
1354 | case TRY_FINALLY_EXPR: | |
1355 | case TRY_CATCH_EXPR: | |
1356 | if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0))) | |
1357 | return true; | |
1358 | if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1))) | |
1359 | return true; | |
1360 | break; | |
1361 | ||
1362 | case CATCH_EXPR: | |
1363 | return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt)); | |
1364 | case EH_FILTER_EXPR: | |
1365 | return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt)); | |
1366 | case BIND_EXPR: | |
1367 | return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt)); | |
1368 | ||
1369 | default: | |
1370 | /* Not a live container. */ | |
1371 | break; | |
1372 | } | |
1373 | ||
1374 | return false; | |
1375 | } | |
1376 | ||
1377 | static void | |
1378 | remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data) | |
1379 | { | |
1380 | tree then_clause, else_clause, cond; | |
1381 | bool save_has_label, then_has_label, else_has_label; | |
1382 | ||
1383 | save_has_label = data->has_label; | |
1384 | data->has_label = false; | |
1385 | data->last_goto = NULL; | |
1386 | ||
1387 | remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data); | |
1388 | ||
1389 | then_has_label = data->has_label; | |
1390 | data->has_label = false; | |
1391 | data->last_goto = NULL; | |
1392 | ||
1393 | remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data); | |
1394 | ||
1395 | else_has_label = data->has_label; | |
1396 | data->has_label = save_has_label | then_has_label | else_has_label; | |
1397 | ||
6de9cd9a DN |
1398 | then_clause = COND_EXPR_THEN (*stmt_p); |
1399 | else_clause = COND_EXPR_ELSE (*stmt_p); | |
18faa5da | 1400 | cond = fold (COND_EXPR_COND (*stmt_p)); |
6de9cd9a DN |
1401 | |
1402 | /* If neither arm does anything at all, we can remove the whole IF. */ | |
1403 | if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause)) | |
1404 | { | |
1405 | *stmt_p = build_empty_stmt (); | |
1406 | data->repeat = true; | |
1407 | } | |
1408 | ||
1409 | /* If there are no reachable statements in an arm, then we can | |
1410 | zap the entire conditional. */ | |
1411 | else if (integer_nonzerop (cond) && !else_has_label) | |
1412 | { | |
1413 | if (warn_notreached) | |
1414 | remove_useless_stmts_warn_notreached (else_clause); | |
1415 | *stmt_p = then_clause; | |
1416 | data->repeat = true; | |
1417 | } | |
1418 | else if (integer_zerop (cond) && !then_has_label) | |
1419 | { | |
1420 | if (warn_notreached) | |
1421 | remove_useless_stmts_warn_notreached (then_clause); | |
1422 | *stmt_p = else_clause; | |
1423 | data->repeat = true; | |
1424 | } | |
1425 | ||
1426 | /* Check a couple of simple things on then/else with single stmts. */ | |
1427 | else | |
1428 | { | |
1429 | tree then_stmt = expr_only (then_clause); | |
1430 | tree else_stmt = expr_only (else_clause); | |
1431 | ||
1432 | /* Notice branches to a common destination. */ | |
1433 | if (then_stmt && else_stmt | |
1434 | && TREE_CODE (then_stmt) == GOTO_EXPR | |
1435 | && TREE_CODE (else_stmt) == GOTO_EXPR | |
1436 | && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt))) | |
1437 | { | |
1438 | *stmt_p = then_stmt; | |
1439 | data->repeat = true; | |
1440 | } | |
1441 | ||
1442 | /* If the THEN/ELSE clause merely assigns a value to a variable or | |
1443 | parameter which is already known to contain that value, then | |
1444 | remove the useless THEN/ELSE clause. */ | |
1445 | else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL) | |
1446 | { | |
1447 | if (else_stmt | |
1448 | && TREE_CODE (else_stmt) == MODIFY_EXPR | |
1449 | && TREE_OPERAND (else_stmt, 0) == cond | |
1450 | && integer_zerop (TREE_OPERAND (else_stmt, 1))) | |
1451 | COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list (); | |
1452 | } | |
1453 | else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR) | |
1454 | && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL | |
1455 | || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL) | |
1456 | && TREE_CONSTANT (TREE_OPERAND (cond, 1))) | |
1457 | { | |
1458 | tree stmt = (TREE_CODE (cond) == EQ_EXPR | |
1459 | ? then_stmt : else_stmt); | |
1460 | tree *location = (TREE_CODE (cond) == EQ_EXPR | |
1461 | ? &COND_EXPR_THEN (*stmt_p) | |
1462 | : &COND_EXPR_ELSE (*stmt_p)); | |
1463 | ||
1464 | if (stmt | |
1465 | && TREE_CODE (stmt) == MODIFY_EXPR | |
1466 | && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0) | |
1467 | && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1)) | |
1468 | *location = alloc_stmt_list (); | |
1469 | } | |
1470 | } | |
1471 | ||
1472 | /* Protect GOTOs in the arm of COND_EXPRs from being removed. They | |
1473 | would be re-introduced during lowering. */ | |
1474 | data->last_goto = NULL; | |
1475 | } | |
1476 | ||
1477 | ||
1478 | static void | |
1479 | remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data) | |
1480 | { | |
1481 | bool save_may_branch, save_may_throw; | |
1482 | bool this_may_branch, this_may_throw; | |
1483 | ||
1484 | /* Collect may_branch and may_throw information for the body only. */ | |
1485 | save_may_branch = data->may_branch; | |
1486 | save_may_throw = data->may_throw; | |
1487 | data->may_branch = false; | |
1488 | data->may_throw = false; | |
1489 | data->last_goto = NULL; | |
1490 | ||
1491 | remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data); | |
1492 | ||
1493 | this_may_branch = data->may_branch; | |
1494 | this_may_throw = data->may_throw; | |
1495 | data->may_branch |= save_may_branch; | |
1496 | data->may_throw |= save_may_throw; | |
1497 | data->last_goto = NULL; | |
1498 | ||
1499 | remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data); | |
1500 | ||
1501 | /* If the body is empty, then we can emit the FINALLY block without | |
1502 | the enclosing TRY_FINALLY_EXPR. */ | |
1503 | if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0))) | |
1504 | { | |
1505 | *stmt_p = TREE_OPERAND (*stmt_p, 1); | |
1506 | data->repeat = true; | |
1507 | } | |
1508 | ||
1509 | /* If the handler is empty, then we can emit the TRY block without | |
1510 | the enclosing TRY_FINALLY_EXPR. */ | |
1511 | else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1))) | |
1512 | { | |
1513 | *stmt_p = TREE_OPERAND (*stmt_p, 0); | |
1514 | data->repeat = true; | |
1515 | } | |
1516 | ||
1517 | /* If the body neither throws, nor branches, then we can safely | |
1518 | string the TRY and FINALLY blocks together. */ | |
1519 | else if (!this_may_branch && !this_may_throw) | |
1520 | { | |
1521 | tree stmt = *stmt_p; | |
1522 | *stmt_p = TREE_OPERAND (stmt, 0); | |
1523 | append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p); | |
1524 | data->repeat = true; | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | ||
1529 | static void | |
1530 | remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data) | |
1531 | { | |
1532 | bool save_may_throw, this_may_throw; | |
1533 | tree_stmt_iterator i; | |
1534 | tree stmt; | |
1535 | ||
1536 | /* Collect may_throw information for the body only. */ | |
1537 | save_may_throw = data->may_throw; | |
1538 | data->may_throw = false; | |
1539 | data->last_goto = NULL; | |
1540 | ||
1541 | remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data); | |
1542 | ||
1543 | this_may_throw = data->may_throw; | |
1544 | data->may_throw = save_may_throw; | |
1545 | ||
1546 | /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */ | |
1547 | if (!this_may_throw) | |
1548 | { | |
1549 | if (warn_notreached) | |
1550 | remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1)); | |
1551 | *stmt_p = TREE_OPERAND (*stmt_p, 0); | |
1552 | data->repeat = true; | |
1553 | return; | |
1554 | } | |
1555 | ||
1556 | /* Process the catch clause specially. We may be able to tell that | |
1557 | no exceptions propagate past this point. */ | |
1558 | ||
1559 | this_may_throw = true; | |
1560 | i = tsi_start (TREE_OPERAND (*stmt_p, 1)); | |
1561 | stmt = tsi_stmt (i); | |
1562 | data->last_goto = NULL; | |
1563 | ||
1564 | switch (TREE_CODE (stmt)) | |
1565 | { | |
1566 | case CATCH_EXPR: | |
1567 | for (; !tsi_end_p (i); tsi_next (&i)) | |
1568 | { | |
1569 | stmt = tsi_stmt (i); | |
1570 | /* If we catch all exceptions, then the body does not | |
1571 | propagate exceptions past this point. */ | |
1572 | if (CATCH_TYPES (stmt) == NULL) | |
1573 | this_may_throw = false; | |
1574 | data->last_goto = NULL; | |
1575 | remove_useless_stmts_1 (&CATCH_BODY (stmt), data); | |
1576 | } | |
1577 | break; | |
1578 | ||
1579 | case EH_FILTER_EXPR: | |
1580 | if (EH_FILTER_MUST_NOT_THROW (stmt)) | |
1581 | this_may_throw = false; | |
1582 | else if (EH_FILTER_TYPES (stmt) == NULL) | |
1583 | this_may_throw = false; | |
1584 | remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data); | |
1585 | break; | |
1586 | ||
1587 | default: | |
1588 | /* Otherwise this is a cleanup. */ | |
1589 | remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data); | |
1590 | ||
1591 | /* If the cleanup is empty, then we can emit the TRY block without | |
1592 | the enclosing TRY_CATCH_EXPR. */ | |
1593 | if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1))) | |
1594 | { | |
1595 | *stmt_p = TREE_OPERAND (*stmt_p, 0); | |
1596 | data->repeat = true; | |
1597 | } | |
1598 | break; | |
1599 | } | |
1600 | data->may_throw |= this_may_throw; | |
1601 | } | |
1602 | ||
1603 | ||
1604 | static void | |
1605 | remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data) | |
1606 | { | |
1607 | tree block; | |
1608 | ||
1609 | /* First remove anything underneath the BIND_EXPR. */ | |
1610 | remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data); | |
1611 | ||
1612 | /* If the BIND_EXPR has no variables, then we can pull everything | |
1613 | up one level and remove the BIND_EXPR, unless this is the toplevel | |
1614 | BIND_EXPR for the current function or an inlined function. | |
1615 | ||
1616 | When this situation occurs we will want to apply this | |
1617 | optimization again. */ | |
1618 | block = BIND_EXPR_BLOCK (*stmt_p); | |
1619 | if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE | |
1620 | && *stmt_p != DECL_SAVED_TREE (current_function_decl) | |
1621 | && (! block | |
1622 | || ! BLOCK_ABSTRACT_ORIGIN (block) | |
1623 | || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) | |
1624 | != FUNCTION_DECL))) | |
1625 | { | |
1626 | *stmt_p = BIND_EXPR_BODY (*stmt_p); | |
1627 | data->repeat = true; | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | ||
1632 | static void | |
1633 | remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data) | |
1634 | { | |
1635 | tree dest = GOTO_DESTINATION (*stmt_p); | |
1636 | ||
1637 | data->may_branch = true; | |
1638 | data->last_goto = NULL; | |
1639 | ||
1640 | /* Record the last goto expr, so that we can delete it if unnecessary. */ | |
1641 | if (TREE_CODE (dest) == LABEL_DECL) | |
1642 | data->last_goto = stmt_p; | |
1643 | } | |
1644 | ||
1645 | ||
1646 | static void | |
1647 | remove_useless_stmts_label (tree *stmt_p, struct rus_data *data) | |
1648 | { | |
1649 | tree label = LABEL_EXPR_LABEL (*stmt_p); | |
1650 | ||
1651 | data->has_label = true; | |
1652 | ||
1653 | /* We do want to jump across non-local label receiver code. */ | |
1654 | if (DECL_NONLOCAL (label)) | |
1655 | data->last_goto = NULL; | |
1656 | ||
1657 | else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label) | |
1658 | { | |
1659 | *data->last_goto = build_empty_stmt (); | |
1660 | data->repeat = true; | |
1661 | } | |
1662 | ||
1663 | /* ??? Add something here to delete unused labels. */ | |
1664 | } | |
1665 | ||
1666 | ||
1667 | /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its | |
1668 | decl. This allows us to eliminate redundant or useless | |
1669 | calls to "const" functions. | |
1670 | ||
1671 | Gimplifier already does the same operation, but we may notice functions | |
1672 | being const and pure once their calls has been gimplified, so we need | |
1673 | to update the flag. */ | |
1674 | ||
1675 | static void | |
1676 | update_call_expr_flags (tree call) | |
1677 | { | |
1678 | tree decl = get_callee_fndecl (call); | |
1679 | if (!decl) | |
1680 | return; | |
1681 | if (call_expr_flags (call) & (ECF_CONST | ECF_PURE)) | |
1682 | TREE_SIDE_EFFECTS (call) = 0; | |
1683 | if (TREE_NOTHROW (decl)) | |
1684 | TREE_NOTHROW (call) = 1; | |
1685 | } | |
1686 | ||
1687 | ||
1688 | /* T is CALL_EXPR. Set current_function_calls_* flags. */ | |
1689 | ||
1690 | void | |
1691 | notice_special_calls (tree t) | |
1692 | { | |
1693 | int flags = call_expr_flags (t); | |
1694 | ||
1695 | if (flags & ECF_MAY_BE_ALLOCA) | |
1696 | current_function_calls_alloca = true; | |
1697 | if (flags & ECF_RETURNS_TWICE) | |
1698 | current_function_calls_setjmp = true; | |
1699 | } | |
1700 | ||
1701 | ||
1702 | /* Clear flags set by notice_special_calls. Used by dead code removal | |
1703 | to update the flags. */ | |
1704 | ||
1705 | void | |
1706 | clear_special_calls (void) | |
1707 | { | |
1708 | current_function_calls_alloca = false; | |
1709 | current_function_calls_setjmp = false; | |
1710 | } | |
1711 | ||
1712 | ||
1713 | static void | |
1714 | remove_useless_stmts_1 (tree *tp, struct rus_data *data) | |
1715 | { | |
cd709752 | 1716 | tree t = *tp, op; |
6de9cd9a DN |
1717 | |
1718 | switch (TREE_CODE (t)) | |
1719 | { | |
1720 | case COND_EXPR: | |
1721 | remove_useless_stmts_cond (tp, data); | |
1722 | break; | |
1723 | ||
1724 | case TRY_FINALLY_EXPR: | |
1725 | remove_useless_stmts_tf (tp, data); | |
1726 | break; | |
1727 | ||
1728 | case TRY_CATCH_EXPR: | |
1729 | remove_useless_stmts_tc (tp, data); | |
1730 | break; | |
1731 | ||
1732 | case BIND_EXPR: | |
1733 | remove_useless_stmts_bind (tp, data); | |
1734 | break; | |
1735 | ||
1736 | case GOTO_EXPR: | |
1737 | remove_useless_stmts_goto (tp, data); | |
1738 | break; | |
1739 | ||
1740 | case LABEL_EXPR: | |
1741 | remove_useless_stmts_label (tp, data); | |
1742 | break; | |
1743 | ||
1744 | case RETURN_EXPR: | |
53e782e5 | 1745 | fold_stmt (tp); |
6de9cd9a DN |
1746 | data->last_goto = NULL; |
1747 | data->may_branch = true; | |
1748 | break; | |
1749 | ||
1750 | case CALL_EXPR: | |
53e782e5 | 1751 | fold_stmt (tp); |
6de9cd9a DN |
1752 | data->last_goto = NULL; |
1753 | notice_special_calls (t); | |
1754 | update_call_expr_flags (t); | |
1755 | if (tree_could_throw_p (t)) | |
1756 | data->may_throw = true; | |
1757 | break; | |
1758 | ||
1759 | case MODIFY_EXPR: | |
1760 | data->last_goto = NULL; | |
53e782e5 | 1761 | fold_stmt (tp); |
cd709752 RH |
1762 | op = get_call_expr_in (t); |
1763 | if (op) | |
6de9cd9a | 1764 | { |
cd709752 RH |
1765 | update_call_expr_flags (op); |
1766 | notice_special_calls (op); | |
6de9cd9a DN |
1767 | } |
1768 | if (tree_could_throw_p (t)) | |
1769 | data->may_throw = true; | |
1770 | break; | |
1771 | ||
1772 | case STATEMENT_LIST: | |
1773 | { | |
1774 | tree_stmt_iterator i = tsi_start (t); | |
1775 | while (!tsi_end_p (i)) | |
1776 | { | |
1777 | t = tsi_stmt (i); | |
1778 | if (IS_EMPTY_STMT (t)) | |
1779 | { | |
1780 | tsi_delink (&i); | |
1781 | continue; | |
1782 | } | |
1783 | ||
1784 | remove_useless_stmts_1 (tsi_stmt_ptr (i), data); | |
1785 | ||
1786 | t = tsi_stmt (i); | |
1787 | if (TREE_CODE (t) == STATEMENT_LIST) | |
1788 | { | |
1789 | tsi_link_before (&i, t, TSI_SAME_STMT); | |
1790 | tsi_delink (&i); | |
1791 | } | |
1792 | else | |
1793 | tsi_next (&i); | |
1794 | } | |
1795 | } | |
1796 | break; | |
8e14584d | 1797 | case ASM_EXPR: |
53e782e5 AP |
1798 | fold_stmt (tp); |
1799 | data->last_goto = NULL; | |
1800 | break; | |
6de9cd9a DN |
1801 | |
1802 | default: | |
1803 | data->last_goto = NULL; | |
1804 | break; | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | static void | |
1809 | remove_useless_stmts (void) | |
1810 | { | |
1811 | struct rus_data data; | |
1812 | ||
1813 | clear_special_calls (); | |
1814 | ||
1815 | do | |
1816 | { | |
1817 | memset (&data, 0, sizeof (data)); | |
1818 | remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data); | |
1819 | } | |
1820 | while (data.repeat); | |
1821 | } | |
1822 | ||
1823 | ||
1824 | struct tree_opt_pass pass_remove_useless_stmts = | |
1825 | { | |
1826 | "useless", /* name */ | |
1827 | NULL, /* gate */ | |
1828 | remove_useless_stmts, /* execute */ | |
1829 | NULL, /* sub */ | |
1830 | NULL, /* next */ | |
1831 | 0, /* static_pass_number */ | |
1832 | 0, /* tv_id */ | |
1833 | PROP_gimple_any, /* properties_required */ | |
1834 | 0, /* properties_provided */ | |
1835 | 0, /* properties_destroyed */ | |
1836 | 0, /* todo_flags_start */ | |
9f8628ba PB |
1837 | TODO_dump_func, /* todo_flags_finish */ |
1838 | 0 /* letter */ | |
6de9cd9a DN |
1839 | }; |
1840 | ||
1841 | ||
1842 | /* Remove obviously useless statements in basic block BB. */ | |
1843 | ||
1844 | static void | |
1845 | cfg_remove_useless_stmts_bb (basic_block bb) | |
1846 | { | |
1847 | block_stmt_iterator bsi; | |
1848 | tree stmt = NULL_TREE; | |
1849 | tree cond, var = NULL_TREE, val = NULL_TREE; | |
1850 | struct var_ann_d *ann; | |
1851 | ||
1852 | /* Check whether we come here from a condition, and if so, get the | |
1853 | condition. */ | |
628f6a4e BE |
1854 | if (EDGE_COUNT (bb->preds) != 1 |
1855 | || !(EDGE_PRED (bb, 0)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) | |
6de9cd9a DN |
1856 | return; |
1857 | ||
628f6a4e | 1858 | cond = COND_EXPR_COND (last_stmt (EDGE_PRED (bb, 0)->src)); |
6de9cd9a DN |
1859 | |
1860 | if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL) | |
1861 | { | |
1862 | var = cond; | |
628f6a4e | 1863 | val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE |
6de9cd9a DN |
1864 | ? boolean_false_node : boolean_true_node); |
1865 | } | |
1866 | else if (TREE_CODE (cond) == TRUTH_NOT_EXPR | |
1867 | && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL | |
1868 | || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)) | |
1869 | { | |
1870 | var = TREE_OPERAND (cond, 0); | |
628f6a4e | 1871 | val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE |
6de9cd9a DN |
1872 | ? boolean_true_node : boolean_false_node); |
1873 | } | |
1874 | else | |
1875 | { | |
628f6a4e | 1876 | if (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE) |
6de9cd9a DN |
1877 | cond = invert_truthvalue (cond); |
1878 | if (TREE_CODE (cond) == EQ_EXPR | |
1879 | && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL | |
1880 | || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL) | |
1881 | && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL | |
1882 | || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL | |
1883 | || TREE_CONSTANT (TREE_OPERAND (cond, 1)))) | |
1884 | { | |
1885 | var = TREE_OPERAND (cond, 0); | |
1886 | val = TREE_OPERAND (cond, 1); | |
1887 | } | |
1888 | else | |
1889 | return; | |
1890 | } | |
1891 | ||
1892 | /* Only work for normal local variables. */ | |
1893 | ann = var_ann (var); | |
1894 | if (!ann | |
1895 | || ann->may_aliases | |
1896 | || TREE_ADDRESSABLE (var)) | |
1897 | return; | |
1898 | ||
1899 | if (! TREE_CONSTANT (val)) | |
1900 | { | |
1901 | ann = var_ann (val); | |
1902 | if (!ann | |
1903 | || ann->may_aliases | |
1904 | || TREE_ADDRESSABLE (val)) | |
1905 | return; | |
1906 | } | |
1907 | ||
1908 | /* Ignore floating point variables, since comparison behaves weird for | |
1909 | them. */ | |
1910 | if (FLOAT_TYPE_P (TREE_TYPE (var))) | |
1911 | return; | |
1912 | ||
1913 | for (bsi = bsi_start (bb); !bsi_end_p (bsi);) | |
1914 | { | |
1915 | stmt = bsi_stmt (bsi); | |
1916 | ||
1917 | /* If the THEN/ELSE clause merely assigns a value to a variable/parameter | |
1918 | which is already known to contain that value, then remove the useless | |
1919 | THEN/ELSE clause. */ | |
1920 | if (TREE_CODE (stmt) == MODIFY_EXPR | |
1921 | && TREE_OPERAND (stmt, 0) == var | |
1922 | && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0)) | |
1923 | { | |
1924 | bsi_remove (&bsi); | |
1925 | continue; | |
1926 | } | |
1927 | ||
631b67ce RK |
1928 | /* Invalidate the var if we encounter something that could modify it. |
1929 | Likewise for the value it was previously set to. Note that we only | |
1930 | consider values that are either a VAR_DECL or PARM_DECL so we | |
1931 | can test for conflict very simply. */ | |
6de9cd9a | 1932 | if (TREE_CODE (stmt) == ASM_EXPR |
6de9cd9a | 1933 | || (TREE_CODE (stmt) == MODIFY_EXPR |
631b67ce RK |
1934 | && (TREE_OPERAND (stmt, 0) == var |
1935 | || TREE_OPERAND (stmt, 0) == val))) | |
6de9cd9a DN |
1936 | return; |
1937 | ||
1938 | bsi_next (&bsi); | |
1939 | } | |
1940 | } | |
1941 | ||
1942 | ||
1943 | /* A CFG-aware version of remove_useless_stmts. */ | |
1944 | ||
1945 | void | |
1946 | cfg_remove_useless_stmts (void) | |
1947 | { | |
1948 | basic_block bb; | |
1949 | ||
1950 | #ifdef ENABLE_CHECKING | |
1951 | verify_flow_info (); | |
1952 | #endif | |
1953 | ||
1954 | FOR_EACH_BB (bb) | |
1955 | { | |
1956 | cfg_remove_useless_stmts_bb (bb); | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | ||
1961 | /* Remove PHI nodes associated with basic block BB and all edges out of BB. */ | |
1962 | ||
1963 | static void | |
1964 | remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb) | |
1965 | { | |
1966 | tree phi; | |
1967 | ||
1968 | /* Since this block is no longer reachable, we can just delete all | |
1969 | of its PHI nodes. */ | |
1970 | phi = phi_nodes (bb); | |
1971 | while (phi) | |
1972 | { | |
17192884 | 1973 | tree next = PHI_CHAIN (phi); |
6de9cd9a DN |
1974 | remove_phi_node (phi, NULL_TREE, bb); |
1975 | phi = next; | |
1976 | } | |
1977 | ||
1978 | /* Remove edges to BB's successors. */ | |
628f6a4e | 1979 | while (EDGE_COUNT (bb->succs) > 0) |
d0d2cc21 | 1980 | remove_edge (EDGE_SUCC (bb, 0)); |
6de9cd9a DN |
1981 | } |
1982 | ||
1983 | ||
1984 | /* Remove statements of basic block BB. */ | |
1985 | ||
1986 | static void | |
1987 | remove_bb (basic_block bb) | |
1988 | { | |
1989 | block_stmt_iterator i; | |
9506ac2b | 1990 | source_locus loc = 0; |
6de9cd9a DN |
1991 | |
1992 | if (dump_file) | |
1993 | { | |
1994 | fprintf (dump_file, "Removing basic block %d\n", bb->index); | |
1995 | if (dump_flags & TDF_DETAILS) | |
1996 | { | |
1997 | dump_bb (bb, dump_file, 0); | |
1998 | fprintf (dump_file, "\n"); | |
1999 | } | |
2000 | } | |
2001 | ||
2002 | /* Remove all the instructions in the block. */ | |
77568960 | 2003 | for (i = bsi_start (bb); !bsi_end_p (i);) |
6de9cd9a DN |
2004 | { |
2005 | tree stmt = bsi_stmt (i); | |
77568960 AP |
2006 | if (TREE_CODE (stmt) == LABEL_EXPR |
2007 | && FORCED_LABEL (LABEL_EXPR_LABEL (stmt))) | |
2008 | { | |
2009 | basic_block new_bb = bb->prev_bb; | |
ef0b4f28 | 2010 | block_stmt_iterator new_bsi = bsi_start (new_bb); |
77568960 AP |
2011 | |
2012 | bsi_remove (&i); | |
ef0b4f28 | 2013 | bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT); |
77568960 AP |
2014 | } |
2015 | else | |
2016 | { | |
2017 | release_defs (stmt); | |
6de9cd9a | 2018 | |
77568960 AP |
2019 | set_bb_for_stmt (stmt, NULL); |
2020 | bsi_remove (&i); | |
2021 | } | |
6de9cd9a DN |
2022 | |
2023 | /* Don't warn for removed gotos. Gotos are often removed due to | |
2024 | jump threading, thus resulting in bogus warnings. Not great, | |
2025 | since this way we lose warnings for gotos in the original | |
2026 | program that are indeed unreachable. */ | |
9506ac2b | 2027 | if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc) |
43e05e45 SB |
2028 | { |
2029 | source_locus t; | |
2030 | ||
9506ac2b | 2031 | #ifdef USE_MAPPED_LOCATION |
43e05e45 | 2032 | t = EXPR_LOCATION (stmt); |
9506ac2b | 2033 | #else |
43e05e45 | 2034 | t = EXPR_LOCUS (stmt); |
9506ac2b | 2035 | #endif |
43e05e45 SB |
2036 | if (t && LOCATION_LINE (*t) > 0) |
2037 | loc = t; | |
2038 | } | |
6de9cd9a DN |
2039 | } |
2040 | ||
2041 | /* If requested, give a warning that the first statement in the | |
2042 | block is unreachable. We walk statements backwards in the | |
2043 | loop above, so the last statement we process is the first statement | |
2044 | in the block. */ | |
2045 | if (warn_notreached && loc) | |
9506ac2b PB |
2046 | #ifdef USE_MAPPED_LOCATION |
2047 | warning ("%Hwill never be executed", &loc); | |
2048 | #else | |
6de9cd9a | 2049 | warning ("%Hwill never be executed", loc); |
9506ac2b | 2050 | #endif |
6de9cd9a DN |
2051 | |
2052 | remove_phi_nodes_and_edges_for_unreachable_block (bb); | |
2053 | } | |
2054 | ||
7ded35b4 RS |
2055 | /* A list of all the noreturn calls passed to modify_stmt. |
2056 | cleanup_control_flow uses it to detect cases where a mid-block | |
2057 | indirect call has been turned into a noreturn call. When this | |
2058 | happens, all the instructions after the call are no longer | |
2059 | reachable and must be deleted as dead. */ | |
2060 | ||
2061 | VEC(tree) *modified_noreturn_calls; | |
2062 | ||
6de9cd9a DN |
2063 | /* Try to remove superfluous control structures. */ |
2064 | ||
2065 | static bool | |
2066 | cleanup_control_flow (void) | |
2067 | { | |
2068 | basic_block bb; | |
2069 | block_stmt_iterator bsi; | |
2070 | bool retval = false; | |
7ded35b4 RS |
2071 | tree stmt; |
2072 | ||
2073 | /* Detect cases where a mid-block call is now known not to return. */ | |
2074 | while (VEC_length (tree, modified_noreturn_calls)) | |
2075 | { | |
2076 | stmt = VEC_pop (tree, modified_noreturn_calls); | |
2077 | bb = bb_for_stmt (stmt); | |
2078 | if (bb != NULL && last_stmt (bb) != stmt && noreturn_call_p (stmt)) | |
2079 | split_block (bb, stmt); | |
2080 | } | |
6de9cd9a DN |
2081 | |
2082 | FOR_EACH_BB (bb) | |
2083 | { | |
2084 | bsi = bsi_last (bb); | |
2085 | ||
2086 | if (bsi_end_p (bsi)) | |
2087 | continue; | |
2088 | ||
2089 | stmt = bsi_stmt (bsi); | |
2090 | if (TREE_CODE (stmt) == COND_EXPR | |
2091 | || TREE_CODE (stmt) == SWITCH_EXPR) | |
2092 | retval |= cleanup_control_expr_graph (bb, bsi); | |
d7f3fc19 RS |
2093 | |
2094 | /* Check for indirect calls that have been turned into | |
2095 | noreturn calls. */ | |
7ded35b4 | 2096 | if (noreturn_call_p (stmt) && remove_fallthru_edge (bb->succs)) |
d7f3fc19 RS |
2097 | { |
2098 | free_dominance_info (CDI_DOMINATORS); | |
2099 | retval = true; | |
2100 | } | |
6de9cd9a DN |
2101 | } |
2102 | return retval; | |
2103 | } | |
2104 | ||
2105 | ||
2106 | /* Disconnect an unreachable block in the control expression starting | |
2107 | at block BB. */ | |
2108 | ||
2109 | static bool | |
2110 | cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi) | |
2111 | { | |
2112 | edge taken_edge; | |
2113 | bool retval = false; | |
2114 | tree expr = bsi_stmt (bsi), val; | |
2115 | ||
628f6a4e | 2116 | if (EDGE_COUNT (bb->succs) > 1) |
6de9cd9a | 2117 | { |
628f6a4e BE |
2118 | edge e; |
2119 | edge_iterator ei; | |
6de9cd9a DN |
2120 | |
2121 | switch (TREE_CODE (expr)) | |
2122 | { | |
2123 | case COND_EXPR: | |
2124 | val = COND_EXPR_COND (expr); | |
2125 | break; | |
2126 | ||
2127 | case SWITCH_EXPR: | |
2128 | val = SWITCH_COND (expr); | |
2129 | if (TREE_CODE (val) != INTEGER_CST) | |
2130 | return false; | |
2131 | break; | |
2132 | ||
2133 | default: | |
1e128c5f | 2134 | gcc_unreachable (); |
6de9cd9a DN |
2135 | } |
2136 | ||
2137 | taken_edge = find_taken_edge (bb, val); | |
2138 | if (!taken_edge) | |
2139 | return false; | |
2140 | ||
2141 | /* Remove all the edges except the one that is always executed. */ | |
628f6a4e | 2142 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
6de9cd9a | 2143 | { |
6de9cd9a DN |
2144 | if (e != taken_edge) |
2145 | { | |
2146 | taken_edge->probability += e->probability; | |
2147 | taken_edge->count += e->count; | |
d0d2cc21 | 2148 | remove_edge (e); |
6de9cd9a DN |
2149 | retval = true; |
2150 | } | |
628f6a4e BE |
2151 | else |
2152 | ei_next (&ei); | |
6de9cd9a DN |
2153 | } |
2154 | if (taken_edge->probability > REG_BR_PROB_BASE) | |
2155 | taken_edge->probability = REG_BR_PROB_BASE; | |
2156 | } | |
2157 | else | |
628f6a4e | 2158 | taken_edge = EDGE_SUCC (bb, 0); |
6de9cd9a DN |
2159 | |
2160 | bsi_remove (&bsi); | |
2161 | taken_edge->flags = EDGE_FALLTHRU; | |
2162 | ||
2163 | /* We removed some paths from the cfg. */ | |
fce22de5 | 2164 | free_dominance_info (CDI_DOMINATORS); |
6de9cd9a DN |
2165 | |
2166 | return retval; | |
2167 | } | |
2168 | ||
d7f3fc19 RS |
2169 | /* Remove any fallthru edge from EV. Return true if an edge was removed. */ |
2170 | ||
2171 | static bool | |
2172 | remove_fallthru_edge (VEC(edge) *ev) | |
2173 | { | |
2174 | edge_iterator ei; | |
2175 | edge e; | |
2176 | ||
2177 | FOR_EACH_EDGE (e, ei, ev) | |
2178 | if ((e->flags & EDGE_FALLTHRU) != 0) | |
2179 | { | |
2180 | remove_edge (e); | |
2181 | return true; | |
2182 | } | |
2183 | return false; | |
2184 | } | |
6de9cd9a | 2185 | |
35920270 KH |
2186 | /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a |
2187 | predicate VAL, return the edge that will be taken out of the block. | |
2188 | If VAL does not match a unique edge, NULL is returned. */ | |
6de9cd9a DN |
2189 | |
2190 | edge | |
2191 | find_taken_edge (basic_block bb, tree val) | |
2192 | { | |
2193 | tree stmt; | |
2194 | ||
2195 | stmt = last_stmt (bb); | |
2196 | ||
1e128c5f GB |
2197 | gcc_assert (stmt); |
2198 | gcc_assert (is_ctrl_stmt (stmt)); | |
65f4323d | 2199 | gcc_assert (val); |
6de9cd9a | 2200 | |
255cd731 | 2201 | /* If VAL is a predicate of the form N RELOP N, where N is an |
6a97296a | 2202 | SSA_NAME, we can usually determine its truth value. */ |
65f4323d | 2203 | if (COMPARISON_CLASS_P (val)) |
6a97296a | 2204 | val = fold (val); |
255cd731 | 2205 | |
6de9cd9a DN |
2206 | /* If VAL is not a constant, we can't determine which edge might |
2207 | be taken. */ | |
65f4323d | 2208 | if (!really_constant_p (val)) |
6de9cd9a DN |
2209 | return NULL; |
2210 | ||
2211 | if (TREE_CODE (stmt) == COND_EXPR) | |
2212 | return find_taken_edge_cond_expr (bb, val); | |
2213 | ||
2214 | if (TREE_CODE (stmt) == SWITCH_EXPR) | |
2215 | return find_taken_edge_switch_expr (bb, val); | |
2216 | ||
35920270 | 2217 | gcc_unreachable (); |
6de9cd9a DN |
2218 | } |
2219 | ||
2220 | ||
2221 | /* Given a constant value VAL and the entry block BB to a COND_EXPR | |
2222 | statement, determine which of the two edges will be taken out of the | |
2223 | block. Return NULL if either edge may be taken. */ | |
2224 | ||
2225 | static edge | |
2226 | find_taken_edge_cond_expr (basic_block bb, tree val) | |
2227 | { | |
2228 | edge true_edge, false_edge; | |
2229 | ||
2230 | extract_true_false_edges_from_block (bb, &true_edge, &false_edge); | |
2231 | ||
6de9cd9a DN |
2232 | /* Otherwise, try to determine which branch of the if() will be taken. |
2233 | If VAL is a constant but it can't be reduced to a 0 or a 1, then | |
2234 | we don't really know which edge will be taken at runtime. This | |
2235 | may happen when comparing addresses (e.g., if (&var1 == 4)). */ | |
2236 | if (integer_nonzerop (val)) | |
2237 | return true_edge; | |
2238 | else if (integer_zerop (val)) | |
2239 | return false_edge; | |
2240 | else | |
2241 | return NULL; | |
2242 | } | |
2243 | ||
2244 | ||
2245 | /* Given a constant value VAL and the entry block BB to a SWITCH_EXPR | |
2246 | statement, determine which edge will be taken out of the block. Return | |
2247 | NULL if any edge may be taken. */ | |
2248 | ||
2249 | static edge | |
2250 | find_taken_edge_switch_expr (basic_block bb, tree val) | |
2251 | { | |
2252 | tree switch_expr, taken_case; | |
2253 | basic_block dest_bb; | |
2254 | edge e; | |
2255 | ||
2256 | if (TREE_CODE (val) != INTEGER_CST) | |
2257 | return NULL; | |
2258 | ||
2259 | switch_expr = last_stmt (bb); | |
2260 | taken_case = find_case_label_for_value (switch_expr, val); | |
2261 | dest_bb = label_to_block (CASE_LABEL (taken_case)); | |
2262 | ||
2263 | e = find_edge (bb, dest_bb); | |
1e128c5f | 2264 | gcc_assert (e); |
6de9cd9a DN |
2265 | return e; |
2266 | } | |
2267 | ||
2268 | ||
f667741c SB |
2269 | /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL. |
2270 | We can make optimal use here of the fact that the case labels are | |
2271 | sorted: We can do a binary search for a case matching VAL. */ | |
6de9cd9a DN |
2272 | |
2273 | static tree | |
2274 | find_case_label_for_value (tree switch_expr, tree val) | |
2275 | { | |
2276 | tree vec = SWITCH_LABELS (switch_expr); | |
f667741c SB |
2277 | size_t low, high, n = TREE_VEC_LENGTH (vec); |
2278 | tree default_case = TREE_VEC_ELT (vec, n - 1); | |
6de9cd9a | 2279 | |
f667741c | 2280 | for (low = -1, high = n - 1; high - low > 1; ) |
6de9cd9a | 2281 | { |
f667741c | 2282 | size_t i = (high + low) / 2; |
6de9cd9a | 2283 | tree t = TREE_VEC_ELT (vec, i); |
f667741c SB |
2284 | int cmp; |
2285 | ||
2286 | /* Cache the result of comparing CASE_LOW and val. */ | |
2287 | cmp = tree_int_cst_compare (CASE_LOW (t), val); | |
6de9cd9a | 2288 | |
f667741c SB |
2289 | if (cmp > 0) |
2290 | high = i; | |
2291 | else | |
2292 | low = i; | |
2293 | ||
2294 | if (CASE_HIGH (t) == NULL) | |
6de9cd9a | 2295 | { |
f667741c SB |
2296 | /* A singe-valued case label. */ |
2297 | if (cmp == 0) | |
6de9cd9a DN |
2298 | return t; |
2299 | } | |
2300 | else | |
2301 | { | |
2302 | /* A case range. We can only handle integer ranges. */ | |
f667741c | 2303 | if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0) |
6de9cd9a DN |
2304 | return t; |
2305 | } | |
2306 | } | |
2307 | ||
6de9cd9a DN |
2308 | return default_case; |
2309 | } | |
2310 | ||
2311 | ||
2312 | /* If all the PHI nodes in DEST have alternatives for E1 and E2 and | |
2313 | those alternatives are equal in each of the PHI nodes, then return | |
2314 | true, else return false. */ | |
2315 | ||
2316 | static bool | |
2317 | phi_alternatives_equal (basic_block dest, edge e1, edge e2) | |
2318 | { | |
5dd8841a KH |
2319 | int n1 = e1->dest_idx; |
2320 | int n2 = e2->dest_idx; | |
2321 | tree phi; | |
6de9cd9a | 2322 | |
17192884 | 2323 | for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a | 2324 | { |
5dd8841a KH |
2325 | tree val1 = PHI_ARG_DEF (phi, n1); |
2326 | tree val2 = PHI_ARG_DEF (phi, n2); | |
6de9cd9a | 2327 | |
5dd8841a KH |
2328 | gcc_assert (val1 != NULL_TREE); |
2329 | gcc_assert (val2 != NULL_TREE); | |
6de9cd9a | 2330 | |
cdef8bc6 | 2331 | if (!operand_equal_for_phi_arg_p (val1, val2)) |
6de9cd9a DN |
2332 | return false; |
2333 | } | |
2334 | ||
2335 | return true; | |
2336 | } | |
2337 | ||
2338 | ||
6de9cd9a DN |
2339 | /*--------------------------------------------------------------------------- |
2340 | Debugging functions | |
2341 | ---------------------------------------------------------------------------*/ | |
2342 | ||
2343 | /* Dump tree-specific information of block BB to file OUTF. */ | |
2344 | ||
2345 | void | |
2346 | tree_dump_bb (basic_block bb, FILE *outf, int indent) | |
2347 | { | |
2348 | dump_generic_bb (outf, bb, indent, TDF_VOPS); | |
2349 | } | |
2350 | ||
2351 | ||
2352 | /* Dump a basic block on stderr. */ | |
2353 | ||
2354 | void | |
2355 | debug_tree_bb (basic_block bb) | |
2356 | { | |
2357 | dump_bb (bb, stderr, 0); | |
2358 | } | |
2359 | ||
2360 | ||
2361 | /* Dump basic block with index N on stderr. */ | |
2362 | ||
2363 | basic_block | |
2364 | debug_tree_bb_n (int n) | |
2365 | { | |
2366 | debug_tree_bb (BASIC_BLOCK (n)); | |
2367 | return BASIC_BLOCK (n); | |
2368 | } | |
2369 | ||
2370 | ||
2371 | /* Dump the CFG on stderr. | |
2372 | ||
2373 | FLAGS are the same used by the tree dumping functions | |
2374 | (see TDF_* in tree.h). */ | |
2375 | ||
2376 | void | |
2377 | debug_tree_cfg (int flags) | |
2378 | { | |
2379 | dump_tree_cfg (stderr, flags); | |
2380 | } | |
2381 | ||
2382 | ||
2383 | /* Dump the program showing basic block boundaries on the given FILE. | |
2384 | ||
2385 | FLAGS are the same used by the tree dumping functions (see TDF_* in | |
2386 | tree.h). */ | |
2387 | ||
2388 | void | |
2389 | dump_tree_cfg (FILE *file, int flags) | |
2390 | { | |
2391 | if (flags & TDF_DETAILS) | |
2392 | { | |
2393 | const char *funcname | |
673fda6b | 2394 | = lang_hooks.decl_printable_name (current_function_decl, 2); |
6de9cd9a DN |
2395 | |
2396 | fputc ('\n', file); | |
2397 | fprintf (file, ";; Function %s\n\n", funcname); | |
2398 | fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n", | |
2399 | n_basic_blocks, n_edges, last_basic_block); | |
2400 | ||
2401 | brief_dump_cfg (file); | |
2402 | fprintf (file, "\n"); | |
2403 | } | |
2404 | ||
2405 | if (flags & TDF_STATS) | |
2406 | dump_cfg_stats (file); | |
2407 | ||
2408 | dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS); | |
2409 | } | |
2410 | ||
2411 | ||
2412 | /* Dump CFG statistics on FILE. */ | |
2413 | ||
2414 | void | |
2415 | dump_cfg_stats (FILE *file) | |
2416 | { | |
2417 | static long max_num_merged_labels = 0; | |
2418 | unsigned long size, total = 0; | |
f7fda749 | 2419 | int n_edges; |
6de9cd9a DN |
2420 | basic_block bb; |
2421 | const char * const fmt_str = "%-30s%-13s%12s\n"; | |
f7fda749 | 2422 | const char * const fmt_str_1 = "%-30s%13d%11lu%c\n"; |
6de9cd9a DN |
2423 | const char * const fmt_str_3 = "%-43s%11lu%c\n"; |
2424 | const char *funcname | |
673fda6b | 2425 | = lang_hooks.decl_printable_name (current_function_decl, 2); |
6de9cd9a DN |
2426 | |
2427 | ||
2428 | fprintf (file, "\nCFG Statistics for %s\n\n", funcname); | |
2429 | ||
2430 | fprintf (file, "---------------------------------------------------------\n"); | |
2431 | fprintf (file, fmt_str, "", " Number of ", "Memory"); | |
2432 | fprintf (file, fmt_str, "", " instances ", "used "); | |
2433 | fprintf (file, "---------------------------------------------------------\n"); | |
2434 | ||
2435 | size = n_basic_blocks * sizeof (struct basic_block_def); | |
2436 | total += size; | |
f7fda749 RH |
2437 | fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks, |
2438 | SCALE (size), LABEL (size)); | |
6de9cd9a DN |
2439 | |
2440 | n_edges = 0; | |
2441 | FOR_EACH_BB (bb) | |
628f6a4e | 2442 | n_edges += EDGE_COUNT (bb->succs); |
6de9cd9a DN |
2443 | size = n_edges * sizeof (struct edge_def); |
2444 | total += size; | |
2445 | fprintf (file, fmt_str_1, "Edges", n_edges, SCALE (size), LABEL (size)); | |
2446 | ||
2447 | size = n_basic_blocks * sizeof (struct bb_ann_d); | |
2448 | total += size; | |
2449 | fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks, | |
2450 | SCALE (size), LABEL (size)); | |
2451 | ||
2452 | fprintf (file, "---------------------------------------------------------\n"); | |
2453 | fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total), | |
2454 | LABEL (total)); | |
2455 | fprintf (file, "---------------------------------------------------------\n"); | |
2456 | fprintf (file, "\n"); | |
2457 | ||
2458 | if (cfg_stats.num_merged_labels > max_num_merged_labels) | |
2459 | max_num_merged_labels = cfg_stats.num_merged_labels; | |
2460 | ||
2461 | fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n", | |
2462 | cfg_stats.num_merged_labels, max_num_merged_labels); | |
2463 | ||
2464 | fprintf (file, "\n"); | |
2465 | } | |
2466 | ||
2467 | ||
2468 | /* Dump CFG statistics on stderr. Keep extern so that it's always | |
2469 | linked in the final executable. */ | |
2470 | ||
2471 | void | |
2472 | debug_cfg_stats (void) | |
2473 | { | |
2474 | dump_cfg_stats (stderr); | |
2475 | } | |
2476 | ||
2477 | ||
2478 | /* Dump the flowgraph to a .vcg FILE. */ | |
2479 | ||
2480 | static void | |
2481 | tree_cfg2vcg (FILE *file) | |
2482 | { | |
2483 | edge e; | |
628f6a4e | 2484 | edge_iterator ei; |
6de9cd9a DN |
2485 | basic_block bb; |
2486 | const char *funcname | |
673fda6b | 2487 | = lang_hooks.decl_printable_name (current_function_decl, 2); |
6de9cd9a DN |
2488 | |
2489 | /* Write the file header. */ | |
2490 | fprintf (file, "graph: { title: \"%s\"\n", funcname); | |
2491 | fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n"); | |
2492 | fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n"); | |
2493 | ||
2494 | /* Write blocks and edges. */ | |
628f6a4e | 2495 | FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs) |
6de9cd9a DN |
2496 | { |
2497 | fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"", | |
2498 | e->dest->index); | |
2499 | ||
2500 | if (e->flags & EDGE_FAKE) | |
2501 | fprintf (file, " linestyle: dotted priority: 10"); | |
2502 | else | |
2503 | fprintf (file, " linestyle: solid priority: 100"); | |
2504 | ||
2505 | fprintf (file, " }\n"); | |
2506 | } | |
2507 | fputc ('\n', file); | |
2508 | ||
2509 | FOR_EACH_BB (bb) | |
2510 | { | |
2511 | enum tree_code head_code, end_code; | |
2512 | const char *head_name, *end_name; | |
2513 | int head_line = 0; | |
2514 | int end_line = 0; | |
2515 | tree first = first_stmt (bb); | |
2516 | tree last = last_stmt (bb); | |
2517 | ||
2518 | if (first) | |
2519 | { | |
2520 | head_code = TREE_CODE (first); | |
2521 | head_name = tree_code_name[head_code]; | |
2522 | head_line = get_lineno (first); | |
2523 | } | |
2524 | else | |
2525 | head_name = "no-statement"; | |
2526 | ||
2527 | if (last) | |
2528 | { | |
2529 | end_code = TREE_CODE (last); | |
2530 | end_name = tree_code_name[end_code]; | |
2531 | end_line = get_lineno (last); | |
2532 | } | |
2533 | else | |
2534 | end_name = "no-statement"; | |
2535 | ||
2536 | fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n", | |
2537 | bb->index, bb->index, head_name, head_line, end_name, | |
2538 | end_line); | |
2539 | ||
628f6a4e | 2540 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
2541 | { |
2542 | if (e->dest == EXIT_BLOCK_PTR) | |
2543 | fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index); | |
2544 | else | |
2545 | fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index); | |
2546 | ||
2547 | if (e->flags & EDGE_FAKE) | |
2548 | fprintf (file, " priority: 10 linestyle: dotted"); | |
2549 | else | |
2550 | fprintf (file, " priority: 100 linestyle: solid"); | |
2551 | ||
2552 | fprintf (file, " }\n"); | |
2553 | } | |
2554 | ||
2555 | if (bb->next_bb != EXIT_BLOCK_PTR) | |
2556 | fputc ('\n', file); | |
2557 | } | |
2558 | ||
2559 | fputs ("}\n\n", file); | |
2560 | } | |
2561 | ||
2562 | ||
2563 | ||
2564 | /*--------------------------------------------------------------------------- | |
2565 | Miscellaneous helpers | |
2566 | ---------------------------------------------------------------------------*/ | |
2567 | ||
2568 | /* Return true if T represents a stmt that always transfers control. */ | |
2569 | ||
2570 | bool | |
2571 | is_ctrl_stmt (tree t) | |
2572 | { | |
2573 | return (TREE_CODE (t) == COND_EXPR | |
2574 | || TREE_CODE (t) == SWITCH_EXPR | |
2575 | || TREE_CODE (t) == GOTO_EXPR | |
2576 | || TREE_CODE (t) == RETURN_EXPR | |
2577 | || TREE_CODE (t) == RESX_EXPR); | |
2578 | } | |
2579 | ||
2580 | ||
2581 | /* Return true if T is a statement that may alter the flow of control | |
2582 | (e.g., a call to a non-returning function). */ | |
2583 | ||
2584 | bool | |
2585 | is_ctrl_altering_stmt (tree t) | |
2586 | { | |
cd709752 | 2587 | tree call; |
6de9cd9a | 2588 | |
1e128c5f | 2589 | gcc_assert (t); |
cd709752 RH |
2590 | call = get_call_expr_in (t); |
2591 | if (call) | |
6de9cd9a | 2592 | { |
6de9cd9a DN |
2593 | /* A non-pure/const CALL_EXPR alters flow control if the current |
2594 | function has nonlocal labels. */ | |
cd709752 | 2595 | if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label) |
6de9cd9a DN |
2596 | return true; |
2597 | ||
2598 | /* A CALL_EXPR also alters control flow if it does not return. */ | |
6e14af16 | 2599 | if (call_expr_flags (call) & ECF_NORETURN) |
6de9cd9a | 2600 | return true; |
6de9cd9a DN |
2601 | } |
2602 | ||
2603 | /* If a statement can throw, it alters control flow. */ | |
2604 | return tree_can_throw_internal (t); | |
2605 | } | |
2606 | ||
2607 | ||
2608 | /* Return true if T is a computed goto. */ | |
2609 | ||
2610 | bool | |
2611 | computed_goto_p (tree t) | |
2612 | { | |
2613 | return (TREE_CODE (t) == GOTO_EXPR | |
2614 | && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL); | |
2615 | } | |
2616 | ||
2617 | ||
2618 | /* Checks whether EXPR is a simple local goto. */ | |
2619 | ||
2620 | bool | |
2621 | simple_goto_p (tree expr) | |
2622 | { | |
ab8907ef RH |
2623 | return (TREE_CODE (expr) == GOTO_EXPR |
2624 | && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL); | |
6de9cd9a DN |
2625 | } |
2626 | ||
2627 | ||
2628 | /* Return true if T should start a new basic block. PREV_T is the | |
2629 | statement preceding T. It is used when T is a label or a case label. | |
2630 | Labels should only start a new basic block if their previous statement | |
2631 | wasn't a label. Otherwise, sequence of labels would generate | |
2632 | unnecessary basic blocks that only contain a single label. */ | |
2633 | ||
2634 | static inline bool | |
2635 | stmt_starts_bb_p (tree t, tree prev_t) | |
2636 | { | |
2637 | enum tree_code code; | |
2638 | ||
2639 | if (t == NULL_TREE) | |
2640 | return false; | |
2641 | ||
2642 | /* LABEL_EXPRs start a new basic block only if the preceding | |
2643 | statement wasn't a label of the same type. This prevents the | |
2644 | creation of consecutive blocks that have nothing but a single | |
2645 | label. */ | |
2646 | code = TREE_CODE (t); | |
2647 | if (code == LABEL_EXPR) | |
2648 | { | |
2649 | /* Nonlocal and computed GOTO targets always start a new block. */ | |
2650 | if (code == LABEL_EXPR | |
2651 | && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t)) | |
2652 | || FORCED_LABEL (LABEL_EXPR_LABEL (t)))) | |
2653 | return true; | |
2654 | ||
2655 | if (prev_t && TREE_CODE (prev_t) == code) | |
2656 | { | |
2657 | if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t))) | |
2658 | return true; | |
2659 | ||
2660 | cfg_stats.num_merged_labels++; | |
2661 | return false; | |
2662 | } | |
2663 | else | |
2664 | return true; | |
2665 | } | |
2666 | ||
2667 | return false; | |
2668 | } | |
2669 | ||
2670 | ||
2671 | /* Return true if T should end a basic block. */ | |
2672 | ||
2673 | bool | |
2674 | stmt_ends_bb_p (tree t) | |
2675 | { | |
2676 | return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t); | |
2677 | } | |
2678 | ||
2679 | ||
2680 | /* Add gotos that used to be represented implicitly in the CFG. */ | |
2681 | ||
2682 | void | |
2683 | disband_implicit_edges (void) | |
2684 | { | |
2685 | basic_block bb; | |
2686 | block_stmt_iterator last; | |
2687 | edge e; | |
628f6a4e | 2688 | edge_iterator ei; |
eb4e1c01 | 2689 | tree stmt, label; |
6de9cd9a DN |
2690 | |
2691 | FOR_EACH_BB (bb) | |
2692 | { | |
2693 | last = bsi_last (bb); | |
2694 | stmt = last_stmt (bb); | |
2695 | ||
2696 | if (stmt && TREE_CODE (stmt) == COND_EXPR) | |
2697 | { | |
2698 | /* Remove superfluous gotos from COND_EXPR branches. Moved | |
2699 | from cfg_remove_useless_stmts here since it violates the | |
2700 | invariants for tree--cfg correspondence and thus fits better | |
2701 | here where we do it anyway. */ | |
9ff3d2de JL |
2702 | e = find_edge (bb, bb->next_bb); |
2703 | if (e) | |
6de9cd9a | 2704 | { |
6de9cd9a DN |
2705 | if (e->flags & EDGE_TRUE_VALUE) |
2706 | COND_EXPR_THEN (stmt) = build_empty_stmt (); | |
2707 | else if (e->flags & EDGE_FALSE_VALUE) | |
2708 | COND_EXPR_ELSE (stmt) = build_empty_stmt (); | |
2709 | else | |
1e128c5f | 2710 | gcc_unreachable (); |
6de9cd9a DN |
2711 | e->flags |= EDGE_FALLTHRU; |
2712 | } | |
2713 | ||
2714 | continue; | |
2715 | } | |
2716 | ||
2717 | if (stmt && TREE_CODE (stmt) == RETURN_EXPR) | |
2718 | { | |
2719 | /* Remove the RETURN_EXPR if we may fall though to the exit | |
2720 | instead. */ | |
628f6a4e BE |
2721 | gcc_assert (EDGE_COUNT (bb->succs) == 1); |
2722 | gcc_assert (EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR); | |
6de9cd9a DN |
2723 | |
2724 | if (bb->next_bb == EXIT_BLOCK_PTR | |
2725 | && !TREE_OPERAND (stmt, 0)) | |
2726 | { | |
2727 | bsi_remove (&last); | |
628f6a4e | 2728 | EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU; |
6de9cd9a DN |
2729 | } |
2730 | continue; | |
2731 | } | |
2732 | ||
2733 | /* There can be no fallthru edge if the last statement is a control | |
2734 | one. */ | |
2735 | if (stmt && is_ctrl_stmt (stmt)) | |
2736 | continue; | |
2737 | ||
2738 | /* Find a fallthru edge and emit the goto if necessary. */ | |
628f6a4e | 2739 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
2740 | if (e->flags & EDGE_FALLTHRU) |
2741 | break; | |
2742 | ||
62b857ea | 2743 | if (!e || e->dest == bb->next_bb) |
6de9cd9a DN |
2744 | continue; |
2745 | ||
1e128c5f | 2746 | gcc_assert (e->dest != EXIT_BLOCK_PTR); |
6de9cd9a DN |
2747 | label = tree_block_label (e->dest); |
2748 | ||
62b857ea | 2749 | stmt = build1 (GOTO_EXPR, void_type_node, label); |
9506ac2b PB |
2750 | #ifdef USE_MAPPED_LOCATION |
2751 | SET_EXPR_LOCATION (stmt, e->goto_locus); | |
2752 | #else | |
62b857ea | 2753 | SET_EXPR_LOCUS (stmt, e->goto_locus); |
9506ac2b | 2754 | #endif |
62b857ea | 2755 | bsi_insert_after (&last, stmt, BSI_NEW_STMT); |
6de9cd9a DN |
2756 | e->flags &= ~EDGE_FALLTHRU; |
2757 | } | |
2758 | } | |
2759 | ||
242229bb | 2760 | /* Remove block annotations and other datastructures. */ |
6de9cd9a DN |
2761 | |
2762 | void | |
242229bb | 2763 | delete_tree_cfg_annotations (void) |
6de9cd9a | 2764 | { |
242229bb | 2765 | basic_block bb; |
6de9cd9a DN |
2766 | if (n_basic_blocks > 0) |
2767 | free_blocks_annotations (); | |
2768 | ||
6de9cd9a DN |
2769 | label_to_block_map = NULL; |
2770 | free_rbi_pool (); | |
242229bb JH |
2771 | FOR_EACH_BB (bb) |
2772 | bb->rbi = NULL; | |
6de9cd9a DN |
2773 | } |
2774 | ||
2775 | ||
2776 | /* Return the first statement in basic block BB. */ | |
2777 | ||
2778 | tree | |
2779 | first_stmt (basic_block bb) | |
2780 | { | |
2781 | block_stmt_iterator i = bsi_start (bb); | |
2782 | return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE; | |
2783 | } | |
2784 | ||
2785 | ||
2786 | /* Return the last statement in basic block BB. */ | |
2787 | ||
2788 | tree | |
2789 | last_stmt (basic_block bb) | |
2790 | { | |
2791 | block_stmt_iterator b = bsi_last (bb); | |
2792 | return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE; | |
2793 | } | |
2794 | ||
2795 | ||
2796 | /* Return a pointer to the last statement in block BB. */ | |
2797 | ||
2798 | tree * | |
2799 | last_stmt_ptr (basic_block bb) | |
2800 | { | |
2801 | block_stmt_iterator last = bsi_last (bb); | |
2802 | return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL; | |
2803 | } | |
2804 | ||
2805 | ||
2806 | /* Return the last statement of an otherwise empty block. Return NULL | |
2807 | if the block is totally empty, or if it contains more than one | |
2808 | statement. */ | |
2809 | ||
2810 | tree | |
2811 | last_and_only_stmt (basic_block bb) | |
2812 | { | |
2813 | block_stmt_iterator i = bsi_last (bb); | |
2814 | tree last, prev; | |
2815 | ||
2816 | if (bsi_end_p (i)) | |
2817 | return NULL_TREE; | |
2818 | ||
2819 | last = bsi_stmt (i); | |
2820 | bsi_prev (&i); | |
2821 | if (bsi_end_p (i)) | |
2822 | return last; | |
2823 | ||
2824 | /* Empty statements should no longer appear in the instruction stream. | |
2825 | Everything that might have appeared before should be deleted by | |
2826 | remove_useless_stmts, and the optimizers should just bsi_remove | |
2827 | instead of smashing with build_empty_stmt. | |
2828 | ||
2829 | Thus the only thing that should appear here in a block containing | |
2830 | one executable statement is a label. */ | |
2831 | prev = bsi_stmt (i); | |
2832 | if (TREE_CODE (prev) == LABEL_EXPR) | |
2833 | return last; | |
2834 | else | |
2835 | return NULL_TREE; | |
2836 | } | |
2837 | ||
2838 | ||
2839 | /* Mark BB as the basic block holding statement T. */ | |
2840 | ||
2841 | void | |
2842 | set_bb_for_stmt (tree t, basic_block bb) | |
2843 | { | |
30d396e3 ZD |
2844 | if (TREE_CODE (t) == PHI_NODE) |
2845 | PHI_BB (t) = bb; | |
2846 | else if (TREE_CODE (t) == STATEMENT_LIST) | |
6de9cd9a DN |
2847 | { |
2848 | tree_stmt_iterator i; | |
2849 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
2850 | set_bb_for_stmt (tsi_stmt (i), bb); | |
2851 | } | |
2852 | else | |
2853 | { | |
2854 | stmt_ann_t ann = get_stmt_ann (t); | |
2855 | ann->bb = bb; | |
2856 | ||
2857 | /* If the statement is a label, add the label to block-to-labels map | |
2858 | so that we can speed up edge creation for GOTO_EXPRs. */ | |
2859 | if (TREE_CODE (t) == LABEL_EXPR) | |
2860 | { | |
2861 | int uid; | |
2862 | ||
2863 | t = LABEL_EXPR_LABEL (t); | |
2864 | uid = LABEL_DECL_UID (t); | |
2865 | if (uid == -1) | |
2866 | { | |
2867 | LABEL_DECL_UID (t) = uid = cfun->last_label_uid++; | |
2868 | if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid) | |
2869 | VARRAY_GROW (label_to_block_map, 3 * uid / 2); | |
2870 | } | |
2871 | else | |
1e128c5f GB |
2872 | /* We're moving an existing label. Make sure that we've |
2873 | removed it from the old block. */ | |
2874 | gcc_assert (!bb || !VARRAY_BB (label_to_block_map, uid)); | |
6de9cd9a DN |
2875 | VARRAY_BB (label_to_block_map, uid) = bb; |
2876 | } | |
2877 | } | |
2878 | } | |
2879 | ||
8b11a64c ZD |
2880 | /* Finds iterator for STMT. */ |
2881 | ||
2882 | extern block_stmt_iterator | |
1a1804c2 | 2883 | bsi_for_stmt (tree stmt) |
8b11a64c ZD |
2884 | { |
2885 | block_stmt_iterator bsi; | |
2886 | ||
2887 | for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi)) | |
2888 | if (bsi_stmt (bsi) == stmt) | |
2889 | return bsi; | |
2890 | ||
1e128c5f | 2891 | gcc_unreachable (); |
8b11a64c | 2892 | } |
6de9cd9a DN |
2893 | |
2894 | /* Insert statement (or statement list) T before the statement | |
2895 | pointed-to by iterator I. M specifies how to update iterator I | |
2896 | after insertion (see enum bsi_iterator_update). */ | |
2897 | ||
2898 | void | |
2899 | bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m) | |
2900 | { | |
2901 | set_bb_for_stmt (t, i->bb); | |
6de9cd9a | 2902 | tsi_link_before (&i->tsi, t, m); |
68b9f53b | 2903 | modify_stmt (t); |
6de9cd9a DN |
2904 | } |
2905 | ||
2906 | ||
2907 | /* Insert statement (or statement list) T after the statement | |
2908 | pointed-to by iterator I. M specifies how to update iterator I | |
2909 | after insertion (see enum bsi_iterator_update). */ | |
2910 | ||
2911 | void | |
2912 | bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m) | |
2913 | { | |
2914 | set_bb_for_stmt (t, i->bb); | |
6de9cd9a | 2915 | tsi_link_after (&i->tsi, t, m); |
68b9f53b | 2916 | modify_stmt (t); |
6de9cd9a DN |
2917 | } |
2918 | ||
2919 | ||
2920 | /* Remove the statement pointed to by iterator I. The iterator is updated | |
2921 | to the next statement. */ | |
2922 | ||
2923 | void | |
2924 | bsi_remove (block_stmt_iterator *i) | |
2925 | { | |
2926 | tree t = bsi_stmt (*i); | |
2927 | set_bb_for_stmt (t, NULL); | |
6de9cd9a DN |
2928 | tsi_delink (&i->tsi); |
2929 | } | |
2930 | ||
2931 | ||
2932 | /* Move the statement at FROM so it comes right after the statement at TO. */ | |
2933 | ||
2934 | void | |
2935 | bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to) | |
2936 | { | |
2937 | tree stmt = bsi_stmt (*from); | |
2938 | bsi_remove (from); | |
2939 | bsi_insert_after (to, stmt, BSI_SAME_STMT); | |
2940 | } | |
2941 | ||
2942 | ||
2943 | /* Move the statement at FROM so it comes right before the statement at TO. */ | |
2944 | ||
2945 | void | |
2946 | bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to) | |
2947 | { | |
2948 | tree stmt = bsi_stmt (*from); | |
2949 | bsi_remove (from); | |
2950 | bsi_insert_before (to, stmt, BSI_SAME_STMT); | |
2951 | } | |
2952 | ||
2953 | ||
2954 | /* Move the statement at FROM to the end of basic block BB. */ | |
2955 | ||
2956 | void | |
2957 | bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb) | |
2958 | { | |
2959 | block_stmt_iterator last = bsi_last (bb); | |
2960 | ||
2961 | /* Have to check bsi_end_p because it could be an empty block. */ | |
2962 | if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last))) | |
2963 | bsi_move_before (from, &last); | |
2964 | else | |
2965 | bsi_move_after (from, &last); | |
2966 | } | |
2967 | ||
2968 | ||
2969 | /* Replace the contents of the statement pointed to by iterator BSI | |
2970 | with STMT. If PRESERVE_EH_INFO is true, the exception handling | |
2971 | information of the original statement is preserved. */ | |
2972 | ||
2973 | void | |
2974 | bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info) | |
2975 | { | |
2976 | int eh_region; | |
2977 | tree orig_stmt = bsi_stmt (*bsi); | |
2978 | ||
2979 | SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt)); | |
2980 | set_bb_for_stmt (stmt, bsi->bb); | |
2981 | ||
2982 | /* Preserve EH region information from the original statement, if | |
2983 | requested by the caller. */ | |
2984 | if (preserve_eh_info) | |
2985 | { | |
2986 | eh_region = lookup_stmt_eh_region (orig_stmt); | |
2987 | if (eh_region >= 0) | |
2988 | add_stmt_to_eh_region (stmt, eh_region); | |
2989 | } | |
2990 | ||
2991 | *bsi_stmt_ptr (*bsi) = stmt; | |
2992 | modify_stmt (stmt); | |
2993 | } | |
2994 | ||
2995 | ||
2996 | /* Insert the statement pointed-to by BSI into edge E. Every attempt | |
2997 | is made to place the statement in an existing basic block, but | |
2998 | sometimes that isn't possible. When it isn't possible, the edge is | |
2999 | split and the statement is added to the new block. | |
3000 | ||
3001 | In all cases, the returned *BSI points to the correct location. The | |
3002 | return value is true if insertion should be done after the location, | |
82b85a85 ZD |
3003 | or false if it should be done before the location. If new basic block |
3004 | has to be created, it is stored in *NEW_BB. */ | |
6de9cd9a DN |
3005 | |
3006 | static bool | |
82b85a85 ZD |
3007 | tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi, |
3008 | basic_block *new_bb) | |
6de9cd9a DN |
3009 | { |
3010 | basic_block dest, src; | |
3011 | tree tmp; | |
3012 | ||
3013 | dest = e->dest; | |
3014 | restart: | |
3015 | ||
3016 | /* If the destination has one predecessor which has no PHI nodes, | |
3017 | insert there. Except for the exit block. | |
3018 | ||
3019 | The requirement for no PHI nodes could be relaxed. Basically we | |
3020 | would have to examine the PHIs to prove that none of them used | |
e28d0cfb | 3021 | the value set by the statement we want to insert on E. That |
6de9cd9a | 3022 | hardly seems worth the effort. */ |
628f6a4e | 3023 | if (EDGE_COUNT (dest->preds) == 1 |
6de9cd9a DN |
3024 | && ! phi_nodes (dest) |
3025 | && dest != EXIT_BLOCK_PTR) | |
3026 | { | |
3027 | *bsi = bsi_start (dest); | |
3028 | if (bsi_end_p (*bsi)) | |
3029 | return true; | |
3030 | ||
3031 | /* Make sure we insert after any leading labels. */ | |
3032 | tmp = bsi_stmt (*bsi); | |
3033 | while (TREE_CODE (tmp) == LABEL_EXPR) | |
3034 | { | |
3035 | bsi_next (bsi); | |
3036 | if (bsi_end_p (*bsi)) | |
3037 | break; | |
3038 | tmp = bsi_stmt (*bsi); | |
3039 | } | |
3040 | ||
3041 | if (bsi_end_p (*bsi)) | |
3042 | { | |
3043 | *bsi = bsi_last (dest); | |
3044 | return true; | |
3045 | } | |
3046 | else | |
3047 | return false; | |
3048 | } | |
3049 | ||
3050 | /* If the source has one successor, the edge is not abnormal and | |
3051 | the last statement does not end a basic block, insert there. | |
3052 | Except for the entry block. */ | |
3053 | src = e->src; | |
3054 | if ((e->flags & EDGE_ABNORMAL) == 0 | |
628f6a4e | 3055 | && EDGE_COUNT (src->succs) == 1 |
6de9cd9a DN |
3056 | && src != ENTRY_BLOCK_PTR) |
3057 | { | |
3058 | *bsi = bsi_last (src); | |
3059 | if (bsi_end_p (*bsi)) | |
3060 | return true; | |
3061 | ||
3062 | tmp = bsi_stmt (*bsi); | |
3063 | if (!stmt_ends_bb_p (tmp)) | |
3064 | return true; | |
ce068299 JH |
3065 | |
3066 | /* Insert code just before returning the value. We may need to decompose | |
3067 | the return in the case it contains non-trivial operand. */ | |
3068 | if (TREE_CODE (tmp) == RETURN_EXPR) | |
3069 | { | |
3070 | tree op = TREE_OPERAND (tmp, 0); | |
3071 | if (!is_gimple_val (op)) | |
3072 | { | |
1e128c5f | 3073 | gcc_assert (TREE_CODE (op) == MODIFY_EXPR); |
ce068299 JH |
3074 | bsi_insert_before (bsi, op, BSI_NEW_STMT); |
3075 | TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0); | |
3076 | } | |
3077 | bsi_prev (bsi); | |
3078 | return true; | |
3079 | } | |
6de9cd9a DN |
3080 | } |
3081 | ||
3082 | /* Otherwise, create a new basic block, and split this edge. */ | |
3083 | dest = split_edge (e); | |
82b85a85 ZD |
3084 | if (new_bb) |
3085 | *new_bb = dest; | |
628f6a4e | 3086 | e = EDGE_PRED (dest, 0); |
6de9cd9a DN |
3087 | goto restart; |
3088 | } | |
3089 | ||
3090 | ||
3091 | /* This routine will commit all pending edge insertions, creating any new | |
8e731e4e | 3092 | basic blocks which are necessary. */ |
6de9cd9a DN |
3093 | |
3094 | void | |
8e731e4e | 3095 | bsi_commit_edge_inserts (void) |
6de9cd9a DN |
3096 | { |
3097 | basic_block bb; | |
3098 | edge e; | |
628f6a4e | 3099 | edge_iterator ei; |
6de9cd9a | 3100 | |
edfaf675 | 3101 | bsi_commit_one_edge_insert (EDGE_SUCC (ENTRY_BLOCK_PTR, 0), NULL); |
6de9cd9a DN |
3102 | |
3103 | FOR_EACH_BB (bb) | |
628f6a4e | 3104 | FOR_EACH_EDGE (e, ei, bb->succs) |
edfaf675 | 3105 | bsi_commit_one_edge_insert (e, NULL); |
6de9cd9a DN |
3106 | } |
3107 | ||
3108 | ||
edfaf675 AM |
3109 | /* Commit insertions pending at edge E. If a new block is created, set NEW_BB |
3110 | to this block, otherwise set it to NULL. */ | |
6de9cd9a | 3111 | |
edfaf675 AM |
3112 | void |
3113 | bsi_commit_one_edge_insert (edge e, basic_block *new_bb) | |
6de9cd9a | 3114 | { |
edfaf675 AM |
3115 | if (new_bb) |
3116 | *new_bb = NULL; | |
6de9cd9a DN |
3117 | if (PENDING_STMT (e)) |
3118 | { | |
3119 | block_stmt_iterator bsi; | |
3120 | tree stmt = PENDING_STMT (e); | |
3121 | ||
3122 | PENDING_STMT (e) = NULL_TREE; | |
3123 | ||
edfaf675 | 3124 | if (tree_find_edge_insert_loc (e, &bsi, new_bb)) |
6de9cd9a DN |
3125 | bsi_insert_after (&bsi, stmt, BSI_NEW_STMT); |
3126 | else | |
3127 | bsi_insert_before (&bsi, stmt, BSI_NEW_STMT); | |
3128 | } | |
3129 | } | |
3130 | ||
3131 | ||
3132 | /* Add STMT to the pending list of edge E. No actual insertion is | |
3133 | made until a call to bsi_commit_edge_inserts () is made. */ | |
3134 | ||
3135 | void | |
3136 | bsi_insert_on_edge (edge e, tree stmt) | |
3137 | { | |
3138 | append_to_statement_list (stmt, &PENDING_STMT (e)); | |
3139 | } | |
3140 | ||
adb35797 KH |
3141 | /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new |
3142 | block has to be created, it is returned. */ | |
82b85a85 ZD |
3143 | |
3144 | basic_block | |
3145 | bsi_insert_on_edge_immediate (edge e, tree stmt) | |
3146 | { | |
3147 | block_stmt_iterator bsi; | |
3148 | basic_block new_bb = NULL; | |
3149 | ||
1e128c5f | 3150 | gcc_assert (!PENDING_STMT (e)); |
82b85a85 ZD |
3151 | |
3152 | if (tree_find_edge_insert_loc (e, &bsi, &new_bb)) | |
3153 | bsi_insert_after (&bsi, stmt, BSI_NEW_STMT); | |
3154 | else | |
3155 | bsi_insert_before (&bsi, stmt, BSI_NEW_STMT); | |
3156 | ||
3157 | return new_bb; | |
3158 | } | |
6de9cd9a | 3159 | |
6de9cd9a DN |
3160 | /*--------------------------------------------------------------------------- |
3161 | Tree specific functions for CFG manipulation | |
3162 | ---------------------------------------------------------------------------*/ | |
3163 | ||
4f7db7f7 KH |
3164 | /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */ |
3165 | ||
3166 | static void | |
3167 | reinstall_phi_args (edge new_edge, edge old_edge) | |
3168 | { | |
3169 | tree var, phi; | |
3170 | ||
3171 | if (!PENDING_STMT (old_edge)) | |
3172 | return; | |
3173 | ||
3174 | for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest); | |
3175 | var && phi; | |
3176 | var = TREE_CHAIN (var), phi = PHI_CHAIN (phi)) | |
3177 | { | |
3178 | tree result = TREE_PURPOSE (var); | |
3179 | tree arg = TREE_VALUE (var); | |
3180 | ||
3181 | gcc_assert (result == PHI_RESULT (phi)); | |
3182 | ||
d2e398df | 3183 | add_phi_arg (phi, arg, new_edge); |
4f7db7f7 KH |
3184 | } |
3185 | ||
3186 | PENDING_STMT (old_edge) = NULL; | |
3187 | } | |
3188 | ||
6de9cd9a DN |
3189 | /* Split a (typically critical) edge EDGE_IN. Return the new block. |
3190 | Abort on abnormal edges. */ | |
3191 | ||
3192 | static basic_block | |
3193 | tree_split_edge (edge edge_in) | |
3194 | { | |
3195 | basic_block new_bb, after_bb, dest, src; | |
3196 | edge new_edge, e; | |
6de9cd9a DN |
3197 | |
3198 | /* Abnormal edges cannot be split. */ | |
1e128c5f | 3199 | gcc_assert (!(edge_in->flags & EDGE_ABNORMAL)); |
6de9cd9a DN |
3200 | |
3201 | src = edge_in->src; | |
3202 | dest = edge_in->dest; | |
3203 | ||
3204 | /* Place the new block in the block list. Try to keep the new block | |
3205 | near its "logical" location. This is of most help to humans looking | |
3206 | at debugging dumps. */ | |
7510740d | 3207 | if (dest->prev_bb && find_edge (dest->prev_bb, dest)) |
6de9cd9a | 3208 | after_bb = edge_in->src; |
7510740d KH |
3209 | else |
3210 | after_bb = dest->prev_bb; | |
6de9cd9a DN |
3211 | |
3212 | new_bb = create_empty_bb (after_bb); | |
b829f3fa JH |
3213 | new_bb->frequency = EDGE_FREQUENCY (edge_in); |
3214 | new_bb->count = edge_in->count; | |
6de9cd9a | 3215 | new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU); |
b829f3fa JH |
3216 | new_edge->probability = REG_BR_PROB_BASE; |
3217 | new_edge->count = edge_in->count; | |
6de9cd9a | 3218 | |
1e128c5f GB |
3219 | e = redirect_edge_and_branch (edge_in, new_bb); |
3220 | gcc_assert (e); | |
4f7db7f7 | 3221 | reinstall_phi_args (new_edge, e); |
6de9cd9a DN |
3222 | |
3223 | return new_bb; | |
3224 | } | |
3225 | ||
3226 | ||
3227 | /* Return true when BB has label LABEL in it. */ | |
3228 | ||
3229 | static bool | |
3230 | has_label_p (basic_block bb, tree label) | |
3231 | { | |
3232 | block_stmt_iterator bsi; | |
3233 | ||
3234 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
3235 | { | |
3236 | tree stmt = bsi_stmt (bsi); | |
3237 | ||
3238 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
3239 | return false; | |
3240 | if (LABEL_EXPR_LABEL (stmt) == label) | |
3241 | return true; | |
3242 | } | |
3243 | return false; | |
3244 | } | |
3245 | ||
3246 | ||
3247 | /* Callback for walk_tree, check that all elements with address taken are | |
7a442a1d SB |
3248 | properly noticed as such. The DATA is an int* that is 1 if TP was seen |
3249 | inside a PHI node. */ | |
6de9cd9a DN |
3250 | |
3251 | static tree | |
2fbe90f2 | 3252 | verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) |
6de9cd9a DN |
3253 | { |
3254 | tree t = *tp, x; | |
7a442a1d | 3255 | bool in_phi = (data != NULL); |
6de9cd9a DN |
3256 | |
3257 | if (TYPE_P (t)) | |
3258 | *walk_subtrees = 0; | |
2fbe90f2 | 3259 | |
50b04185 RK |
3260 | /* Check operand N for being valid GIMPLE and give error MSG if not. |
3261 | We check for constants explicitly since they are not considered | |
3262 | gimple invariants if they overflowed. */ | |
2fbe90f2 | 3263 | #define CHECK_OP(N, MSG) \ |
6615c446 JO |
3264 | do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \ |
3265 | && !is_gimple_val (TREE_OPERAND (t, N))) \ | |
2fbe90f2 | 3266 | { error (MSG); return TREE_OPERAND (t, N); }} while (0) |
6de9cd9a DN |
3267 | |
3268 | switch (TREE_CODE (t)) | |
3269 | { | |
3270 | case SSA_NAME: | |
3271 | if (SSA_NAME_IN_FREE_LIST (t)) | |
3272 | { | |
3273 | error ("SSA name in freelist but still referenced"); | |
3274 | return *tp; | |
3275 | } | |
3276 | break; | |
3277 | ||
3278 | case MODIFY_EXPR: | |
3279 | x = TREE_OPERAND (t, 0); | |
3280 | if (TREE_CODE (x) == BIT_FIELD_REF | |
3281 | && is_gimple_reg (TREE_OPERAND (x, 0))) | |
3282 | { | |
3283 | error ("GIMPLE register modified with BIT_FIELD_REF"); | |
2fbe90f2 | 3284 | return t; |
6de9cd9a DN |
3285 | } |
3286 | break; | |
3287 | ||
3288 | case ADDR_EXPR: | |
7a442a1d SB |
3289 | /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing |
3290 | dead PHIs that take the address of something. But if the PHI | |
3291 | result is dead, the fact that it takes the address of anything | |
3292 | is irrelevant. Because we can not tell from here if a PHI result | |
3293 | is dead, we just skip this check for PHIs altogether. This means | |
3294 | we may be missing "valid" checks, but what can you do? | |
3295 | This was PR19217. */ | |
3296 | if (in_phi) | |
3297 | break; | |
3298 | ||
2fbe90f2 RK |
3299 | /* Skip any references (they will be checked when we recurse down the |
3300 | tree) and ensure that any variable used as a prefix is marked | |
3301 | addressable. */ | |
3302 | for (x = TREE_OPERAND (t, 0); | |
afe84921 | 3303 | handled_component_p (x); |
44de5aeb RK |
3304 | x = TREE_OPERAND (x, 0)) |
3305 | ; | |
3306 | ||
6de9cd9a DN |
3307 | if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL) |
3308 | return NULL; | |
3309 | if (!TREE_ADDRESSABLE (x)) | |
3310 | { | |
3311 | error ("address taken, but ADDRESSABLE bit not set"); | |
3312 | return x; | |
3313 | } | |
3314 | break; | |
3315 | ||
3316 | case COND_EXPR: | |
a6234684 | 3317 | x = COND_EXPR_COND (t); |
6de9cd9a DN |
3318 | if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE) |
3319 | { | |
3320 | error ("non-boolean used in condition"); | |
3321 | return x; | |
3322 | } | |
3323 | break; | |
3324 | ||
3325 | case NOP_EXPR: | |
3326 | case CONVERT_EXPR: | |
3327 | case FIX_TRUNC_EXPR: | |
3328 | case FIX_CEIL_EXPR: | |
3329 | case FIX_FLOOR_EXPR: | |
3330 | case FIX_ROUND_EXPR: | |
3331 | case FLOAT_EXPR: | |
3332 | case NEGATE_EXPR: | |
3333 | case ABS_EXPR: | |
3334 | case BIT_NOT_EXPR: | |
3335 | case NON_LVALUE_EXPR: | |
3336 | case TRUTH_NOT_EXPR: | |
2fbe90f2 | 3337 | CHECK_OP (0, "Invalid operand to unary operator"); |
6de9cd9a DN |
3338 | break; |
3339 | ||
3340 | case REALPART_EXPR: | |
3341 | case IMAGPART_EXPR: | |
2fbe90f2 RK |
3342 | case COMPONENT_REF: |
3343 | case ARRAY_REF: | |
3344 | case ARRAY_RANGE_REF: | |
3345 | case BIT_FIELD_REF: | |
3346 | case VIEW_CONVERT_EXPR: | |
3347 | /* We have a nest of references. Verify that each of the operands | |
3348 | that determine where to reference is either a constant or a variable, | |
3349 | verify that the base is valid, and then show we've already checked | |
3350 | the subtrees. */ | |
afe84921 | 3351 | while (handled_component_p (t)) |
2fbe90f2 RK |
3352 | { |
3353 | if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2)) | |
3354 | CHECK_OP (2, "Invalid COMPONENT_REF offset operator"); | |
3355 | else if (TREE_CODE (t) == ARRAY_REF | |
3356 | || TREE_CODE (t) == ARRAY_RANGE_REF) | |
3357 | { | |
3358 | CHECK_OP (1, "Invalid array index."); | |
3359 | if (TREE_OPERAND (t, 2)) | |
3360 | CHECK_OP (2, "Invalid array lower bound."); | |
3361 | if (TREE_OPERAND (t, 3)) | |
3362 | CHECK_OP (3, "Invalid array stride."); | |
3363 | } | |
3364 | else if (TREE_CODE (t) == BIT_FIELD_REF) | |
3365 | { | |
3366 | CHECK_OP (1, "Invalid operand to BIT_FIELD_REF"); | |
3367 | CHECK_OP (2, "Invalid operand to BIT_FIELD_REF"); | |
3368 | } | |
3369 | ||
3370 | t = TREE_OPERAND (t, 0); | |
3371 | } | |
3372 | ||
6615c446 | 3373 | if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t)) |
2fbe90f2 RK |
3374 | { |
3375 | error ("Invalid reference prefix."); | |
3376 | return t; | |
3377 | } | |
3378 | *walk_subtrees = 0; | |
6de9cd9a DN |
3379 | break; |
3380 | ||
3381 | case LT_EXPR: | |
3382 | case LE_EXPR: | |
3383 | case GT_EXPR: | |
3384 | case GE_EXPR: | |
3385 | case EQ_EXPR: | |
3386 | case NE_EXPR: | |
3387 | case UNORDERED_EXPR: | |
3388 | case ORDERED_EXPR: | |
3389 | case UNLT_EXPR: | |
3390 | case UNLE_EXPR: | |
3391 | case UNGT_EXPR: | |
3392 | case UNGE_EXPR: | |
3393 | case UNEQ_EXPR: | |
d1a7edaf | 3394 | case LTGT_EXPR: |
6de9cd9a DN |
3395 | case PLUS_EXPR: |
3396 | case MINUS_EXPR: | |
3397 | case MULT_EXPR: | |
3398 | case TRUNC_DIV_EXPR: | |
3399 | case CEIL_DIV_EXPR: | |
3400 | case FLOOR_DIV_EXPR: | |
3401 | case ROUND_DIV_EXPR: | |
3402 | case TRUNC_MOD_EXPR: | |
3403 | case CEIL_MOD_EXPR: | |
3404 | case FLOOR_MOD_EXPR: | |
3405 | case ROUND_MOD_EXPR: | |
3406 | case RDIV_EXPR: | |
3407 | case EXACT_DIV_EXPR: | |
3408 | case MIN_EXPR: | |
3409 | case MAX_EXPR: | |
3410 | case LSHIFT_EXPR: | |
3411 | case RSHIFT_EXPR: | |
3412 | case LROTATE_EXPR: | |
3413 | case RROTATE_EXPR: | |
3414 | case BIT_IOR_EXPR: | |
3415 | case BIT_XOR_EXPR: | |
3416 | case BIT_AND_EXPR: | |
50b04185 RK |
3417 | CHECK_OP (0, "Invalid operand to binary operator"); |
3418 | CHECK_OP (1, "Invalid operand to binary operator"); | |
6de9cd9a DN |
3419 | break; |
3420 | ||
3421 | default: | |
3422 | break; | |
3423 | } | |
3424 | return NULL; | |
2fbe90f2 RK |
3425 | |
3426 | #undef CHECK_OP | |
6de9cd9a DN |
3427 | } |
3428 | ||
3429 | ||
3430 | /* Verify STMT, return true if STMT is not in GIMPLE form. | |
3431 | TODO: Implement type checking. */ | |
3432 | ||
3433 | static bool | |
1eaba2f2 | 3434 | verify_stmt (tree stmt, bool last_in_block) |
6de9cd9a DN |
3435 | { |
3436 | tree addr; | |
3437 | ||
3438 | if (!is_gimple_stmt (stmt)) | |
3439 | { | |
3440 | error ("Is not a valid GIMPLE statement."); | |
1eaba2f2 | 3441 | goto fail; |
6de9cd9a DN |
3442 | } |
3443 | ||
3444 | addr = walk_tree (&stmt, verify_expr, NULL, NULL); | |
3445 | if (addr) | |
3446 | { | |
3447 | debug_generic_stmt (addr); | |
3448 | return true; | |
3449 | } | |
3450 | ||
1eaba2f2 RH |
3451 | /* If the statement is marked as part of an EH region, then it is |
3452 | expected that the statement could throw. Verify that when we | |
3453 | have optimizations that simplify statements such that we prove | |
3454 | that they cannot throw, that we update other data structures | |
3455 | to match. */ | |
3456 | if (lookup_stmt_eh_region (stmt) >= 0) | |
3457 | { | |
3458 | if (!tree_could_throw_p (stmt)) | |
3459 | { | |
971801ff | 3460 | error ("Statement marked for throw, but doesn%'t."); |
1eaba2f2 RH |
3461 | goto fail; |
3462 | } | |
3463 | if (!last_in_block && tree_can_throw_internal (stmt)) | |
3464 | { | |
3465 | error ("Statement marked for throw in middle of block."); | |
3466 | goto fail; | |
3467 | } | |
3468 | } | |
3469 | ||
6de9cd9a | 3470 | return false; |
1eaba2f2 RH |
3471 | |
3472 | fail: | |
3473 | debug_generic_stmt (stmt); | |
3474 | return true; | |
6de9cd9a DN |
3475 | } |
3476 | ||
3477 | ||
3478 | /* Return true when the T can be shared. */ | |
3479 | ||
3480 | static bool | |
3481 | tree_node_can_be_shared (tree t) | |
3482 | { | |
6615c446 | 3483 | if (IS_TYPE_OR_DECL_P (t) |
6de9cd9a DN |
3484 | /* We check for constants explicitly since they are not considered |
3485 | gimple invariants if they overflowed. */ | |
6615c446 | 3486 | || CONSTANT_CLASS_P (t) |
6de9cd9a | 3487 | || is_gimple_min_invariant (t) |
5e23162d | 3488 | || TREE_CODE (t) == SSA_NAME |
bac8beb4 | 3489 | || t == error_mark_node) |
6de9cd9a DN |
3490 | return true; |
3491 | ||
92b6dff3 JL |
3492 | if (TREE_CODE (t) == CASE_LABEL_EXPR) |
3493 | return true; | |
3494 | ||
44de5aeb | 3495 | while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) |
6de9cd9a DN |
3496 | /* We check for constants explicitly since they are not considered |
3497 | gimple invariants if they overflowed. */ | |
6615c446 | 3498 | && (CONSTANT_CLASS_P (TREE_OPERAND (t, 1)) |
6de9cd9a DN |
3499 | || is_gimple_min_invariant (TREE_OPERAND (t, 1)))) |
3500 | || (TREE_CODE (t) == COMPONENT_REF | |
3501 | || TREE_CODE (t) == REALPART_EXPR | |
3502 | || TREE_CODE (t) == IMAGPART_EXPR)) | |
3503 | t = TREE_OPERAND (t, 0); | |
3504 | ||
3505 | if (DECL_P (t)) | |
3506 | return true; | |
3507 | ||
3508 | return false; | |
3509 | } | |
3510 | ||
3511 | ||
3512 | /* Called via walk_trees. Verify tree sharing. */ | |
3513 | ||
3514 | static tree | |
3515 | verify_node_sharing (tree * tp, int *walk_subtrees, void *data) | |
3516 | { | |
3517 | htab_t htab = (htab_t) data; | |
3518 | void **slot; | |
3519 | ||
3520 | if (tree_node_can_be_shared (*tp)) | |
3521 | { | |
3522 | *walk_subtrees = false; | |
3523 | return NULL; | |
3524 | } | |
3525 | ||
3526 | slot = htab_find_slot (htab, *tp, INSERT); | |
3527 | if (*slot) | |
3528 | return *slot; | |
3529 | *slot = *tp; | |
3530 | ||
3531 | return NULL; | |
3532 | } | |
3533 | ||
3534 | ||
3535 | /* Verify the GIMPLE statement chain. */ | |
3536 | ||
3537 | void | |
3538 | verify_stmts (void) | |
3539 | { | |
3540 | basic_block bb; | |
3541 | block_stmt_iterator bsi; | |
3542 | bool err = false; | |
3543 | htab_t htab; | |
3544 | tree addr; | |
3545 | ||
3546 | timevar_push (TV_TREE_STMT_VERIFY); | |
3547 | htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); | |
3548 | ||
3549 | FOR_EACH_BB (bb) | |
3550 | { | |
3551 | tree phi; | |
3552 | int i; | |
3553 | ||
17192884 | 3554 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a DN |
3555 | { |
3556 | int phi_num_args = PHI_NUM_ARGS (phi); | |
3557 | ||
3558 | for (i = 0; i < phi_num_args; i++) | |
3559 | { | |
3560 | tree t = PHI_ARG_DEF (phi, i); | |
3561 | tree addr; | |
3562 | ||
3563 | /* Addressable variables do have SSA_NAMEs but they | |
3564 | are not considered gimple values. */ | |
3565 | if (TREE_CODE (t) != SSA_NAME | |
3566 | && TREE_CODE (t) != FUNCTION_DECL | |
3567 | && !is_gimple_val (t)) | |
3568 | { | |
3569 | error ("PHI def is not a GIMPLE value"); | |
3570 | debug_generic_stmt (phi); | |
3571 | debug_generic_stmt (t); | |
3572 | err |= true; | |
3573 | } | |
3574 | ||
7a442a1d | 3575 | addr = walk_tree (&t, verify_expr, (void *) 1, NULL); |
6de9cd9a DN |
3576 | if (addr) |
3577 | { | |
3578 | debug_generic_stmt (addr); | |
3579 | err |= true; | |
3580 | } | |
3581 | ||
3582 | addr = walk_tree (&t, verify_node_sharing, htab, NULL); | |
3583 | if (addr) | |
3584 | { | |
3585 | error ("Incorrect sharing of tree nodes"); | |
3586 | debug_generic_stmt (phi); | |
3587 | debug_generic_stmt (addr); | |
3588 | err |= true; | |
3589 | } | |
3590 | } | |
3591 | } | |
3592 | ||
1eaba2f2 | 3593 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); ) |
6de9cd9a DN |
3594 | { |
3595 | tree stmt = bsi_stmt (bsi); | |
1eaba2f2 RH |
3596 | bsi_next (&bsi); |
3597 | err |= verify_stmt (stmt, bsi_end_p (bsi)); | |
6de9cd9a DN |
3598 | addr = walk_tree (&stmt, verify_node_sharing, htab, NULL); |
3599 | if (addr) | |
3600 | { | |
3601 | error ("Incorrect sharing of tree nodes"); | |
3602 | debug_generic_stmt (stmt); | |
3603 | debug_generic_stmt (addr); | |
3604 | err |= true; | |
3605 | } | |
3606 | } | |
3607 | } | |
3608 | ||
3609 | if (err) | |
3610 | internal_error ("verify_stmts failed."); | |
3611 | ||
3612 | htab_delete (htab); | |
3613 | timevar_pop (TV_TREE_STMT_VERIFY); | |
3614 | } | |
3615 | ||
3616 | ||
3617 | /* Verifies that the flow information is OK. */ | |
3618 | ||
3619 | static int | |
3620 | tree_verify_flow_info (void) | |
3621 | { | |
3622 | int err = 0; | |
3623 | basic_block bb; | |
3624 | block_stmt_iterator bsi; | |
3625 | tree stmt; | |
3626 | edge e; | |
628f6a4e | 3627 | edge_iterator ei; |
6de9cd9a DN |
3628 | |
3629 | if (ENTRY_BLOCK_PTR->stmt_list) | |
3630 | { | |
3631 | error ("ENTRY_BLOCK has a statement list associated with it\n"); | |
3632 | err = 1; | |
3633 | } | |
3634 | ||
3635 | if (EXIT_BLOCK_PTR->stmt_list) | |
3636 | { | |
3637 | error ("EXIT_BLOCK has a statement list associated with it\n"); | |
3638 | err = 1; | |
3639 | } | |
3640 | ||
628f6a4e | 3641 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds) |
6de9cd9a DN |
3642 | if (e->flags & EDGE_FALLTHRU) |
3643 | { | |
3644 | error ("Fallthru to exit from bb %d\n", e->src->index); | |
3645 | err = 1; | |
3646 | } | |
3647 | ||
3648 | FOR_EACH_BB (bb) | |
3649 | { | |
3650 | bool found_ctrl_stmt = false; | |
3651 | ||
548414c6 KH |
3652 | stmt = NULL_TREE; |
3653 | ||
6de9cd9a DN |
3654 | /* Skip labels on the start of basic block. */ |
3655 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
3656 | { | |
548414c6 KH |
3657 | tree prev_stmt = stmt; |
3658 | ||
3659 | stmt = bsi_stmt (bsi); | |
3660 | ||
3661 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
6de9cd9a DN |
3662 | break; |
3663 | ||
548414c6 KH |
3664 | if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt))) |
3665 | { | |
3666 | error ("Nonlocal label %s is not first " | |
cde68236 | 3667 | "in a sequence of labels in bb %d", |
548414c6 KH |
3668 | IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))), |
3669 | bb->index); | |
3670 | err = 1; | |
3671 | } | |
3672 | ||
3673 | if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb) | |
6de9cd9a DN |
3674 | { |
3675 | error ("Label %s to block does not match in bb %d\n", | |
77568960 | 3676 | IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))), |
6de9cd9a DN |
3677 | bb->index); |
3678 | err = 1; | |
3679 | } | |
3680 | ||
548414c6 | 3681 | if (decl_function_context (LABEL_EXPR_LABEL (stmt)) |
6de9cd9a DN |
3682 | != current_function_decl) |
3683 | { | |
3684 | error ("Label %s has incorrect context in bb %d\n", | |
77568960 | 3685 | IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))), |
6de9cd9a DN |
3686 | bb->index); |
3687 | err = 1; | |
3688 | } | |
3689 | } | |
3690 | ||
3691 | /* Verify that body of basic block BB is free of control flow. */ | |
3692 | for (; !bsi_end_p (bsi); bsi_next (&bsi)) | |
3693 | { | |
3694 | tree stmt = bsi_stmt (bsi); | |
3695 | ||
3696 | if (found_ctrl_stmt) | |
3697 | { | |
3698 | error ("Control flow in the middle of basic block %d\n", | |
3699 | bb->index); | |
3700 | err = 1; | |
3701 | } | |
3702 | ||
3703 | if (stmt_ends_bb_p (stmt)) | |
3704 | found_ctrl_stmt = true; | |
3705 | ||
3706 | if (TREE_CODE (stmt) == LABEL_EXPR) | |
3707 | { | |
3708 | error ("Label %s in the middle of basic block %d\n", | |
3709 | IDENTIFIER_POINTER (DECL_NAME (stmt)), | |
3710 | bb->index); | |
3711 | err = 1; | |
3712 | } | |
3713 | } | |
3714 | bsi = bsi_last (bb); | |
3715 | if (bsi_end_p (bsi)) | |
3716 | continue; | |
3717 | ||
3718 | stmt = bsi_stmt (bsi); | |
3719 | ||
3720 | if (is_ctrl_stmt (stmt)) | |
3721 | { | |
628f6a4e | 3722 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
3723 | if (e->flags & EDGE_FALLTHRU) |
3724 | { | |
3725 | error ("Fallthru edge after a control statement in bb %d \n", | |
3726 | bb->index); | |
3727 | err = 1; | |
3728 | } | |
3729 | } | |
3730 | ||
3731 | switch (TREE_CODE (stmt)) | |
3732 | { | |
3733 | case COND_EXPR: | |
3734 | { | |
3735 | edge true_edge; | |
3736 | edge false_edge; | |
3737 | if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR | |
3738 | || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR) | |
3739 | { | |
3740 | error ("Structured COND_EXPR at the end of bb %d\n", bb->index); | |
3741 | err = 1; | |
3742 | } | |
3743 | ||
3744 | extract_true_false_edges_from_block (bb, &true_edge, &false_edge); | |
3745 | ||
3746 | if (!true_edge || !false_edge | |
3747 | || !(true_edge->flags & EDGE_TRUE_VALUE) | |
3748 | || !(false_edge->flags & EDGE_FALSE_VALUE) | |
3749 | || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL)) | |
3750 | || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL)) | |
628f6a4e | 3751 | || EDGE_COUNT (bb->succs) >= 3) |
6de9cd9a DN |
3752 | { |
3753 | error ("Wrong outgoing edge flags at end of bb %d\n", | |
3754 | bb->index); | |
3755 | err = 1; | |
3756 | } | |
3757 | ||
3758 | if (!has_label_p (true_edge->dest, | |
3759 | GOTO_DESTINATION (COND_EXPR_THEN (stmt)))) | |
3760 | { | |
971801ff | 3761 | error ("%<then%> label does not match edge at end of bb %d\n", |
6de9cd9a DN |
3762 | bb->index); |
3763 | err = 1; | |
3764 | } | |
3765 | ||
3766 | if (!has_label_p (false_edge->dest, | |
3767 | GOTO_DESTINATION (COND_EXPR_ELSE (stmt)))) | |
3768 | { | |
971801ff | 3769 | error ("%<else%> label does not match edge at end of bb %d\n", |
6de9cd9a DN |
3770 | bb->index); |
3771 | err = 1; | |
3772 | } | |
3773 | } | |
3774 | break; | |
3775 | ||
3776 | case GOTO_EXPR: | |
3777 | if (simple_goto_p (stmt)) | |
3778 | { | |
3779 | error ("Explicit goto at end of bb %d\n", bb->index); | |
3780 | err = 1; | |
3781 | } | |
3782 | else | |
3783 | { | |
3784 | /* FIXME. We should double check that the labels in the | |
3785 | destination blocks have their address taken. */ | |
628f6a4e | 3786 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
3787 | if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE |
3788 | | EDGE_FALSE_VALUE)) | |
3789 | || !(e->flags & EDGE_ABNORMAL)) | |
3790 | { | |
3791 | error ("Wrong outgoing edge flags at end of bb %d\n", | |
3792 | bb->index); | |
3793 | err = 1; | |
3794 | } | |
3795 | } | |
3796 | break; | |
3797 | ||
3798 | case RETURN_EXPR: | |
628f6a4e BE |
3799 | if (EDGE_COUNT (bb->succs) != 1 |
3800 | || (EDGE_SUCC (bb, 0)->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL | |
6de9cd9a DN |
3801 | | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) |
3802 | { | |
3803 | error ("Wrong outgoing edge flags at end of bb %d\n", bb->index); | |
3804 | err = 1; | |
3805 | } | |
628f6a4e | 3806 | if (EDGE_SUCC (bb, 0)->dest != EXIT_BLOCK_PTR) |
6de9cd9a DN |
3807 | { |
3808 | error ("Return edge does not point to exit in bb %d\n", | |
3809 | bb->index); | |
3810 | err = 1; | |
3811 | } | |
3812 | break; | |
3813 | ||
3814 | case SWITCH_EXPR: | |
3815 | { | |
7853504d | 3816 | tree prev; |
6de9cd9a DN |
3817 | edge e; |
3818 | size_t i, n; | |
3819 | tree vec; | |
3820 | ||
3821 | vec = SWITCH_LABELS (stmt); | |
3822 | n = TREE_VEC_LENGTH (vec); | |
3823 | ||
3824 | /* Mark all the destination basic blocks. */ | |
3825 | for (i = 0; i < n; ++i) | |
3826 | { | |
3827 | tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i)); | |
3828 | basic_block label_bb = label_to_block (lab); | |
3829 | ||
1e128c5f | 3830 | gcc_assert (!label_bb->aux || label_bb->aux == (void *)1); |
6de9cd9a DN |
3831 | label_bb->aux = (void *)1; |
3832 | } | |
3833 | ||
7853504d SB |
3834 | /* Verify that the case labels are sorted. */ |
3835 | prev = TREE_VEC_ELT (vec, 0); | |
3836 | for (i = 1; i < n - 1; ++i) | |
3837 | { | |
3838 | tree c = TREE_VEC_ELT (vec, i); | |
3839 | if (! CASE_LOW (c)) | |
3840 | { | |
3841 | error ("Found default case not at end of case vector"); | |
3842 | err = 1; | |
3843 | continue; | |
3844 | } | |
3845 | if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c))) | |
3846 | { | |
3847 | error ("Case labels not sorted:\n "); | |
3848 | print_generic_expr (stderr, prev, 0); | |
3849 | fprintf (stderr," is greater than "); | |
3850 | print_generic_expr (stderr, c, 0); | |
3851 | fprintf (stderr," but comes before it.\n"); | |
3852 | err = 1; | |
3853 | } | |
3854 | prev = c; | |
3855 | } | |
3856 | if (CASE_LOW (TREE_VEC_ELT (vec, n - 1))) | |
3857 | { | |
3858 | error ("No default case found at end of case vector"); | |
3859 | err = 1; | |
3860 | } | |
3861 | ||
628f6a4e | 3862 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
3863 | { |
3864 | if (!e->dest->aux) | |
3865 | { | |
3866 | error ("Extra outgoing edge %d->%d\n", | |
3867 | bb->index, e->dest->index); | |
3868 | err = 1; | |
3869 | } | |
3870 | e->dest->aux = (void *)2; | |
3871 | if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL | |
3872 | | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) | |
3873 | { | |
3874 | error ("Wrong outgoing edge flags at end of bb %d\n", | |
3875 | bb->index); | |
3876 | err = 1; | |
3877 | } | |
3878 | } | |
3879 | ||
3880 | /* Check that we have all of them. */ | |
3881 | for (i = 0; i < n; ++i) | |
3882 | { | |
3883 | tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i)); | |
3884 | basic_block label_bb = label_to_block (lab); | |
3885 | ||
3886 | if (label_bb->aux != (void *)2) | |
3887 | { | |
6ade0959 | 3888 | error ("Missing edge %i->%i", |
6de9cd9a DN |
3889 | bb->index, label_bb->index); |
3890 | err = 1; | |
3891 | } | |
3892 | } | |
3893 | ||
628f6a4e | 3894 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
3895 | e->dest->aux = (void *)0; |
3896 | } | |
3897 | ||
3898 | default: ; | |
3899 | } | |
3900 | } | |
3901 | ||
3902 | if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY) | |
3903 | verify_dominators (CDI_DOMINATORS); | |
3904 | ||
3905 | return err; | |
3906 | } | |
3907 | ||
3908 | ||
f0b698c1 | 3909 | /* Updates phi nodes after creating a forwarder block joined |
6de9cd9a DN |
3910 | by edge FALLTHRU. */ |
3911 | ||
3912 | static void | |
3913 | tree_make_forwarder_block (edge fallthru) | |
3914 | { | |
3915 | edge e; | |
628f6a4e | 3916 | edge_iterator ei; |
6de9cd9a | 3917 | basic_block dummy, bb; |
5ae71719 | 3918 | tree phi, new_phi, var; |
6de9cd9a DN |
3919 | |
3920 | dummy = fallthru->src; | |
3921 | bb = fallthru->dest; | |
3922 | ||
628f6a4e | 3923 | if (EDGE_COUNT (bb->preds) == 1) |
6de9cd9a DN |
3924 | return; |
3925 | ||
3926 | /* If we redirected a branch we must create new phi nodes at the | |
3927 | start of BB. */ | |
17192884 | 3928 | for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi)) |
6de9cd9a DN |
3929 | { |
3930 | var = PHI_RESULT (phi); | |
3931 | new_phi = create_phi_node (var, bb); | |
3932 | SSA_NAME_DEF_STMT (var) = new_phi; | |
d00ad49b | 3933 | SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi)); |
d2e398df | 3934 | add_phi_arg (new_phi, PHI_RESULT (phi), fallthru); |
6de9cd9a DN |
3935 | } |
3936 | ||
17192884 | 3937 | /* Ensure that the PHI node chain is in the same order. */ |
5ae71719 | 3938 | set_phi_nodes (bb, phi_reverse (phi_nodes (bb))); |
6de9cd9a DN |
3939 | |
3940 | /* Add the arguments we have stored on edges. */ | |
628f6a4e | 3941 | FOR_EACH_EDGE (e, ei, bb->preds) |
6de9cd9a DN |
3942 | { |
3943 | if (e == fallthru) | |
3944 | continue; | |
3945 | ||
71882046 | 3946 | flush_pending_stmts (e); |
6de9cd9a DN |
3947 | } |
3948 | } | |
3949 | ||
3950 | ||
3951 | /* Return true if basic block BB does nothing except pass control | |
3952 | flow to another block and that we can safely insert a label at | |
10a52335 KH |
3953 | the start of the successor block. |
3954 | ||
3955 | As a precondition, we require that BB be not equal to | |
3956 | ENTRY_BLOCK_PTR. */ | |
6de9cd9a DN |
3957 | |
3958 | static bool | |
23ab2e4e | 3959 | tree_forwarder_block_p (basic_block bb, bool phi_wanted) |
6de9cd9a DN |
3960 | { |
3961 | block_stmt_iterator bsi; | |
6de9cd9a | 3962 | |
10a52335 | 3963 | /* BB must have a single outgoing edge. */ |
628f6a4e | 3964 | if (EDGE_COUNT (bb->succs) != 1 |
23ab2e4e KH |
3965 | /* If PHI_WANTED is false, BB must not have any PHI nodes. |
3966 | Otherwise, BB must have PHI nodes. */ | |
3967 | || (phi_nodes (bb) != NULL_TREE) != phi_wanted | |
10a52335 | 3968 | /* BB may not be a predecessor of EXIT_BLOCK_PTR. */ |
628f6a4e | 3969 | || EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR |
63bb59a3 ZD |
3970 | /* Nor should this be an infinite loop. */ |
3971 | || EDGE_SUCC (bb, 0)->dest == bb | |
10a52335 KH |
3972 | /* BB may not have an abnormal outgoing edge. */ |
3973 | || (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL)) | |
78b6731d | 3974 | return false; |
6de9cd9a | 3975 | |
10a52335 KH |
3976 | #if ENABLE_CHECKING |
3977 | gcc_assert (bb != ENTRY_BLOCK_PTR); | |
3978 | #endif | |
3979 | ||
f8ef82ea KH |
3980 | /* Now walk through the statements backward. We can ignore labels, |
3981 | anything else means this is not a forwarder block. */ | |
3982 | for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
6de9cd9a DN |
3983 | { |
3984 | tree stmt = bsi_stmt (bsi); | |
3985 | ||
3986 | switch (TREE_CODE (stmt)) | |
3987 | { | |
3988 | case LABEL_EXPR: | |
3989 | if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt))) | |
3990 | return false; | |
3991 | break; | |
3992 | ||
3993 | default: | |
6de9cd9a DN |
3994 | return false; |
3995 | } | |
3996 | } | |
3997 | ||
91d9ede4 KH |
3998 | if (find_edge (ENTRY_BLOCK_PTR, bb)) |
3999 | return false; | |
4000 | ||
6de9cd9a DN |
4001 | return true; |
4002 | } | |
4003 | ||
63bb59a3 ZD |
4004 | /* Return true if BB has at least one abnormal incoming edge. */ |
4005 | ||
4006 | static inline bool | |
4007 | has_abnormal_incoming_edge_p (basic_block bb) | |
4008 | { | |
4009 | edge e; | |
4010 | edge_iterator ei; | |
4011 | ||
4012 | FOR_EACH_EDGE (e, ei, bb->preds) | |
4013 | if (e->flags & EDGE_ABNORMAL) | |
4014 | return true; | |
4015 | ||
4016 | return false; | |
4017 | } | |
4018 | ||
adb35797 | 4019 | /* Removes forwarder block BB. Returns false if this failed. If a new |
63bb59a3 ZD |
4020 | forwarder block is created due to redirection of edges, it is |
4021 | stored to worklist. */ | |
072269d8 KH |
4022 | |
4023 | static bool | |
63bb59a3 | 4024 | remove_forwarder_block (basic_block bb, basic_block **worklist) |
072269d8 | 4025 | { |
63bb59a3 ZD |
4026 | edge succ = EDGE_SUCC (bb, 0), e, s; |
4027 | basic_block dest = succ->dest; | |
4028 | tree label; | |
4029 | tree phi; | |
072269d8 | 4030 | edge_iterator ei; |
63bb59a3 ZD |
4031 | block_stmt_iterator bsi, bsi_to; |
4032 | bool seen_abnormal_edge = false; | |
4033 | ||
4034 | /* We check for infinite loops already in tree_forwarder_block_p. | |
4035 | However it may happen that the infinite loop is created | |
4036 | afterwards due to removal of forwarders. */ | |
4037 | if (dest == bb) | |
4038 | return false; | |
072269d8 | 4039 | |
a85ba5a8 | 4040 | /* If the destination block consists of a nonlocal label, do not merge |
63bb59a3 | 4041 | it. */ |
04d38c3b | 4042 | label = first_stmt (dest); |
63bb59a3 ZD |
4043 | if (label |
4044 | && TREE_CODE (label) == LABEL_EXPR | |
4045 | && DECL_NONLOCAL (LABEL_EXPR_LABEL (label))) | |
4046 | return false; | |
4047 | ||
4048 | /* If there is an abnormal edge to basic block BB, but not into | |
4049 | dest, problems might occur during removal of the phi node at out | |
4050 | of ssa due to overlapping live ranges of registers. | |
4051 | ||
4052 | If there is an abnormal edge in DEST, the problems would occur | |
4053 | anyway since cleanup_dead_labels would then merge the labels for | |
4054 | two different eh regions, and rest of exception handling code | |
4055 | does not like it. | |
4056 | ||
4057 | So if there is an abnormal edge to BB, proceed only if there is | |
4058 | no abnormal edge to DEST and there are no phi nodes in DEST. */ | |
4059 | if (has_abnormal_incoming_edge_p (bb)) | |
072269d8 | 4060 | { |
63bb59a3 | 4061 | seen_abnormal_edge = true; |
072269d8 | 4062 | |
63bb59a3 ZD |
4063 | if (has_abnormal_incoming_edge_p (dest) |
4064 | || phi_nodes (dest) != NULL_TREE) | |
4065 | return false; | |
4066 | } | |
072269d8 | 4067 | |
63bb59a3 ZD |
4068 | /* If there are phi nodes in DEST, and some of the blocks that are |
4069 | predecessors of BB are also predecessors of DEST, check that the | |
4070 | phi node arguments match. */ | |
4071 | if (phi_nodes (dest)) | |
4072 | { | |
4073 | FOR_EACH_EDGE (e, ei, bb->preds) | |
072269d8 | 4074 | { |
63bb59a3 ZD |
4075 | s = find_edge (e->src, dest); |
4076 | if (!s) | |
4077 | continue; | |
4078 | ||
4079 | if (!phi_alternatives_equal (dest, succ, s)) | |
4080 | return false; | |
072269d8 | 4081 | } |
63bb59a3 | 4082 | } |
072269d8 | 4083 | |
63bb59a3 ZD |
4084 | /* Redirect the edges. */ |
4085 | for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); ) | |
4086 | { | |
4087 | if (e->flags & EDGE_ABNORMAL) | |
072269d8 | 4088 | { |
63bb59a3 ZD |
4089 | /* If there is an abnormal edge, redirect it anyway, and |
4090 | move the labels to the new block to make it legal. */ | |
4091 | s = redirect_edge_succ_nodup (e, dest); | |
072269d8 | 4092 | } |
63bb59a3 ZD |
4093 | else |
4094 | s = redirect_edge_and_branch (e, dest); | |
072269d8 | 4095 | |
63bb59a3 | 4096 | if (s == e) |
072269d8 | 4097 | { |
63bb59a3 ZD |
4098 | /* Create arguments for the phi nodes, since the edge was not |
4099 | here before. */ | |
072269d8 | 4100 | for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi)) |
63bb59a3 | 4101 | add_phi_arg (phi, PHI_ARG_DEF (phi, succ->dest_idx), s); |
072269d8 | 4102 | } |
63bb59a3 | 4103 | else |
072269d8 | 4104 | { |
63bb59a3 ZD |
4105 | /* The source basic block might become a forwarder. We know |
4106 | that it was not a forwarder before, since it used to have | |
4107 | at least two outgoing edges, so we may just add it to | |
4108 | worklist. */ | |
23ab2e4e | 4109 | if (tree_forwarder_block_p (s->src, false)) |
63bb59a3 | 4110 | *(*worklist)++ = s->src; |
072269d8 | 4111 | } |
63bb59a3 | 4112 | } |
072269d8 | 4113 | |
63bb59a3 ZD |
4114 | if (seen_abnormal_edge) |
4115 | { | |
4116 | /* Move the labels to the new block, so that the redirection of | |
4117 | the abnormal edges works. */ | |
072269d8 | 4118 | |
63bb59a3 ZD |
4119 | bsi_to = bsi_start (dest); |
4120 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); ) | |
4121 | { | |
4122 | label = bsi_stmt (bsi); | |
4123 | gcc_assert (TREE_CODE (label) == LABEL_EXPR); | |
4124 | bsi_remove (&bsi); | |
4125 | bsi_insert_before (&bsi_to, label, BSI_CONTINUE_LINKING); | |
072269d8 KH |
4126 | } |
4127 | } | |
4128 | ||
63bb59a3 ZD |
4129 | /* Update the dominators. */ |
4130 | if (dom_info_available_p (CDI_DOMINATORS)) | |
4131 | { | |
4132 | basic_block dom, dombb, domdest; | |
4133 | ||
4134 | dombb = get_immediate_dominator (CDI_DOMINATORS, bb); | |
4135 | domdest = get_immediate_dominator (CDI_DOMINATORS, dest); | |
4136 | if (domdest == bb) | |
4137 | { | |
4138 | /* Shortcut to avoid calling (relatively expensive) | |
4139 | nearest_common_dominator unless necessary. */ | |
4140 | dom = dombb; | |
4141 | } | |
4142 | else | |
4143 | dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb); | |
072269d8 | 4144 | |
63bb59a3 ZD |
4145 | set_immediate_dominator (CDI_DOMINATORS, dest, dom); |
4146 | } | |
6de9cd9a | 4147 | |
63bb59a3 ZD |
4148 | /* And kill the forwarder block. */ |
4149 | delete_basic_block (bb); | |
6de9cd9a | 4150 | |
63bb59a3 ZD |
4151 | return true; |
4152 | } | |
2abacef0 | 4153 | |
63bb59a3 | 4154 | /* Removes forwarder blocks. */ |
072269d8 | 4155 | |
6de9cd9a | 4156 | static bool |
63bb59a3 | 4157 | cleanup_forwarder_blocks (void) |
6de9cd9a | 4158 | { |
072269d8 | 4159 | basic_block bb; |
63bb59a3 | 4160 | bool changed = false; |
29cc8719 | 4161 | basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks); |
31864f59 | 4162 | basic_block *current = worklist; |
6de9cd9a DN |
4163 | |
4164 | FOR_EACH_BB (bb) | |
08445125 | 4165 | { |
23ab2e4e | 4166 | if (tree_forwarder_block_p (bb, false)) |
63bb59a3 | 4167 | *current++ = bb; |
08445125 | 4168 | } |
6de9cd9a | 4169 | |
63bb59a3 | 4170 | while (current != worklist) |
08445125 | 4171 | { |
31864f59 | 4172 | bb = *--current; |
63bb59a3 | 4173 | changed |= remove_forwarder_block (bb, ¤t); |
6de9cd9a | 4174 | } |
08445125 KH |
4175 | |
4176 | free (worklist); | |
63bb59a3 | 4177 | return changed; |
6de9cd9a DN |
4178 | } |
4179 | ||
23ab2e4e KH |
4180 | /* Merge the PHI nodes at BB into those at BB's sole successor. */ |
4181 | ||
4182 | static void | |
4183 | remove_forwarder_block_with_phi (basic_block bb) | |
4184 | { | |
4185 | edge succ = EDGE_SUCC (bb, 0); | |
4186 | basic_block dest = succ->dest; | |
28773f15 | 4187 | tree label; |
23ab2e4e | 4188 | basic_block dombb, domdest, dom; |
23ab2e4e KH |
4189 | |
4190 | /* We check for infinite loops already in tree_forwarder_block_p. | |
4191 | However it may happen that the infinite loop is created | |
4192 | afterwards due to removal of forwarders. */ | |
4193 | if (dest == bb) | |
4194 | return; | |
4195 | ||
4196 | /* If the destination block consists of a nonlocal label, do not | |
4197 | merge it. */ | |
28773f15 KH |
4198 | label = first_stmt (dest); |
4199 | if (label | |
4200 | && TREE_CODE (label) == LABEL_EXPR | |
4201 | && DECL_NONLOCAL (LABEL_EXPR_LABEL (label))) | |
4202 | return; | |
23ab2e4e KH |
4203 | |
4204 | /* Redirect each incoming edge to BB to DEST. */ | |
4205 | while (EDGE_COUNT (bb->preds) > 0) | |
4206 | { | |
4207 | edge e = EDGE_PRED (bb, 0), s; | |
4208 | tree phi; | |
4209 | ||
4210 | s = find_edge (e->src, dest); | |
4211 | if (s) | |
4212 | { | |
4213 | /* We already have an edge S from E->src to DEST. If S and | |
4214 | E->dest's sole successor edge have the same PHI arguments | |
4215 | at DEST, redirect S to DEST. */ | |
4216 | if (phi_alternatives_equal (dest, s, succ)) | |
4217 | { | |
4218 | e = redirect_edge_and_branch (e, dest); | |
4219 | PENDING_STMT (e) = NULL_TREE; | |
4220 | continue; | |
4221 | } | |
4222 | ||
a85ba5a8 | 4223 | /* PHI arguments are different. Create a forwarder block by |
23ab2e4e KH |
4224 | splitting E so that we can merge PHI arguments on E to |
4225 | DEST. */ | |
4226 | e = EDGE_SUCC (split_edge (e), 0); | |
4227 | } | |
4228 | ||
4229 | s = redirect_edge_and_branch (e, dest); | |
4230 | ||
4231 | /* redirect_edge_and_branch must not create a new edge. */ | |
4232 | gcc_assert (s == e); | |
4233 | ||
4234 | /* Add to the PHI nodes at DEST each PHI argument removed at the | |
4235 | destination of E. */ | |
4236 | for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi)) | |
4237 | { | |
4238 | tree def = PHI_ARG_DEF (phi, succ->dest_idx); | |
4239 | ||
4240 | if (TREE_CODE (def) == SSA_NAME) | |
4241 | { | |
4242 | tree var; | |
4243 | ||
4244 | /* If DEF is one of the results of PHI nodes removed during | |
4245 | redirection, replace it with the PHI argument that used | |
4246 | to be on E. */ | |
4247 | for (var = PENDING_STMT (e); var; var = TREE_CHAIN (var)) | |
4248 | { | |
4249 | tree old_arg = TREE_PURPOSE (var); | |
4250 | tree new_arg = TREE_VALUE (var); | |
4251 | ||
4252 | if (def == old_arg) | |
4253 | { | |
4254 | def = new_arg; | |
4255 | break; | |
4256 | } | |
4257 | } | |
4258 | } | |
4259 | ||
4260 | add_phi_arg (phi, def, s); | |
4261 | } | |
4262 | ||
4263 | PENDING_STMT (e) = NULL; | |
4264 | } | |
4265 | ||
4266 | /* Update the dominators. */ | |
4267 | dombb = get_immediate_dominator (CDI_DOMINATORS, bb); | |
4268 | domdest = get_immediate_dominator (CDI_DOMINATORS, dest); | |
4269 | if (domdest == bb) | |
4270 | { | |
4271 | /* Shortcut to avoid calling (relatively expensive) | |
4272 | nearest_common_dominator unless necessary. */ | |
4273 | dom = dombb; | |
4274 | } | |
4275 | else | |
4276 | dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb); | |
4277 | ||
4278 | set_immediate_dominator (CDI_DOMINATORS, dest, dom); | |
4279 | ||
4280 | /* Remove BB since all of BB's incoming edges have been redirected | |
4281 | to DEST. */ | |
4282 | delete_basic_block (bb); | |
4283 | } | |
4284 | ||
a85ba5a8 KH |
4285 | /* This pass merges PHI nodes if one feeds into another. For example, |
4286 | suppose we have the following: | |
23ab2e4e KH |
4287 | |
4288 | goto <bb 9> (<L9>); | |
4289 | ||
4290 | <L8>:; | |
4291 | tem_17 = foo (); | |
4292 | ||
4293 | # tem_6 = PHI <tem_17(8), tem_23(7)>; | |
4294 | <L9>:; | |
4295 | ||
4296 | # tem_3 = PHI <tem_6(9), tem_2(5)>; | |
4297 | <L10>:; | |
4298 | ||
4299 | Then we merge the first PHI node into the second one like so: | |
4300 | ||
4301 | goto <bb 9> (<L10>); | |
4302 | ||
4303 | <L8>:; | |
4304 | tem_17 = foo (); | |
4305 | ||
4306 | # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>; | |
4307 | <L10>:; | |
4308 | */ | |
4309 | ||
4310 | static void | |
4311 | merge_phi_nodes (void) | |
4312 | { | |
4313 | basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks); | |
4314 | basic_block *current = worklist; | |
4315 | basic_block bb; | |
4316 | ||
4317 | calculate_dominance_info (CDI_DOMINATORS); | |
4318 | ||
4319 | /* Find all PHI nodes that we may be able to merge. */ | |
4320 | FOR_EACH_BB (bb) | |
4321 | { | |
4322 | basic_block dest; | |
4323 | ||
4324 | /* Look for a forwarder block with PHI nodes. */ | |
4325 | if (!tree_forwarder_block_p (bb, true)) | |
4326 | continue; | |
4327 | ||
4328 | dest = EDGE_SUCC (bb, 0)->dest; | |
4329 | ||
4330 | /* We have to feed into another basic block with PHI | |
4331 | nodes. */ | |
4332 | if (!phi_nodes (dest) | |
4333 | /* We don't want to deal with a basic block with | |
4334 | abnormal edges. */ | |
4335 | || has_abnormal_incoming_edge_p (bb)) | |
4336 | continue; | |
4337 | ||
4338 | if (!dominated_by_p (CDI_DOMINATORS, dest, bb)) | |
4339 | { | |
4340 | /* If BB does not dominate DEST, then the PHI nodes at | |
4341 | DEST must be the only users of the results of the PHI | |
4342 | nodes at BB. */ | |
4343 | *current++ = bb; | |
4344 | } | |
4345 | } | |
4346 | ||
4347 | /* Now let's drain WORKLIST. */ | |
4348 | while (current != worklist) | |
4349 | { | |
4350 | bb = *--current; | |
4351 | remove_forwarder_block_with_phi (bb); | |
4352 | } | |
4353 | ||
4354 | free (worklist); | |
4355 | } | |
4356 | ||
4357 | static bool | |
4358 | gate_merge_phi (void) | |
4359 | { | |
4360 | return 1; | |
4361 | } | |
4362 | ||
4363 | struct tree_opt_pass pass_merge_phi = { | |
4364 | "mergephi", /* name */ | |
4365 | gate_merge_phi, /* gate */ | |
4366 | merge_phi_nodes, /* execute */ | |
4367 | NULL, /* sub */ | |
4368 | NULL, /* next */ | |
4369 | 0, /* static_pass_number */ | |
4370 | TV_TREE_MERGE_PHI, /* tv_id */ | |
4371 | PROP_cfg | PROP_ssa, /* properties_required */ | |
4372 | 0, /* properties_provided */ | |
4373 | 0, /* properties_destroyed */ | |
4374 | 0, /* todo_flags_start */ | |
4375 | TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */ | |
4376 | | TODO_verify_ssa, | |
4377 | 0 /* letter */ | |
4378 | }; | |
4379 | ||
6de9cd9a DN |
4380 | /* Return a non-special label in the head of basic block BLOCK. |
4381 | Create one if it doesn't exist. */ | |
4382 | ||
d7621d3c | 4383 | tree |
6de9cd9a DN |
4384 | tree_block_label (basic_block bb) |
4385 | { | |
4386 | block_stmt_iterator i, s = bsi_start (bb); | |
4387 | bool first = true; | |
4388 | tree label, stmt; | |
4389 | ||
4390 | for (i = s; !bsi_end_p (i); first = false, bsi_next (&i)) | |
4391 | { | |
4392 | stmt = bsi_stmt (i); | |
4393 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
4394 | break; | |
4395 | label = LABEL_EXPR_LABEL (stmt); | |
4396 | if (!DECL_NONLOCAL (label)) | |
4397 | { | |
4398 | if (!first) | |
4399 | bsi_move_before (&i, &s); | |
4400 | return label; | |
4401 | } | |
4402 | } | |
4403 | ||
4404 | label = create_artificial_label (); | |
4405 | stmt = build1 (LABEL_EXPR, void_type_node, label); | |
4406 | bsi_insert_before (&s, stmt, BSI_NEW_STMT); | |
4407 | return label; | |
4408 | } | |
4409 | ||
4410 | ||
4411 | /* Attempt to perform edge redirection by replacing a possibly complex | |
4412 | jump instruction by a goto or by removing the jump completely. | |
4413 | This can apply only if all edges now point to the same block. The | |
4414 | parameters and return values are equivalent to | |
4415 | redirect_edge_and_branch. */ | |
4416 | ||
4417 | static edge | |
4418 | tree_try_redirect_by_replacing_jump (edge e, basic_block target) | |
4419 | { | |
4420 | basic_block src = e->src; | |
6de9cd9a DN |
4421 | block_stmt_iterator b; |
4422 | tree stmt; | |
6de9cd9a | 4423 | |
07b43a87 KH |
4424 | /* We can replace or remove a complex jump only when we have exactly |
4425 | two edges. */ | |
4426 | if (EDGE_COUNT (src->succs) != 2 | |
4427 | /* Verify that all targets will be TARGET. Specifically, the | |
4428 | edge that is not E must also go to TARGET. */ | |
4429 | || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target) | |
6de9cd9a DN |
4430 | return NULL; |
4431 | ||
4432 | b = bsi_last (src); | |
4433 | if (bsi_end_p (b)) | |
4434 | return NULL; | |
4435 | stmt = bsi_stmt (b); | |
4436 | ||
4437 | if (TREE_CODE (stmt) == COND_EXPR | |
4438 | || TREE_CODE (stmt) == SWITCH_EXPR) | |
4439 | { | |
4440 | bsi_remove (&b); | |
4441 | e = ssa_redirect_edge (e, target); | |
4442 | e->flags = EDGE_FALLTHRU; | |
4443 | return e; | |
4444 | } | |
4445 | ||
4446 | return NULL; | |
4447 | } | |
4448 | ||
4449 | ||
4450 | /* Redirect E to DEST. Return NULL on failure. Otherwise, return the | |
4451 | edge representing the redirected branch. */ | |
4452 | ||
4453 | static edge | |
4454 | tree_redirect_edge_and_branch (edge e, basic_block dest) | |
4455 | { | |
4456 | basic_block bb = e->src; | |
4457 | block_stmt_iterator bsi; | |
4458 | edge ret; | |
4459 | tree label, stmt; | |
4460 | ||
4461 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) | |
4462 | return NULL; | |
4463 | ||
4464 | if (e->src != ENTRY_BLOCK_PTR | |
4465 | && (ret = tree_try_redirect_by_replacing_jump (e, dest))) | |
4466 | return ret; | |
4467 | ||
4468 | if (e->dest == dest) | |
4469 | return NULL; | |
4470 | ||
4471 | label = tree_block_label (dest); | |
4472 | ||
4473 | bsi = bsi_last (bb); | |
4474 | stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi); | |
4475 | ||
4476 | switch (stmt ? TREE_CODE (stmt) : ERROR_MARK) | |
4477 | { | |
4478 | case COND_EXPR: | |
4479 | stmt = (e->flags & EDGE_TRUE_VALUE | |
4480 | ? COND_EXPR_THEN (stmt) | |
4481 | : COND_EXPR_ELSE (stmt)); | |
4482 | GOTO_DESTINATION (stmt) = label; | |
4483 | break; | |
4484 | ||
4485 | case GOTO_EXPR: | |
4486 | /* No non-abnormal edges should lead from a non-simple goto, and | |
4487 | simple ones should be represented implicitly. */ | |
1e128c5f | 4488 | gcc_unreachable (); |
6de9cd9a DN |
4489 | |
4490 | case SWITCH_EXPR: | |
4491 | { | |
d6be0d7f | 4492 | tree cases = get_cases_for_edge (e, stmt); |
6de9cd9a | 4493 | |
d6be0d7f JL |
4494 | /* If we have a list of cases associated with E, then use it |
4495 | as it's a lot faster than walking the entire case vector. */ | |
4496 | if (cases) | |
6de9cd9a | 4497 | { |
4edbbd3f | 4498 | edge e2 = find_edge (e->src, dest); |
d6be0d7f JL |
4499 | tree last, first; |
4500 | ||
4501 | first = cases; | |
4502 | while (cases) | |
4503 | { | |
4504 | last = cases; | |
4505 | CASE_LABEL (cases) = label; | |
4506 | cases = TREE_CHAIN (cases); | |
4507 | } | |
4508 | ||
4509 | /* If there was already an edge in the CFG, then we need | |
4510 | to move all the cases associated with E to E2. */ | |
4511 | if (e2) | |
4512 | { | |
4513 | tree cases2 = get_cases_for_edge (e2, stmt); | |
4514 | ||
4515 | TREE_CHAIN (last) = TREE_CHAIN (cases2); | |
4516 | TREE_CHAIN (cases2) = first; | |
4517 | } | |
6de9cd9a | 4518 | } |
92b6dff3 JL |
4519 | else |
4520 | { | |
d6be0d7f JL |
4521 | tree vec = SWITCH_LABELS (stmt); |
4522 | size_t i, n = TREE_VEC_LENGTH (vec); | |
4523 | ||
4524 | for (i = 0; i < n; i++) | |
4525 | { | |
4526 | tree elt = TREE_VEC_ELT (vec, i); | |
4527 | ||
4528 | if (label_to_block (CASE_LABEL (elt)) == e->dest) | |
4529 | CASE_LABEL (elt) = label; | |
4530 | } | |
92b6dff3 | 4531 | } |
d6be0d7f | 4532 | |
92b6dff3 | 4533 | break; |
6de9cd9a | 4534 | } |
6de9cd9a DN |
4535 | |
4536 | case RETURN_EXPR: | |
4537 | bsi_remove (&bsi); | |
4538 | e->flags |= EDGE_FALLTHRU; | |
4539 | break; | |
4540 | ||
4541 | default: | |
4542 | /* Otherwise it must be a fallthru edge, and we don't need to | |
4543 | do anything besides redirecting it. */ | |
1e128c5f | 4544 | gcc_assert (e->flags & EDGE_FALLTHRU); |
6de9cd9a DN |
4545 | break; |
4546 | } | |
4547 | ||
4548 | /* Update/insert PHI nodes as necessary. */ | |
4549 | ||
4550 | /* Now update the edges in the CFG. */ | |
4551 | e = ssa_redirect_edge (e, dest); | |
4552 | ||
4553 | return e; | |
4554 | } | |
4555 | ||
4556 | ||
4557 | /* Simple wrapper, as we can always redirect fallthru edges. */ | |
4558 | ||
4559 | static basic_block | |
4560 | tree_redirect_edge_and_branch_force (edge e, basic_block dest) | |
4561 | { | |
4562 | e = tree_redirect_edge_and_branch (e, dest); | |
1e128c5f | 4563 | gcc_assert (e); |
6de9cd9a DN |
4564 | |
4565 | return NULL; | |
4566 | } | |
4567 | ||
4568 | ||
4569 | /* Splits basic block BB after statement STMT (but at least after the | |
4570 | labels). If STMT is NULL, BB is split just after the labels. */ | |
4571 | ||
4572 | static basic_block | |
4573 | tree_split_block (basic_block bb, void *stmt) | |
4574 | { | |
4575 | block_stmt_iterator bsi, bsi_tgt; | |
4576 | tree act; | |
4577 | basic_block new_bb; | |
4578 | edge e; | |
628f6a4e | 4579 | edge_iterator ei; |
6de9cd9a DN |
4580 | |
4581 | new_bb = create_empty_bb (bb); | |
4582 | ||
4583 | /* Redirect the outgoing edges. */ | |
628f6a4e BE |
4584 | new_bb->succs = bb->succs; |
4585 | bb->succs = NULL; | |
4586 | FOR_EACH_EDGE (e, ei, new_bb->succs) | |
6de9cd9a DN |
4587 | e->src = new_bb; |
4588 | ||
4589 | if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR) | |
4590 | stmt = NULL; | |
4591 | ||
4592 | /* Move everything from BSI to the new basic block. */ | |
4593 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
4594 | { | |
4595 | act = bsi_stmt (bsi); | |
4596 | if (TREE_CODE (act) == LABEL_EXPR) | |
4597 | continue; | |
4598 | ||
4599 | if (!stmt) | |
4600 | break; | |
4601 | ||
4602 | if (stmt == act) | |
4603 | { | |
4604 | bsi_next (&bsi); | |
4605 | break; | |
4606 | } | |
4607 | } | |
4608 | ||
4609 | bsi_tgt = bsi_start (new_bb); | |
4610 | while (!bsi_end_p (bsi)) | |
4611 | { | |
4612 | act = bsi_stmt (bsi); | |
4613 | bsi_remove (&bsi); | |
4614 | bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT); | |
4615 | } | |
4616 | ||
4617 | return new_bb; | |
4618 | } | |
4619 | ||
4620 | ||
4621 | /* Moves basic block BB after block AFTER. */ | |
4622 | ||
4623 | static bool | |
4624 | tree_move_block_after (basic_block bb, basic_block after) | |
4625 | { | |
4626 | if (bb->prev_bb == after) | |
4627 | return true; | |
4628 | ||
4629 | unlink_block (bb); | |
4630 | link_block (bb, after); | |
4631 | ||
4632 | return true; | |
4633 | } | |
4634 | ||
4635 | ||
4636 | /* Return true if basic_block can be duplicated. */ | |
4637 | ||
4638 | static bool | |
4639 | tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED) | |
4640 | { | |
4641 | return true; | |
4642 | } | |
4643 | ||
6de9cd9a DN |
4644 | /* Create a duplicate of the basic block BB. NOTE: This does not |
4645 | preserve SSA form. */ | |
4646 | ||
4647 | static basic_block | |
4648 | tree_duplicate_bb (basic_block bb) | |
4649 | { | |
4650 | basic_block new_bb; | |
4651 | block_stmt_iterator bsi, bsi_tgt; | |
4c124b4c AM |
4652 | tree phi, val; |
4653 | ssa_op_iter op_iter; | |
6de9cd9a DN |
4654 | |
4655 | new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb); | |
b0382c67 | 4656 | |
42759f1e ZD |
4657 | /* First copy the phi nodes. We do not copy phi node arguments here, |
4658 | since the edges are not ready yet. Keep the chain of phi nodes in | |
4659 | the same order, so that we can add them later. */ | |
bb29d951 | 4660 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
b0382c67 ZD |
4661 | { |
4662 | mark_for_rewrite (PHI_RESULT (phi)); | |
42759f1e | 4663 | create_phi_node (PHI_RESULT (phi), new_bb); |
b0382c67 | 4664 | } |
5ae71719 | 4665 | set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb))); |
b0382c67 | 4666 | |
6de9cd9a DN |
4667 | bsi_tgt = bsi_start (new_bb); |
4668 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
4669 | { | |
4670 | tree stmt = bsi_stmt (bsi); | |
5f240ec4 | 4671 | tree copy; |
6de9cd9a DN |
4672 | |
4673 | if (TREE_CODE (stmt) == LABEL_EXPR) | |
4674 | continue; | |
4675 | ||
b0382c67 ZD |
4676 | /* Record the definitions. */ |
4677 | get_stmt_operands (stmt); | |
4678 | ||
4c124b4c AM |
4679 | FOR_EACH_SSA_TREE_OPERAND (val, stmt, op_iter, SSA_OP_ALL_DEFS) |
4680 | mark_for_rewrite (val); | |
b0382c67 | 4681 | |
5f240ec4 ZD |
4682 | copy = unshare_expr (stmt); |
4683 | ||
4684 | /* Copy also the virtual operands. */ | |
4685 | get_stmt_ann (copy); | |
4686 | copy_virtual_operands (copy, stmt); | |
4687 | ||
4688 | bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT); | |
6de9cd9a DN |
4689 | } |
4690 | ||
4691 | return new_bb; | |
4692 | } | |
4693 | ||
42759f1e ZD |
4694 | /* Basic block BB_COPY was created by code duplication. Add phi node |
4695 | arguments for edges going out of BB_COPY. The blocks that were | |
4696 | duplicated have rbi->duplicated set to one. */ | |
4697 | ||
4698 | void | |
4699 | add_phi_args_after_copy_bb (basic_block bb_copy) | |
4700 | { | |
4701 | basic_block bb, dest; | |
4702 | edge e, e_copy; | |
628f6a4e | 4703 | edge_iterator ei; |
42759f1e ZD |
4704 | tree phi, phi_copy, phi_next, def; |
4705 | ||
4706 | bb = bb_copy->rbi->original; | |
4707 | ||
628f6a4e | 4708 | FOR_EACH_EDGE (e_copy, ei, bb_copy->succs) |
42759f1e ZD |
4709 | { |
4710 | if (!phi_nodes (e_copy->dest)) | |
4711 | continue; | |
4712 | ||
4713 | if (e_copy->dest->rbi->duplicated) | |
4714 | dest = e_copy->dest->rbi->original; | |
4715 | else | |
4716 | dest = e_copy->dest; | |
4717 | ||
4718 | e = find_edge (bb, dest); | |
4719 | if (!e) | |
4720 | { | |
4721 | /* During loop unrolling the target of the latch edge is copied. | |
4722 | In this case we are not looking for edge to dest, but to | |
4723 | duplicated block whose original was dest. */ | |
628f6a4e | 4724 | FOR_EACH_EDGE (e, ei, bb->succs) |
42759f1e ZD |
4725 | if (e->dest->rbi->duplicated |
4726 | && e->dest->rbi->original == dest) | |
4727 | break; | |
4728 | ||
4729 | gcc_assert (e != NULL); | |
4730 | } | |
4731 | ||
4732 | for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest); | |
4733 | phi; | |
eaf0dc02 | 4734 | phi = phi_next, phi_copy = PHI_CHAIN (phi_copy)) |
42759f1e | 4735 | { |
eaf0dc02 | 4736 | phi_next = PHI_CHAIN (phi); |
42759f1e ZD |
4737 | |
4738 | gcc_assert (PHI_RESULT (phi) == PHI_RESULT (phi_copy)); | |
4739 | def = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
d2e398df | 4740 | add_phi_arg (phi_copy, def, e_copy); |
42759f1e ZD |
4741 | } |
4742 | } | |
4743 | } | |
4744 | ||
4745 | /* Blocks in REGION_COPY array of length N_REGION were created by | |
4746 | duplication of basic blocks. Add phi node arguments for edges | |
4747 | going from these blocks. */ | |
4748 | ||
4749 | void | |
4750 | add_phi_args_after_copy (basic_block *region_copy, unsigned n_region) | |
4751 | { | |
4752 | unsigned i; | |
4753 | ||
4754 | for (i = 0; i < n_region; i++) | |
4755 | region_copy[i]->rbi->duplicated = 1; | |
4756 | ||
4757 | for (i = 0; i < n_region; i++) | |
4758 | add_phi_args_after_copy_bb (region_copy[i]); | |
4759 | ||
4760 | for (i = 0; i < n_region; i++) | |
4761 | region_copy[i]->rbi->duplicated = 0; | |
4762 | } | |
4763 | ||
4764 | /* Maps the old ssa name FROM_NAME to TO_NAME. */ | |
4765 | ||
4766 | struct ssa_name_map_entry | |
4767 | { | |
4768 | tree from_name; | |
4769 | tree to_name; | |
4770 | }; | |
4771 | ||
4772 | /* Hash function for ssa_name_map_entry. */ | |
4773 | ||
4774 | static hashval_t | |
4775 | ssa_name_map_entry_hash (const void *entry) | |
4776 | { | |
4777 | const struct ssa_name_map_entry *en = entry; | |
4778 | return SSA_NAME_VERSION (en->from_name); | |
4779 | } | |
4780 | ||
4781 | /* Equality function for ssa_name_map_entry. */ | |
4782 | ||
4783 | static int | |
4784 | ssa_name_map_entry_eq (const void *in_table, const void *ssa_name) | |
4785 | { | |
4786 | const struct ssa_name_map_entry *en = in_table; | |
4787 | ||
4788 | return en->from_name == ssa_name; | |
4789 | } | |
4790 | ||
4791 | /* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping | |
4792 | to MAP. */ | |
4793 | ||
4794 | void | |
4795 | allocate_ssa_names (bitmap definitions, htab_t *map) | |
4796 | { | |
4797 | tree name; | |
4798 | struct ssa_name_map_entry *entry; | |
4799 | PTR *slot; | |
4800 | unsigned ver; | |
87c476a2 | 4801 | bitmap_iterator bi; |
42759f1e ZD |
4802 | |
4803 | if (!*map) | |
4804 | *map = htab_create (10, ssa_name_map_entry_hash, | |
4805 | ssa_name_map_entry_eq, free); | |
87c476a2 | 4806 | EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi) |
42759f1e ZD |
4807 | { |
4808 | name = ssa_name (ver); | |
4809 | slot = htab_find_slot_with_hash (*map, name, SSA_NAME_VERSION (name), | |
4810 | INSERT); | |
4811 | if (*slot) | |
4812 | entry = *slot; | |
4813 | else | |
4814 | { | |
4815 | entry = xmalloc (sizeof (struct ssa_name_map_entry)); | |
4816 | entry->from_name = name; | |
4817 | *slot = entry; | |
4818 | } | |
4819 | entry->to_name = duplicate_ssa_name (name, SSA_NAME_DEF_STMT (name)); | |
87c476a2 | 4820 | } |
42759f1e ZD |
4821 | } |
4822 | ||
4823 | /* Rewrite the definition DEF in statement STMT to new ssa name as specified | |
4824 | by the mapping MAP. */ | |
4825 | ||
4826 | static void | |
4827 | rewrite_to_new_ssa_names_def (def_operand_p def, tree stmt, htab_t map) | |
4828 | { | |
4829 | tree name = DEF_FROM_PTR (def); | |
4830 | struct ssa_name_map_entry *entry; | |
4831 | ||
4832 | gcc_assert (TREE_CODE (name) == SSA_NAME); | |
4833 | ||
4834 | entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name)); | |
4835 | if (!entry) | |
4836 | return; | |
4837 | ||
4838 | SET_DEF (def, entry->to_name); | |
4839 | SSA_NAME_DEF_STMT (entry->to_name) = stmt; | |
4840 | } | |
4841 | ||
4842 | /* Rewrite the USE to new ssa name as specified by the mapping MAP. */ | |
4843 | ||
4844 | static void | |
4845 | rewrite_to_new_ssa_names_use (use_operand_p use, htab_t map) | |
4846 | { | |
4847 | tree name = USE_FROM_PTR (use); | |
4848 | struct ssa_name_map_entry *entry; | |
4849 | ||
4850 | if (TREE_CODE (name) != SSA_NAME) | |
4851 | return; | |
4852 | ||
4853 | entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name)); | |
4854 | if (!entry) | |
4855 | return; | |
4856 | ||
4857 | SET_USE (use, entry->to_name); | |
4858 | } | |
4859 | ||
4860 | /* Rewrite the ssa names in basic block BB to new ones as specified by the | |
4861 | mapping MAP. */ | |
4862 | ||
4863 | void | |
4864 | rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map) | |
4865 | { | |
4866 | unsigned i; | |
4867 | edge e; | |
628f6a4e | 4868 | edge_iterator ei; |
42759f1e ZD |
4869 | tree phi, stmt; |
4870 | block_stmt_iterator bsi; | |
4871 | use_optype uses; | |
4872 | vuse_optype vuses; | |
4873 | def_optype defs; | |
4874 | v_may_def_optype v_may_defs; | |
4875 | v_must_def_optype v_must_defs; | |
4876 | stmt_ann_t ann; | |
4877 | ||
628f6a4e | 4878 | FOR_EACH_EDGE (e, ei, bb->preds) |
42759f1e ZD |
4879 | if (e->flags & EDGE_ABNORMAL) |
4880 | break; | |
4881 | ||
bb29d951 | 4882 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
42759f1e ZD |
4883 | { |
4884 | rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi), phi, map); | |
4885 | if (e) | |
4886 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1; | |
4887 | } | |
4888 | ||
4889 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
4890 | { | |
4891 | stmt = bsi_stmt (bsi); | |
4892 | get_stmt_operands (stmt); | |
4893 | ann = stmt_ann (stmt); | |
4894 | ||
4895 | uses = USE_OPS (ann); | |
4896 | for (i = 0; i < NUM_USES (uses); i++) | |
4897 | rewrite_to_new_ssa_names_use (USE_OP_PTR (uses, i), map); | |
4898 | ||
4899 | defs = DEF_OPS (ann); | |
4900 | for (i = 0; i < NUM_DEFS (defs); i++) | |
4901 | rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs, i), stmt, map); | |
4902 | ||
4903 | vuses = VUSE_OPS (ann); | |
4904 | for (i = 0; i < NUM_VUSES (vuses); i++) | |
4905 | rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses, i), map); | |
4906 | ||
4907 | v_may_defs = V_MAY_DEF_OPS (ann); | |
4908 | for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++) | |
4909 | { | |
4910 | rewrite_to_new_ssa_names_use | |
4911 | (V_MAY_DEF_OP_PTR (v_may_defs, i), map); | |
4912 | rewrite_to_new_ssa_names_def | |
4913 | (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt, map); | |
4914 | } | |
4915 | ||
4916 | v_must_defs = V_MUST_DEF_OPS (ann); | |
4917 | for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++) | |
52328bf6 DB |
4918 | { |
4919 | rewrite_to_new_ssa_names_def | |
4920 | (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt, map); | |
4921 | rewrite_to_new_ssa_names_use | |
4922 | (V_MUST_DEF_KILL_PTR (v_must_defs, i), map); | |
4923 | } | |
42759f1e ZD |
4924 | } |
4925 | ||
628f6a4e | 4926 | FOR_EACH_EDGE (e, ei, bb->succs) |
bb29d951 | 4927 | for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi)) |
42759f1e ZD |
4928 | { |
4929 | rewrite_to_new_ssa_names_use | |
4930 | (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), map); | |
4931 | ||
4932 | if (e->flags & EDGE_ABNORMAL) | |
4933 | { | |
4934 | tree op = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
4935 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1; | |
4936 | } | |
4937 | } | |
4938 | } | |
4939 | ||
4940 | /* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified | |
4941 | by the mapping MAP. */ | |
4942 | ||
4943 | void | |
4944 | rewrite_to_new_ssa_names (basic_block *region, unsigned n_region, htab_t map) | |
4945 | { | |
4946 | unsigned r; | |
4947 | ||
4948 | for (r = 0; r < n_region; r++) | |
4949 | rewrite_to_new_ssa_names_bb (region[r], map); | |
4950 | } | |
4951 | ||
4952 | /* Duplicates a REGION (set of N_REGION basic blocks) with just a single | |
4953 | important exit edge EXIT. By important we mean that no SSA name defined | |
4954 | inside region is live over the other exit edges of the region. All entry | |
4955 | edges to the region must go to ENTRY->dest. The edge ENTRY is redirected | |
4956 | to the duplicate of the region. SSA form, dominance and loop information | |
4957 | is updated. The new basic blocks are stored to REGION_COPY in the same | |
4958 | order as they had in REGION, provided that REGION_COPY is not NULL. | |
4959 | The function returns false if it is unable to copy the region, | |
4960 | true otherwise. */ | |
4961 | ||
4962 | bool | |
4963 | tree_duplicate_sese_region (edge entry, edge exit, | |
4964 | basic_block *region, unsigned n_region, | |
4965 | basic_block *region_copy) | |
4966 | { | |
4967 | unsigned i, n_doms, ver; | |
4968 | bool free_region_copy = false, copying_header = false; | |
4969 | struct loop *loop = entry->dest->loop_father; | |
4970 | edge exit_copy; | |
4971 | bitmap definitions; | |
71882046 | 4972 | tree phi; |
42759f1e ZD |
4973 | basic_block *doms; |
4974 | htab_t ssa_name_map = NULL; | |
4975 | edge redirected; | |
87c476a2 | 4976 | bitmap_iterator bi; |
42759f1e ZD |
4977 | |
4978 | if (!can_copy_bbs_p (region, n_region)) | |
4979 | return false; | |
4980 | ||
4981 | /* Some sanity checking. Note that we do not check for all possible | |
4982 | missuses of the functions. I.e. if you ask to copy something weird, | |
4983 | it will work, but the state of structures probably will not be | |
4984 | correct. */ | |
4985 | ||
4986 | for (i = 0; i < n_region; i++) | |
4987 | { | |
4988 | /* We do not handle subloops, i.e. all the blocks must belong to the | |
4989 | same loop. */ | |
4990 | if (region[i]->loop_father != loop) | |
4991 | return false; | |
4992 | ||
4993 | if (region[i] != entry->dest | |
4994 | && region[i] == loop->header) | |
4995 | return false; | |
4996 | } | |
4997 | ||
4998 | loop->copy = loop; | |
4999 | ||
5000 | /* In case the function is used for loop header copying (which is the primary | |
5001 | use), ensure that EXIT and its copy will be new latch and entry edges. */ | |
5002 | if (loop->header == entry->dest) | |
5003 | { | |
5004 | copying_header = true; | |
5005 | loop->copy = loop->outer; | |
5006 | ||
5007 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src)) | |
5008 | return false; | |
5009 | ||
5010 | for (i = 0; i < n_region; i++) | |
5011 | if (region[i] != exit->src | |
5012 | && dominated_by_p (CDI_DOMINATORS, region[i], exit->src)) | |
5013 | return false; | |
5014 | } | |
5015 | ||
5016 | if (!region_copy) | |
5017 | { | |
5018 | region_copy = xmalloc (sizeof (basic_block) * n_region); | |
5019 | free_region_copy = true; | |
5020 | } | |
5021 | ||
5022 | gcc_assert (!any_marked_for_rewrite_p ()); | |
5023 | ||
5024 | /* Record blocks outside the region that are duplicated by something | |
5025 | inside. */ | |
5026 | doms = xmalloc (sizeof (basic_block) * n_basic_blocks); | |
5027 | n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms); | |
5028 | ||
5029 | copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop); | |
5030 | definitions = marked_ssa_names (); | |
5031 | ||
5032 | if (copying_header) | |
5033 | { | |
5034 | loop->header = exit->dest; | |
5035 | loop->latch = exit->src; | |
5036 | } | |
5037 | ||
5038 | /* Redirect the entry and add the phi node arguments. */ | |
5039 | redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy); | |
5040 | gcc_assert (redirected != NULL); | |
71882046 | 5041 | flush_pending_stmts (entry); |
42759f1e ZD |
5042 | |
5043 | /* Concerning updating of dominators: We must recount dominators | |
5044 | for entry block and its copy. Anything that is outside of the region, but | |
5045 | was dominated by something inside needs recounting as well. */ | |
5046 | set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src); | |
5047 | doms[n_doms++] = entry->dest->rbi->original; | |
5048 | iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms); | |
5049 | free (doms); | |
5050 | ||
5051 | /* Add the other phi node arguments. */ | |
5052 | add_phi_args_after_copy (region_copy, n_region); | |
5053 | ||
5054 | /* Add phi nodes for definitions at exit. TODO -- once we have immediate | |
5055 | uses, it should be possible to emit phi nodes just for definitions that | |
5056 | are used outside region. */ | |
87c476a2 | 5057 | EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi) |
42759f1e ZD |
5058 | { |
5059 | tree name = ssa_name (ver); | |
5060 | ||
5061 | phi = create_phi_node (name, exit->dest); | |
d2e398df KH |
5062 | add_phi_arg (phi, name, exit); |
5063 | add_phi_arg (phi, name, exit_copy); | |
42759f1e ZD |
5064 | |
5065 | SSA_NAME_DEF_STMT (name) = phi; | |
87c476a2 | 5066 | } |
42759f1e ZD |
5067 | |
5068 | /* And create new definitions inside region and its copy. TODO -- once we | |
5069 | have immediate uses, it might be better to leave definitions in region | |
5070 | unchanged, create new ssa names for phi nodes on exit, and rewrite | |
5071 | the uses, to avoid changing the copied region. */ | |
5072 | allocate_ssa_names (definitions, &ssa_name_map); | |
5073 | rewrite_to_new_ssa_names (region, n_region, ssa_name_map); | |
5074 | allocate_ssa_names (definitions, &ssa_name_map); | |
5075 | rewrite_to_new_ssa_names (region_copy, n_region, ssa_name_map); | |
5076 | htab_delete (ssa_name_map); | |
5077 | ||
5078 | if (free_region_copy) | |
5079 | free (region_copy); | |
5080 | ||
5081 | unmark_all_for_rewrite (); | |
8bdbfff5 | 5082 | BITMAP_FREE (definitions); |
42759f1e ZD |
5083 | |
5084 | return true; | |
5085 | } | |
6de9cd9a DN |
5086 | |
5087 | /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */ | |
5088 | ||
5089 | void | |
5090 | dump_function_to_file (tree fn, FILE *file, int flags) | |
5091 | { | |
5092 | tree arg, vars, var; | |
5093 | bool ignore_topmost_bind = false, any_var = false; | |
5094 | basic_block bb; | |
5095 | tree chain; | |
5096 | ||
673fda6b | 5097 | fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2)); |
6de9cd9a DN |
5098 | |
5099 | arg = DECL_ARGUMENTS (fn); | |
5100 | while (arg) | |
5101 | { | |
5102 | print_generic_expr (file, arg, dump_flags); | |
5103 | if (TREE_CHAIN (arg)) | |
5104 | fprintf (file, ", "); | |
5105 | arg = TREE_CHAIN (arg); | |
5106 | } | |
5107 | fprintf (file, ")\n"); | |
5108 | ||
5109 | if (flags & TDF_RAW) | |
5110 | { | |
5111 | dump_node (fn, TDF_SLIM | flags, file); | |
5112 | return; | |
5113 | } | |
5114 | ||
5115 | /* When GIMPLE is lowered, the variables are no longer available in | |
5116 | BIND_EXPRs, so display them separately. */ | |
5117 | if (cfun && cfun->unexpanded_var_list) | |
5118 | { | |
5119 | ignore_topmost_bind = true; | |
5120 | ||
5121 | fprintf (file, "{\n"); | |
5122 | for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars)) | |
5123 | { | |
5124 | var = TREE_VALUE (vars); | |
5125 | ||
5126 | print_generic_decl (file, var, flags); | |
5127 | fprintf (file, "\n"); | |
5128 | ||
5129 | any_var = true; | |
5130 | } | |
5131 | } | |
5132 | ||
5133 | if (basic_block_info) | |
5134 | { | |
5135 | /* Make a CFG based dump. */ | |
878f99d2 | 5136 | check_bb_profile (ENTRY_BLOCK_PTR, file); |
6de9cd9a DN |
5137 | if (!ignore_topmost_bind) |
5138 | fprintf (file, "{\n"); | |
5139 | ||
5140 | if (any_var && n_basic_blocks) | |
5141 | fprintf (file, "\n"); | |
5142 | ||
5143 | FOR_EACH_BB (bb) | |
5144 | dump_generic_bb (file, bb, 2, flags); | |
5145 | ||
5146 | fprintf (file, "}\n"); | |
878f99d2 | 5147 | check_bb_profile (EXIT_BLOCK_PTR, file); |
6de9cd9a DN |
5148 | } |
5149 | else | |
5150 | { | |
5151 | int indent; | |
5152 | ||
5153 | /* Make a tree based dump. */ | |
5154 | chain = DECL_SAVED_TREE (fn); | |
5155 | ||
5156 | if (TREE_CODE (chain) == BIND_EXPR) | |
5157 | { | |
5158 | if (ignore_topmost_bind) | |
5159 | { | |
5160 | chain = BIND_EXPR_BODY (chain); | |
5161 | indent = 2; | |
5162 | } | |
5163 | else | |
5164 | indent = 0; | |
5165 | } | |
5166 | else | |
5167 | { | |
5168 | if (!ignore_topmost_bind) | |
5169 | fprintf (file, "{\n"); | |
5170 | indent = 2; | |
5171 | } | |
5172 | ||
5173 | if (any_var) | |
5174 | fprintf (file, "\n"); | |
5175 | ||
5176 | print_generic_stmt_indented (file, chain, flags, indent); | |
5177 | if (ignore_topmost_bind) | |
5178 | fprintf (file, "}\n"); | |
5179 | } | |
5180 | ||
5181 | fprintf (file, "\n\n"); | |
5182 | } | |
5183 | ||
5184 | ||
5185 | /* Pretty print of the loops intermediate representation. */ | |
5186 | static void print_loop (FILE *, struct loop *, int); | |
628f6a4e BE |
5187 | static void print_pred_bbs (FILE *, basic_block bb); |
5188 | static void print_succ_bbs (FILE *, basic_block bb); | |
6de9cd9a DN |
5189 | |
5190 | ||
5191 | /* Print the predecessors indexes of edge E on FILE. */ | |
5192 | ||
5193 | static void | |
628f6a4e | 5194 | print_pred_bbs (FILE *file, basic_block bb) |
6de9cd9a | 5195 | { |
628f6a4e BE |
5196 | edge e; |
5197 | edge_iterator ei; | |
5198 | ||
5199 | FOR_EACH_EDGE (e, ei, bb->preds) | |
6de9cd9a | 5200 | fprintf (file, "bb_%d", e->src->index); |
6de9cd9a DN |
5201 | } |
5202 | ||
5203 | ||
5204 | /* Print the successors indexes of edge E on FILE. */ | |
5205 | ||
5206 | static void | |
628f6a4e | 5207 | print_succ_bbs (FILE *file, basic_block bb) |
6de9cd9a | 5208 | { |
628f6a4e BE |
5209 | edge e; |
5210 | edge_iterator ei; | |
5211 | ||
5212 | FOR_EACH_EDGE (e, ei, bb->succs) | |
5213 | fprintf (file, "bb_%d", e->src->index); | |
6de9cd9a DN |
5214 | } |
5215 | ||
5216 | ||
5217 | /* Pretty print LOOP on FILE, indented INDENT spaces. */ | |
5218 | ||
5219 | static void | |
5220 | print_loop (FILE *file, struct loop *loop, int indent) | |
5221 | { | |
5222 | char *s_indent; | |
5223 | basic_block bb; | |
5224 | ||
5225 | if (loop == NULL) | |
5226 | return; | |
5227 | ||
5228 | s_indent = (char *) alloca ((size_t) indent + 1); | |
5229 | memset ((void *) s_indent, ' ', (size_t) indent); | |
5230 | s_indent[indent] = '\0'; | |
5231 | ||
5232 | /* Print the loop's header. */ | |
5233 | fprintf (file, "%sloop_%d\n", s_indent, loop->num); | |
5234 | ||
5235 | /* Print the loop's body. */ | |
5236 | fprintf (file, "%s{\n", s_indent); | |
5237 | FOR_EACH_BB (bb) | |
5238 | if (bb->loop_father == loop) | |
5239 | { | |
5240 | /* Print the basic_block's header. */ | |
5241 | fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index); | |
628f6a4e | 5242 | print_pred_bbs (file, bb); |
6de9cd9a | 5243 | fprintf (file, "}, succs = {"); |
628f6a4e | 5244 | print_succ_bbs (file, bb); |
6de9cd9a DN |
5245 | fprintf (file, "})\n"); |
5246 | ||
5247 | /* Print the basic_block's body. */ | |
5248 | fprintf (file, "%s {\n", s_indent); | |
5249 | tree_dump_bb (bb, file, indent + 4); | |
5250 | fprintf (file, "%s }\n", s_indent); | |
5251 | } | |
5252 | ||
5253 | print_loop (file, loop->inner, indent + 2); | |
5254 | fprintf (file, "%s}\n", s_indent); | |
5255 | print_loop (file, loop->next, indent); | |
5256 | } | |
5257 | ||
5258 | ||
5259 | /* Follow a CFG edge from the entry point of the program, and on entry | |
5260 | of a loop, pretty print the loop structure on FILE. */ | |
5261 | ||
5262 | void | |
5263 | print_loop_ir (FILE *file) | |
5264 | { | |
5265 | basic_block bb; | |
5266 | ||
5267 | bb = BASIC_BLOCK (0); | |
5268 | if (bb && bb->loop_father) | |
5269 | print_loop (file, bb->loop_father, 0); | |
5270 | } | |
5271 | ||
5272 | ||
5273 | /* Debugging loops structure at tree level. */ | |
5274 | ||
5275 | void | |
5276 | debug_loop_ir (void) | |
5277 | { | |
5278 | print_loop_ir (stderr); | |
5279 | } | |
5280 | ||
5281 | ||
5282 | /* Return true if BB ends with a call, possibly followed by some | |
5283 | instructions that must stay with the call. Return false, | |
5284 | otherwise. */ | |
5285 | ||
5286 | static bool | |
5287 | tree_block_ends_with_call_p (basic_block bb) | |
5288 | { | |
5289 | block_stmt_iterator bsi = bsi_last (bb); | |
cd709752 | 5290 | return get_call_expr_in (bsi_stmt (bsi)) != NULL; |
6de9cd9a DN |
5291 | } |
5292 | ||
5293 | ||
5294 | /* Return true if BB ends with a conditional branch. Return false, | |
5295 | otherwise. */ | |
5296 | ||
5297 | static bool | |
5298 | tree_block_ends_with_condjump_p (basic_block bb) | |
5299 | { | |
5300 | tree stmt = tsi_stmt (bsi_last (bb).tsi); | |
5301 | return (TREE_CODE (stmt) == COND_EXPR); | |
5302 | } | |
5303 | ||
5304 | ||
5305 | /* Return true if we need to add fake edge to exit at statement T. | |
5306 | Helper function for tree_flow_call_edges_add. */ | |
5307 | ||
5308 | static bool | |
5309 | need_fake_edge_p (tree t) | |
5310 | { | |
cd709752 | 5311 | tree call; |
6de9cd9a DN |
5312 | |
5313 | /* NORETURN and LONGJMP calls already have an edge to exit. | |
5314 | CONST, PURE and ALWAYS_RETURN calls do not need one. | |
5315 | We don't currently check for CONST and PURE here, although | |
5316 | it would be a good idea, because those attributes are | |
5317 | figured out from the RTL in mark_constant_function, and | |
5318 | the counter incrementation code from -fprofile-arcs | |
5319 | leads to different results from -fbranch-probabilities. */ | |
cd709752 RH |
5320 | call = get_call_expr_in (t); |
5321 | if (call | |
6e14af16 | 5322 | && !(call_expr_flags (call) & (ECF_NORETURN | ECF_ALWAYS_RETURN))) |
6de9cd9a DN |
5323 | return true; |
5324 | ||
5325 | if (TREE_CODE (t) == ASM_EXPR | |
5326 | && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t))) | |
5327 | return true; | |
5328 | ||
5329 | return false; | |
5330 | } | |
5331 | ||
5332 | ||
5333 | /* Add fake edges to the function exit for any non constant and non | |
5334 | noreturn calls, volatile inline assembly in the bitmap of blocks | |
5335 | specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return | |
5336 | the number of blocks that were split. | |
5337 | ||
5338 | The goal is to expose cases in which entering a basic block does | |
5339 | not imply that all subsequent instructions must be executed. */ | |
5340 | ||
5341 | static int | |
5342 | tree_flow_call_edges_add (sbitmap blocks) | |
5343 | { | |
5344 | int i; | |
5345 | int blocks_split = 0; | |
5346 | int last_bb = last_basic_block; | |
5347 | bool check_last_block = false; | |
5348 | ||
5349 | if (n_basic_blocks == 0) | |
5350 | return 0; | |
5351 | ||
5352 | if (! blocks) | |
5353 | check_last_block = true; | |
5354 | else | |
5355 | check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index); | |
5356 | ||
5357 | /* In the last basic block, before epilogue generation, there will be | |
5358 | a fallthru edge to EXIT. Special care is required if the last insn | |
5359 | of the last basic block is a call because make_edge folds duplicate | |
5360 | edges, which would result in the fallthru edge also being marked | |
5361 | fake, which would result in the fallthru edge being removed by | |
5362 | remove_fake_edges, which would result in an invalid CFG. | |
5363 | ||
5364 | Moreover, we can't elide the outgoing fake edge, since the block | |
5365 | profiler needs to take this into account in order to solve the minimal | |
5366 | spanning tree in the case that the call doesn't return. | |
5367 | ||
5368 | Handle this by adding a dummy instruction in a new last basic block. */ | |
5369 | if (check_last_block) | |
5370 | { | |
5371 | basic_block bb = EXIT_BLOCK_PTR->prev_bb; | |
5372 | block_stmt_iterator bsi = bsi_last (bb); | |
5373 | tree t = NULL_TREE; | |
5374 | if (!bsi_end_p (bsi)) | |
5375 | t = bsi_stmt (bsi); | |
5376 | ||
5377 | if (need_fake_edge_p (t)) | |
5378 | { | |
5379 | edge e; | |
5380 | ||
9ff3d2de JL |
5381 | e = find_edge (bb, EXIT_BLOCK_PTR); |
5382 | if (e) | |
5383 | { | |
5384 | bsi_insert_on_edge (e, build_empty_stmt ()); | |
5385 | bsi_commit_edge_inserts (); | |
5386 | } | |
6de9cd9a DN |
5387 | } |
5388 | } | |
5389 | ||
5390 | /* Now add fake edges to the function exit for any non constant | |
5391 | calls since there is no way that we can determine if they will | |
5392 | return or not... */ | |
5393 | for (i = 0; i < last_bb; i++) | |
5394 | { | |
5395 | basic_block bb = BASIC_BLOCK (i); | |
5396 | block_stmt_iterator bsi; | |
5397 | tree stmt, last_stmt; | |
5398 | ||
5399 | if (!bb) | |
5400 | continue; | |
5401 | ||
5402 | if (blocks && !TEST_BIT (blocks, i)) | |
5403 | continue; | |
5404 | ||
5405 | bsi = bsi_last (bb); | |
5406 | if (!bsi_end_p (bsi)) | |
5407 | { | |
5408 | last_stmt = bsi_stmt (bsi); | |
5409 | do | |
5410 | { | |
5411 | stmt = bsi_stmt (bsi); | |
5412 | if (need_fake_edge_p (stmt)) | |
5413 | { | |
5414 | edge e; | |
5415 | /* The handling above of the final block before the | |
5416 | epilogue should be enough to verify that there is | |
5417 | no edge to the exit block in CFG already. | |
5418 | Calling make_edge in such case would cause us to | |
5419 | mark that edge as fake and remove it later. */ | |
5420 | #ifdef ENABLE_CHECKING | |
5421 | if (stmt == last_stmt) | |
628f6a4e | 5422 | { |
9ff3d2de JL |
5423 | e = find_edge (bb, EXIT_BLOCK_PTR); |
5424 | gcc_assert (e == NULL); | |
628f6a4e | 5425 | } |
6de9cd9a DN |
5426 | #endif |
5427 | ||
5428 | /* Note that the following may create a new basic block | |
5429 | and renumber the existing basic blocks. */ | |
5430 | if (stmt != last_stmt) | |
5431 | { | |
5432 | e = split_block (bb, stmt); | |
5433 | if (e) | |
5434 | blocks_split++; | |
5435 | } | |
5436 | make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE); | |
5437 | } | |
5438 | bsi_prev (&bsi); | |
5439 | } | |
5440 | while (!bsi_end_p (bsi)); | |
5441 | } | |
5442 | } | |
5443 | ||
5444 | if (blocks_split) | |
5445 | verify_flow_info (); | |
5446 | ||
5447 | return blocks_split; | |
5448 | } | |
5449 | ||
1eaba2f2 RH |
5450 | bool |
5451 | tree_purge_dead_eh_edges (basic_block bb) | |
5452 | { | |
5453 | bool changed = false; | |
628f6a4e BE |
5454 | edge e; |
5455 | edge_iterator ei; | |
1eaba2f2 RH |
5456 | tree stmt = last_stmt (bb); |
5457 | ||
5458 | if (stmt && tree_can_throw_internal (stmt)) | |
5459 | return false; | |
5460 | ||
628f6a4e | 5461 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
1eaba2f2 | 5462 | { |
1eaba2f2 RH |
5463 | if (e->flags & EDGE_EH) |
5464 | { | |
d0d2cc21 | 5465 | remove_edge (e); |
1eaba2f2 RH |
5466 | changed = true; |
5467 | } | |
628f6a4e BE |
5468 | else |
5469 | ei_next (&ei); | |
1eaba2f2 RH |
5470 | } |
5471 | ||
69d49802 JJ |
5472 | /* Removal of dead EH edges might change dominators of not |
5473 | just immediate successors. E.g. when bb1 is changed so that | |
5474 | it no longer can throw and bb1->bb3 and bb1->bb4 are dead | |
5475 | eh edges purged by this function in: | |
5476 | 0 | |
5477 | / \ | |
5478 | v v | |
5479 | 1-->2 | |
5480 | / \ | | |
5481 | v v | | |
5482 | 3-->4 | | |
5483 | \ v | |
5484 | --->5 | |
5485 | | | |
5486 | - | |
5487 | idom(bb5) must be recomputed. For now just free the dominance | |
5488 | info. */ | |
5489 | if (changed) | |
5490 | free_dominance_info (CDI_DOMINATORS); | |
5491 | ||
1eaba2f2 RH |
5492 | return changed; |
5493 | } | |
5494 | ||
5495 | bool | |
5496 | tree_purge_all_dead_eh_edges (bitmap blocks) | |
5497 | { | |
5498 | bool changed = false; | |
3cd8c58a | 5499 | unsigned i; |
87c476a2 | 5500 | bitmap_iterator bi; |
1eaba2f2 | 5501 | |
87c476a2 ZD |
5502 | EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi) |
5503 | { | |
5504 | changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i)); | |
5505 | } | |
1eaba2f2 RH |
5506 | |
5507 | return changed; | |
5508 | } | |
6de9cd9a | 5509 | |
a100ac1e KH |
5510 | /* This function is called whenever a new edge is created or |
5511 | redirected. */ | |
5512 | ||
5513 | static void | |
5514 | tree_execute_on_growing_pred (edge e) | |
5515 | { | |
5516 | basic_block bb = e->dest; | |
5517 | ||
5518 | if (phi_nodes (bb)) | |
5519 | reserve_phi_args_for_new_edge (bb); | |
5520 | } | |
5521 | ||
e51546f8 KH |
5522 | /* This function is called immediately before edge E is removed from |
5523 | the edge vector E->dest->preds. */ | |
5524 | ||
5525 | static void | |
5526 | tree_execute_on_shrinking_pred (edge e) | |
5527 | { | |
5528 | if (phi_nodes (e->dest)) | |
5529 | remove_phi_args (e); | |
5530 | } | |
5531 | ||
6de9cd9a DN |
5532 | struct cfg_hooks tree_cfg_hooks = { |
5533 | "tree", | |
5534 | tree_verify_flow_info, | |
5535 | tree_dump_bb, /* dump_bb */ | |
5536 | create_bb, /* create_basic_block */ | |
5537 | tree_redirect_edge_and_branch,/* redirect_edge_and_branch */ | |
5538 | tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */ | |
5539 | remove_bb, /* delete_basic_block */ | |
5540 | tree_split_block, /* split_block */ | |
5541 | tree_move_block_after, /* move_block_after */ | |
5542 | tree_can_merge_blocks_p, /* can_merge_blocks_p */ | |
5543 | tree_merge_blocks, /* merge_blocks */ | |
5544 | tree_predict_edge, /* predict_edge */ | |
5545 | tree_predicted_by_p, /* predicted_by_p */ | |
5546 | tree_can_duplicate_bb_p, /* can_duplicate_block_p */ | |
5547 | tree_duplicate_bb, /* duplicate_block */ | |
5548 | tree_split_edge, /* split_edge */ | |
5549 | tree_make_forwarder_block, /* make_forward_block */ | |
5550 | NULL, /* tidy_fallthru_edge */ | |
5551 | tree_block_ends_with_call_p, /* block_ends_with_call_p */ | |
5552 | tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */ | |
d9d4706f | 5553 | tree_flow_call_edges_add, /* flow_call_edges_add */ |
a100ac1e | 5554 | tree_execute_on_growing_pred, /* execute_on_growing_pred */ |
e51546f8 | 5555 | tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */ |
6de9cd9a DN |
5556 | }; |
5557 | ||
5558 | ||
5559 | /* Split all critical edges. */ | |
5560 | ||
5561 | static void | |
5562 | split_critical_edges (void) | |
5563 | { | |
5564 | basic_block bb; | |
5565 | edge e; | |
628f6a4e | 5566 | edge_iterator ei; |
6de9cd9a | 5567 | |
d6be0d7f JL |
5568 | /* split_edge can redirect edges out of SWITCH_EXPRs, which can get |
5569 | expensive. So we want to enable recording of edge to CASE_LABEL_EXPR | |
5570 | mappings around the calls to split_edge. */ | |
5571 | start_recording_case_labels (); | |
6de9cd9a DN |
5572 | FOR_ALL_BB (bb) |
5573 | { | |
628f6a4e | 5574 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
5575 | if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL)) |
5576 | { | |
5577 | split_edge (e); | |
5578 | } | |
5579 | } | |
d6be0d7f | 5580 | end_recording_case_labels (); |
6de9cd9a DN |
5581 | } |
5582 | ||
5583 | struct tree_opt_pass pass_split_crit_edges = | |
5584 | { | |
5d44aeed | 5585 | "crited", /* name */ |
6de9cd9a DN |
5586 | NULL, /* gate */ |
5587 | split_critical_edges, /* execute */ | |
5588 | NULL, /* sub */ | |
5589 | NULL, /* next */ | |
5590 | 0, /* static_pass_number */ | |
5591 | TV_TREE_SPLIT_EDGES, /* tv_id */ | |
5592 | PROP_cfg, /* properties required */ | |
5593 | PROP_no_crit_edges, /* properties_provided */ | |
5594 | 0, /* properties_destroyed */ | |
5595 | 0, /* todo_flags_start */ | |
9f8628ba PB |
5596 | TODO_dump_func, /* todo_flags_finish */ |
5597 | 0 /* letter */ | |
6de9cd9a | 5598 | }; |
26277d41 PB |
5599 | |
5600 | \f | |
5601 | /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into | |
5602 | a temporary, make sure and register it to be renamed if necessary, | |
5603 | and finally return the temporary. Put the statements to compute | |
5604 | EXP before the current statement in BSI. */ | |
5605 | ||
5606 | tree | |
5607 | gimplify_val (block_stmt_iterator *bsi, tree type, tree exp) | |
5608 | { | |
5609 | tree t, new_stmt, orig_stmt; | |
5610 | ||
5611 | if (is_gimple_val (exp)) | |
5612 | return exp; | |
5613 | ||
5614 | t = make_rename_temp (type, NULL); | |
5615 | new_stmt = build (MODIFY_EXPR, type, t, exp); | |
5616 | ||
5617 | orig_stmt = bsi_stmt (*bsi); | |
5618 | SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt)); | |
5619 | TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt); | |
5620 | ||
5621 | bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT); | |
5622 | ||
5623 | return t; | |
5624 | } | |
5625 | ||
5626 | /* Build a ternary operation and gimplify it. Emit code before BSI. | |
5627 | Return the gimple_val holding the result. */ | |
5628 | ||
5629 | tree | |
5630 | gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code, | |
5631 | tree type, tree a, tree b, tree c) | |
5632 | { | |
5633 | tree ret; | |
5634 | ||
5635 | ret = fold (build3 (code, type, a, b, c)); | |
5636 | STRIP_NOPS (ret); | |
5637 | ||
5638 | return gimplify_val (bsi, type, ret); | |
5639 | } | |
5640 | ||
5641 | /* Build a binary operation and gimplify it. Emit code before BSI. | |
5642 | Return the gimple_val holding the result. */ | |
5643 | ||
5644 | tree | |
5645 | gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code, | |
5646 | tree type, tree a, tree b) | |
5647 | { | |
5648 | tree ret; | |
5649 | ||
5650 | ret = fold (build2 (code, type, a, b)); | |
5651 | STRIP_NOPS (ret); | |
5652 | ||
5653 | return gimplify_val (bsi, type, ret); | |
5654 | } | |
5655 | ||
5656 | /* Build a unary operation and gimplify it. Emit code before BSI. | |
5657 | Return the gimple_val holding the result. */ | |
5658 | ||
5659 | tree | |
5660 | gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type, | |
5661 | tree a) | |
5662 | { | |
5663 | tree ret; | |
5664 | ||
5665 | ret = fold (build1 (code, type, a)); | |
5666 | STRIP_NOPS (ret); | |
5667 | ||
5668 | return gimplify_val (bsi, type, ret); | |
5669 | } | |
5670 | ||
5671 | ||
6de9cd9a DN |
5672 | \f |
5673 | /* Emit return warnings. */ | |
5674 | ||
5675 | static void | |
5676 | execute_warn_function_return (void) | |
5677 | { | |
9506ac2b PB |
5678 | #ifdef USE_MAPPED_LOCATION |
5679 | source_location location; | |
5680 | #else | |
6de9cd9a | 5681 | location_t *locus; |
9506ac2b | 5682 | #endif |
6de9cd9a DN |
5683 | tree last; |
5684 | edge e; | |
628f6a4e | 5685 | edge_iterator ei; |
6de9cd9a DN |
5686 | |
5687 | if (warn_missing_noreturn | |
5688 | && !TREE_THIS_VOLATILE (cfun->decl) | |
628f6a4e | 5689 | && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0 |
6de9cd9a | 5690 | && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl)) |
971801ff JM |
5691 | warning ("%Jfunction might be possible candidate for " |
5692 | "attribute %<noreturn%>", | |
6de9cd9a DN |
5693 | cfun->decl); |
5694 | ||
5695 | /* If we have a path to EXIT, then we do return. */ | |
5696 | if (TREE_THIS_VOLATILE (cfun->decl) | |
628f6a4e | 5697 | && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0) |
6de9cd9a | 5698 | { |
9506ac2b PB |
5699 | #ifdef USE_MAPPED_LOCATION |
5700 | location = UNKNOWN_LOCATION; | |
5701 | #else | |
6de9cd9a | 5702 | locus = NULL; |
9506ac2b | 5703 | #endif |
628f6a4e | 5704 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds) |
6de9cd9a DN |
5705 | { |
5706 | last = last_stmt (e->src); | |
5707 | if (TREE_CODE (last) == RETURN_EXPR | |
9506ac2b PB |
5708 | #ifdef USE_MAPPED_LOCATION |
5709 | && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION) | |
5710 | #else | |
6de9cd9a | 5711 | && (locus = EXPR_LOCUS (last)) != NULL) |
9506ac2b | 5712 | #endif |
6de9cd9a DN |
5713 | break; |
5714 | } | |
9506ac2b PB |
5715 | #ifdef USE_MAPPED_LOCATION |
5716 | if (location == UNKNOWN_LOCATION) | |
5717 | location = cfun->function_end_locus; | |
971801ff | 5718 | warning ("%H%<noreturn%> function does return", &location); |
9506ac2b | 5719 | #else |
6de9cd9a DN |
5720 | if (!locus) |
5721 | locus = &cfun->function_end_locus; | |
971801ff | 5722 | warning ("%H%<noreturn%> function does return", locus); |
9506ac2b | 5723 | #endif |
6de9cd9a DN |
5724 | } |
5725 | ||
5726 | /* If we see "return;" in some basic block, then we do reach the end | |
5727 | without returning a value. */ | |
5728 | else if (warn_return_type | |
089efaa4 | 5729 | && !TREE_NO_WARNING (cfun->decl) |
628f6a4e | 5730 | && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0 |
6de9cd9a DN |
5731 | && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl)))) |
5732 | { | |
628f6a4e | 5733 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds) |
6de9cd9a DN |
5734 | { |
5735 | tree last = last_stmt (e->src); | |
5736 | if (TREE_CODE (last) == RETURN_EXPR | |
5737 | && TREE_OPERAND (last, 0) == NULL) | |
5738 | { | |
9506ac2b PB |
5739 | #ifdef USE_MAPPED_LOCATION |
5740 | location = EXPR_LOCATION (last); | |
5741 | if (location == UNKNOWN_LOCATION) | |
5742 | location = cfun->function_end_locus; | |
5743 | warning ("%Hcontrol reaches end of non-void function", &location); | |
5744 | #else | |
6de9cd9a DN |
5745 | locus = EXPR_LOCUS (last); |
5746 | if (!locus) | |
5747 | locus = &cfun->function_end_locus; | |
5748 | warning ("%Hcontrol reaches end of non-void function", locus); | |
9506ac2b | 5749 | #endif |
089efaa4 | 5750 | TREE_NO_WARNING (cfun->decl) = 1; |
6de9cd9a DN |
5751 | break; |
5752 | } | |
5753 | } | |
5754 | } | |
5755 | } | |
5756 | ||
5757 | ||
5758 | /* Given a basic block B which ends with a conditional and has | |
5759 | precisely two successors, determine which of the edges is taken if | |
5760 | the conditional is true and which is taken if the conditional is | |
5761 | false. Set TRUE_EDGE and FALSE_EDGE appropriately. */ | |
5762 | ||
5763 | void | |
5764 | extract_true_false_edges_from_block (basic_block b, | |
5765 | edge *true_edge, | |
5766 | edge *false_edge) | |
5767 | { | |
628f6a4e | 5768 | edge e = EDGE_SUCC (b, 0); |
6de9cd9a DN |
5769 | |
5770 | if (e->flags & EDGE_TRUE_VALUE) | |
5771 | { | |
5772 | *true_edge = e; | |
628f6a4e | 5773 | *false_edge = EDGE_SUCC (b, 1); |
6de9cd9a DN |
5774 | } |
5775 | else | |
5776 | { | |
5777 | *false_edge = e; | |
628f6a4e | 5778 | *true_edge = EDGE_SUCC (b, 1); |
6de9cd9a DN |
5779 | } |
5780 | } | |
5781 | ||
5782 | struct tree_opt_pass pass_warn_function_return = | |
5783 | { | |
5784 | NULL, /* name */ | |
5785 | NULL, /* gate */ | |
5786 | execute_warn_function_return, /* execute */ | |
5787 | NULL, /* sub */ | |
5788 | NULL, /* next */ | |
5789 | 0, /* static_pass_number */ | |
5790 | 0, /* tv_id */ | |
00bfee6f | 5791 | PROP_cfg, /* properties_required */ |
6de9cd9a DN |
5792 | 0, /* properties_provided */ |
5793 | 0, /* properties_destroyed */ | |
5794 | 0, /* todo_flags_start */ | |
9f8628ba PB |
5795 | 0, /* todo_flags_finish */ |
5796 | 0 /* letter */ | |
6de9cd9a DN |
5797 | }; |
5798 | ||
5799 | #include "gt-tree-cfg.h" |