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