OSDN Git Service

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