OSDN Git Service

b9a2fa55ecea985bed0634977ac3e42b4174e888
[pf3gnuchains/gcc-fork.git] / gcc / tree-optimize.c
1 /* Top-level control of tree optimizations.
2    Copyright 2001, 2002, 2003, 2004 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 "expr.h"
33 #include "diagnostic.h"
34 #include "basic-block.h"
35 #include "flags.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
38 #include "timevar.h"
39 #include "function.h"
40 #include "langhooks.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "cgraph.h"
44 #include "tree-inline.h"
45 #include "tree-mudflap.h"
46 #include "tree-pass.h"
47 #include "ggc.h"
48 #include "cgraph.h"
49 #include "graph.h"
50
51
52 /* Global variables used to communicate with passes.  */
53 int dump_flags;
54 bitmap vars_to_rename;
55 bool in_gimple_form;
56
57 /* The root of the compilation pass tree, once constructed.  */
58 static struct tree_opt_pass *all_passes;
59
60 /* Pass: dump the gimplified, inlined, functions.  */
61
62 static struct tree_opt_pass pass_gimple = 
63 {
64   "gimple",                             /* name */
65   NULL,                                 /* gate */
66   NULL,                                 /* execute */
67   NULL,                                 /* sub */
68   NULL,                                 /* next */
69   0,                                    /* static_pass_number */
70   0,                                    /* tv_id */
71   0,                                    /* properties_required */
72   PROP_gimple_any,                      /* properties_provided */
73   0,                                    /* properties_destroyed */
74   0,                                    /* todo_flags_start */
75   TODO_dump_func,                       /* todo_flags_finish */
76   0                                     /* letter */
77 };
78
79 /* Gate: execute, or not, all of the non-trivial optimizations.  */
80
81 static bool
82 gate_all_optimizations (void)
83 {
84   return (optimize >= 1
85           /* Don't bother doing anything if the program has errors.  */
86           && !(errorcount || sorrycount));
87 }
88
89 static struct tree_opt_pass pass_all_optimizations =
90 {
91   NULL,                                 /* name */
92   gate_all_optimizations,               /* gate */
93   NULL,                                 /* execute */
94   NULL,                                 /* sub */
95   NULL,                                 /* next */
96   0,                                    /* static_pass_number */
97   0,                                    /* tv_id */
98   0,                                    /* properties_required */
99   0,                                    /* properties_provided */
100   0,                                    /* properties_destroyed */
101   0,                                    /* todo_flags_start */
102   0,                                    /* todo_flags_finish */
103   0                                     /* letter */
104 };
105
106 /* Pass: cleanup the CFG just before expanding trees to RTL.
107    This is just a round of label cleanups and case node grouping
108    because after the tree optimizers have run such cleanups may
109    be necessary.  */
110
111 static void 
112 execute_cleanup_cfg_post_optimizing (void)
113 {
114   cleanup_tree_cfg ();
115   cleanup_dead_labels ();
116   group_case_labels ();
117 }
118
119 static struct tree_opt_pass pass_cleanup_cfg_post_optimizing =
120 {
121   NULL,                                 /* name */
122   NULL,                                 /* gate */
123   execute_cleanup_cfg_post_optimizing,  /* execute */
124   NULL,                                 /* sub */
125   NULL,                                 /* next */
126   0,                                    /* static_pass_number */
127   0,                                    /* tv_id */
128   PROP_cfg,                             /* properties_required */
129   0,                                    /* properties_provided */
130   0,                                    /* properties_destroyed */
131   0,                                    /* todo_flags_start */
132   0,                                    /* todo_flags_finish */
133   0                                     /* letter */
134 };
135
136 /* Pass: do the actions required to finish with tree-ssa optimization
137    passes.  */
138
139 static void
140 execute_free_datastructures (void)
141 {
142   tree *chain;
143
144   /* ??? This isn't the right place for this.  Worse, it got computed
145      more or less at random in various passes.  */
146   free_dominance_info (CDI_DOMINATORS);
147
148   /* Emit gotos for implicit jumps.  */
149   disband_implicit_edges ();
150
151   /* Remove the ssa structures.  Do it here since this includes statement
152      annotations that need to be intact during disband_implicit_edges.  */
153   delete_tree_ssa ();
154
155   /* Re-chain the statements from the blocks.  */
156   chain = &DECL_SAVED_TREE (current_function_decl);
157   *chain = alloc_stmt_list ();
158
159   /* And get rid of annotations we no longer need.  */
160   delete_tree_cfg_annotations ();
161 }
162
163 static struct tree_opt_pass pass_free_datastructures =
164 {
165   NULL,                                 /* name */
166   NULL,                                 /* gate */
167   execute_free_datastructures,                  /* execute */
168   NULL,                                 /* sub */
169   NULL,                                 /* next */
170   0,                                    /* static_pass_number */
171   0,                                    /* tv_id */
172   PROP_cfg,                             /* properties_required */
173   0,                                    /* properties_provided */
174   0,                                    /* properties_destroyed */
175   0,                                    /* todo_flags_start */
176   0,                                    /* todo_flags_finish */
177   0                                     /* letter */
178 };
179
180
181 /* Do the actions required to initialize internal data structures used
182    in tree-ssa optimization passes.  */
183
184 static void
185 execute_init_datastructures (void)
186 {
187   /* Allocate hash tables, arrays and other structures.  */
188   init_tree_ssa ();
189 }
190
191 static struct tree_opt_pass pass_init_datastructures =
192 {
193   NULL,                                 /* name */
194   NULL,                                 /* gate */
195   execute_init_datastructures,          /* execute */
196   NULL,                                 /* sub */
197   NULL,                                 /* next */
198   0,                                    /* static_pass_number */
199   0,                                    /* tv_id */
200   PROP_cfg,                             /* properties_required */
201   0,                                    /* properties_provided */
202   0,                                    /* properties_destroyed */
203   0,                                    /* todo_flags_start */
204   0,                                    /* todo_flags_finish */
205   0                                     /* letter */
206 };
207
208 /* Iterate over the pass tree allocating dump file numbers.  We want
209    to do this depth first, and independent of whether the pass is
210    enabled or not.  */
211
212 static void
213 register_one_dump_file (struct tree_opt_pass *pass, int n)
214 {
215   char *dot_name, *flag_name;
216   char num[10];
217
218   /* See below in next_pass_1.  */
219   num[0] = '\0';
220   if (pass->static_pass_number != -1)
221     sprintf (num, "%d", ((int) pass->static_pass_number < 0
222                          ? 1 : pass->static_pass_number));
223
224   dot_name = concat (".", pass->name, num, NULL);
225   if (pass->properties_provided & PROP_trees)
226     {
227       flag_name = concat ("tree-", pass->name, num, NULL);
228       pass->static_pass_number = dump_register (dot_name, flag_name,
229                                                 TDF_TREE, n + TDI_tree_all, 0);
230     }
231   else
232     {
233       flag_name = concat ("rtl-", pass->name, num, NULL);
234       pass->static_pass_number = dump_register (dot_name, flag_name,
235                                                 TDF_RTL, n, pass->letter);
236     }
237 }
238
239 static int 
240 register_dump_files (struct tree_opt_pass *pass, int properties)
241 {
242   static int n = 0;
243   do
244     {
245       int new_properties;
246       int pass_number;
247
248       pass->properties_required = properties;
249       new_properties =
250         (properties | pass->properties_provided) & ~pass->properties_destroyed;
251
252       /* Reset the counter when we reach RTL-based passes.  */
253       if ((pass->properties_provided ^ pass->properties_required) & PROP_rtl)
254         n = 0;
255
256       pass_number = n;
257       if (pass->name)
258         n++;
259
260       if (pass->sub)
261         new_properties = register_dump_files (pass->sub, new_properties);
262
263       /* If we have a gate, combine the properties that we could have with
264          and without the pass being examined.  */
265       if (pass->gate)
266         properties &= new_properties;
267       else
268         properties = new_properties;
269
270       pass->properties_provided = properties;
271       if (pass->name)
272         register_one_dump_file (pass, pass_number);
273
274       pass = pass->next;
275     }
276   while (pass);
277
278   return properties;
279 }
280
281 /* Add a pass to the pass list. Duplicate the pass if it's already
282    in the list.  */
283
284 static struct tree_opt_pass **
285 next_pass_1 (struct tree_opt_pass **list, struct tree_opt_pass *pass)
286 {
287
288   /* A nonzero static_pass_number indicates that the
289      pass is already in the list.  */
290   if (pass->static_pass_number)
291     {
292       struct tree_opt_pass *new;
293
294       new = xmalloc (sizeof (*new));
295       memcpy (new, pass, sizeof (*new));
296
297       /* Indicate to register_dump_files that this pass has duplicates,
298          and so it should rename the dump file.  The first instance will
299          be -1, and be number of duplicates = -static_pass_number - 1.
300          Subsequent instances will be > 0 and just the duplicate number.  */
301       if (pass->name)
302         {
303           pass->static_pass_number -= 1;
304           new->static_pass_number = -pass->static_pass_number;
305         }
306       
307       *list = new;
308     }
309   else
310     {
311       pass->static_pass_number = -1;
312       *list = pass;
313     }  
314   
315   return &(*list)->next;
316           
317 }
318
319 /* Construct the pass tree.  */
320
321 void
322 init_tree_optimization_passes (void)
323 {
324   struct tree_opt_pass **p;
325
326 #define NEXT_PASS(PASS)  (p = next_pass_1 (p, &PASS))
327
328   p = &all_passes;
329   NEXT_PASS (pass_gimple);
330   NEXT_PASS (pass_remove_useless_stmts);
331   NEXT_PASS (pass_mudflap_1);
332   NEXT_PASS (pass_lower_cf);
333   NEXT_PASS (pass_lower_eh);
334   NEXT_PASS (pass_build_cfg);
335   NEXT_PASS (pass_pre_expand);
336   NEXT_PASS (pass_tree_profile);
337   NEXT_PASS (pass_init_datastructures);
338   NEXT_PASS (pass_all_optimizations);
339   NEXT_PASS (pass_warn_function_return);
340   NEXT_PASS (pass_mudflap_2);
341   NEXT_PASS (pass_free_datastructures);
342   NEXT_PASS (pass_expand);
343   NEXT_PASS (pass_rest_of_compilation);
344   *p = NULL;
345
346   p = &pass_all_optimizations.sub;
347   NEXT_PASS (pass_referenced_vars);
348   NEXT_PASS (pass_build_ssa);
349   NEXT_PASS (pass_may_alias);
350   NEXT_PASS (pass_rename_ssa_copies);
351   NEXT_PASS (pass_early_warn_uninitialized);
352   NEXT_PASS (pass_dce);
353   NEXT_PASS (pass_dominator);
354   NEXT_PASS (pass_redundant_phi);
355   NEXT_PASS (pass_dce);
356   NEXT_PASS (pass_forwprop);
357   NEXT_PASS (pass_phiopt);
358   NEXT_PASS (pass_may_alias);
359   NEXT_PASS (pass_tail_recursion);
360   NEXT_PASS (pass_ch);
361   NEXT_PASS (pass_profile);
362   NEXT_PASS (pass_sra);
363   NEXT_PASS (pass_rename_ssa_copies);
364   NEXT_PASS (pass_dominator);
365   NEXT_PASS (pass_redundant_phi);
366   NEXT_PASS (pass_dce);
367   NEXT_PASS (pass_dse);
368   NEXT_PASS (pass_may_alias);
369   NEXT_PASS (pass_forwprop);
370   NEXT_PASS (pass_phiopt);
371   NEXT_PASS (pass_ccp);
372   NEXT_PASS (pass_redundant_phi);
373   NEXT_PASS (pass_fold_builtins);
374   NEXT_PASS (pass_split_crit_edges);
375   NEXT_PASS (pass_pre);
376   NEXT_PASS (pass_loop);
377   NEXT_PASS (pass_dominator);
378   NEXT_PASS (pass_redundant_phi);
379   NEXT_PASS (pass_cd_dce);
380   NEXT_PASS (pass_dse);
381   NEXT_PASS (pass_forwprop);
382   NEXT_PASS (pass_phiopt);
383   NEXT_PASS (pass_tail_calls);
384   NEXT_PASS (pass_late_warn_uninitialized);
385   NEXT_PASS (pass_del_ssa);
386   NEXT_PASS (pass_nrv);
387   NEXT_PASS (pass_remove_useless_vars);
388   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
389   *p = NULL;
390
391   p = &pass_loop.sub;
392   NEXT_PASS (pass_loop_init);
393   NEXT_PASS (pass_lim);
394   NEXT_PASS (pass_iv_canon);
395   NEXT_PASS (pass_if_conversion);
396   NEXT_PASS (pass_vectorize);
397   NEXT_PASS (pass_linear_transform);
398   NEXT_PASS (pass_complete_unroll);
399   NEXT_PASS (pass_iv_optimize);
400   NEXT_PASS (pass_loop_done);
401   *p = NULL;
402
403 #undef NEXT_PASS
404
405   /* Register the passes with the tree dump code.  */
406   register_dump_files (all_passes, 0);
407 }
408
409 static void execute_pass_list (struct tree_opt_pass *);
410
411 static unsigned int last_verified;
412
413 static void
414 execute_todo (int properties, unsigned int flags)
415 {
416   if (flags & TODO_rename_vars)
417     {
418       rewrite_into_ssa (false);
419       bitmap_clear (vars_to_rename);
420     }
421
422   if ((flags & TODO_dump_func) && dump_file)
423     {
424       if (properties & PROP_trees)
425         dump_function_to_file (current_function_decl,
426                                dump_file, dump_flags);
427       else if (properties & PROP_cfg)
428         print_rtl_with_bb (dump_file, get_insns ());
429       else
430         print_rtl (dump_file, get_insns ());
431
432       /* Flush the file.  If verification fails, we won't be able to
433          close the file before aborting.  */
434       fflush (dump_file);
435     }
436
437   if (flags & TODO_ggc_collect)
438     ggc_collect ();
439
440 #ifdef ENABLE_CHECKING
441   if (flags & TODO_verify_ssa)
442     verify_ssa ();
443   if (flags & TODO_verify_flow)
444     verify_flow_info ();
445   if (flags & TODO_verify_stmts)
446     verify_stmts ();
447 #endif
448 }
449
450 static bool
451 execute_one_pass (struct tree_opt_pass *pass)
452 {
453   unsigned int todo; 
454
455   /* See if we're supposed to run this pass.  */
456   if (pass->gate && !pass->gate ())
457     return false;
458
459   /* Note that the folders should only create gimple expressions.
460      This is a hack until the new folder is ready.  */
461   in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
462
463   /* Run pre-pass verification.  */
464   todo = pass->todo_flags_start & ~last_verified;
465   if (todo)
466     execute_todo (pass->properties_required, todo);
467
468   /* If a dump file name is present, open it if enabled.  */
469   if (pass->static_pass_number != -1)
470     {
471       bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
472       dump_file_name = get_dump_file_name (pass->static_pass_number);
473       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
474       if (dump_file)
475         {
476           const char *dname, *aname;
477           dname = lang_hooks.decl_printable_name (current_function_decl, 2);
478           aname = (IDENTIFIER_POINTER
479                    (DECL_ASSEMBLER_NAME (current_function_decl)));
480           fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
481              cfun->function_frequency == FUNCTION_FREQUENCY_HOT
482              ? " (hot)"
483              : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
484              ? " (unlikely executed)"
485              : "");
486         }
487
488       if (initializing_dump
489           && graph_dump_format != no_graph
490           && (pass->properties_provided & (PROP_cfg | PROP_rtl))
491               == (PROP_cfg | PROP_rtl))
492         clean_graph_dump_file (dump_file_name);
493     }
494
495   /* If a timevar is present, start it.  */
496   if (pass->tv_id)
497     timevar_push (pass->tv_id);
498
499   /* Do it!  */
500   if (pass->execute)
501     pass->execute ();
502
503   if (dump_file
504       && (pass->properties_provided & (PROP_cfg | PROP_rtl))
505           == (PROP_cfg | PROP_rtl))
506     print_rtl_graph_with_bb (dump_file_name, get_insns ());
507
508   /* Run post-pass cleanup and verification.  */
509   todo = pass->todo_flags_finish;
510   last_verified = todo & TODO_verify_all;
511   if (todo)
512     execute_todo (pass->properties_provided, todo);
513
514   /* Close down timevar and dump file.  */
515   if (pass->tv_id)
516     timevar_pop (pass->tv_id);
517   if (dump_file_name)
518     {
519       free ((char *) dump_file_name);
520       dump_file_name = NULL;
521     }
522   if (dump_file)
523     {
524       dump_end (pass->static_pass_number, dump_file);
525       dump_file = NULL;
526     }
527
528   return true;
529 }
530
531 static void
532 execute_pass_list (struct tree_opt_pass *pass)
533 {
534   do
535     {
536       if (execute_one_pass (pass) && pass->sub)
537         execute_pass_list (pass->sub);
538       pass = pass->next;
539     }
540   while (pass);
541 }
542
543 \f
544 /* For functions-as-trees languages, this performs all optimization and
545    compilation for FNDECL.  */
546
547 void
548 tree_rest_of_compilation (tree fndecl, bool nested_p)
549 {
550   location_t saved_loc;
551   struct cgraph_node *saved_node = NULL, *node;
552
553   timevar_push (TV_EXPAND);
554
555   gcc_assert (!flag_unit_at_a_time || cgraph_global_info_ready);
556
557   /* Initialize the RTL code for the function.  */
558   current_function_decl = fndecl;
559   saved_loc = input_location;
560   input_location = DECL_SOURCE_LOCATION (fndecl);
561   init_function_start (fndecl);
562
563   /* Even though we're inside a function body, we still don't want to
564      call expand_expr to calculate the size of a variable-sized array.
565      We haven't necessarily assigned RTL to all variables yet, so it's
566      not safe to try to expand expressions involving them.  */
567   cfun->x_dont_save_pending_sizes_p = 1;
568
569   node = cgraph_node (fndecl);
570
571   /* We might need the body of this function so that we can expand
572      it inline somewhere else.  This means not lowering some constructs
573      such as exception handling.  */
574   if (cgraph_preserve_function_body_p (fndecl))
575     {
576       if (!flag_unit_at_a_time)
577         {
578           struct cgraph_edge *e;
579
580           saved_node = cgraph_clone_node (node);
581           for (e = saved_node->callees; e; e = e->next_callee)
582             if (!e->inline_failed)
583               cgraph_clone_inlined_nodes (e, true);
584         }
585       cfun->saved_static_chain_decl = cfun->static_chain_decl;
586       cfun->saved_tree = save_body (fndecl, &cfun->saved_args,
587                                     &cfun->saved_static_chain_decl);
588     }
589
590   if (flag_inline_trees)
591     {
592       struct cgraph_edge *e;
593       for (e = node->callees; e; e = e->next_callee)
594         if (!e->inline_failed || warn_inline)
595           break;
596       if (e)
597         {
598           timevar_push (TV_INTEGRATION);
599           optimize_inline_calls (fndecl);
600           timevar_pop (TV_INTEGRATION);
601         }
602     }
603
604   /* We are not going to maintain the cgraph edges up to date.
605      Kill it so it won't confuse us.  */
606   while (node->callees)
607     cgraph_remove_edge (node->callees);
608
609   if (!vars_to_rename)
610     vars_to_rename = BITMAP_XMALLOC ();
611
612   /* If this is a nested function, protect the local variables in the stack
613      above us from being collected while we're compiling this function.  */
614   if (nested_p)
615     ggc_push_context ();
616
617   /* Perform all tree transforms and optimizations.  */
618   execute_pass_list (all_passes);
619
620   /* Restore original body if still needed.  */
621   if (cfun->saved_tree)
622     {
623       DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
624       DECL_ARGUMENTS (fndecl) = cfun->saved_args;
625       cfun->static_chain_decl = cfun->saved_static_chain_decl;
626
627       /* When not in unit-at-a-time mode, we must preserve out of line copy
628          representing node before inlining.  Restore original outgoing edges
629          using clone we created earlier.  */
630       if (!flag_unit_at_a_time)
631         {
632           struct cgraph_edge *e;
633           while (node->callees)
634             cgraph_remove_edge (node->callees);
635           node->callees = saved_node->callees;
636           saved_node->callees = NULL;
637           for (e = node->callees; e; e = e->next_callee)
638             {
639               if (e->callee->global.inlined_to)
640                 e->callee->global.inlined_to = node;
641               e->caller = node;
642             }
643           cgraph_remove_node (saved_node);
644         }
645     }
646   else
647     DECL_SAVED_TREE (fndecl) = NULL;
648   cfun = 0;
649
650   /* If requested, warn about function definitions where the function will
651      return a value (usually of some struct or union type) which itself will
652      take up a lot of stack space.  */
653   if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
654     {
655       tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
656
657       if (ret_type && TYPE_SIZE_UNIT (ret_type)
658           && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
659           && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
660                                    larger_than_size))
661         {
662           unsigned int size_as_int
663             = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
664
665           if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
666             warning ("%Jsize of return value of '%D' is %u bytes",
667                      fndecl, fndecl, size_as_int);
668           else
669             warning ("%Jsize of return value of '%D' is larger than %wd bytes",
670                      fndecl, fndecl, larger_than_size);
671         }
672     }
673
674   if (!nested_p && !flag_inline_trees)
675     {
676       DECL_SAVED_TREE (fndecl) = NULL;
677       if (DECL_STRUCT_FUNCTION (fndecl) == 0
678           && !cgraph_node (fndecl)->origin)
679         {
680           /* Stop pointing to the local nodes about to be freed.
681              But DECL_INITIAL must remain nonzero so we know this
682              was an actual function definition.
683              For a nested function, this is done in c_pop_function_context.
684              If rest_of_compilation set this to 0, leave it 0.  */
685           if (DECL_INITIAL (fndecl) != 0)
686             DECL_INITIAL (fndecl) = error_mark_node;
687         }
688     }
689
690   input_location = saved_loc;
691
692   ggc_collect ();
693
694   /* Undo the GC context switch.  */
695   if (nested_p)
696     ggc_pop_context ();
697   timevar_pop (TV_EXPAND);
698 }