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