]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-optimize.c
Change copyright header to refer to version 3 of the GNU General Public License and...
[thirdparty/gcc.git] / gcc / tree-optimize.c
CommitLineData
ac534736 1/* Top-level control of tree optimizations.
9dcd6f09 2 Copyright 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
6de9cd9a 3 Contributed by Diego Novillo <dnovillo@redhat.com>
4985cde3
RH
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9dcd6f09 9the Free Software Foundation; either version 3, or (at your option)
4985cde3
RH
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
4985cde3
RH
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
6de9cd9a 24#include "tm.h"
4985cde3 25#include "tree.h"
6de9cd9a
DN
26#include "rtl.h"
27#include "tm_p.h"
28#include "hard-reg-set.h"
29#include "basic-block.h"
30#include "output.h"
31#include "expr.h"
32#include "diagnostic.h"
33#include "basic-block.h"
4985cde3 34#include "flags.h"
6de9cd9a
DN
35#include "tree-flow.h"
36#include "tree-dump.h"
4985cde3 37#include "timevar.h"
4985cde3 38#include "function.h"
6de9cd9a
DN
39#include "langhooks.h"
40#include "toplev.h"
41#include "flags.h"
42#include "cgraph.h"
43#include "tree-inline.h"
44#include "tree-mudflap.h"
45#include "tree-pass.h"
4985cde3 46#include "ggc.h"
6de9cd9a 47#include "cgraph.h"
9f8628ba 48#include "graph.h"
2b271002 49#include "cfgloop.h"
e21aff8a 50#include "except.h"
6de9cd9a 51
6de9cd9a 52
6de9cd9a
DN
53/* Gate: execute, or not, all of the non-trivial optimizations. */
54
55static bool
56gate_all_optimizations (void)
57{
58 return (optimize >= 1
7a388ee4
JH
59 /* Don't bother doing anything if the program has errors.
60 We have to pass down the queue if we already went into SSA */
61 && (!(errorcount || sorrycount) || gimple_in_ssa_p (cfun)));
6de9cd9a
DN
62}
63
ef330312 64struct tree_opt_pass pass_all_optimizations =
6de9cd9a
DN
65{
66 NULL, /* name */
67 gate_all_optimizations, /* gate */
68 NULL, /* execute */
69 NULL, /* sub */
70 NULL, /* next */
71 0, /* static_pass_number */
72 0, /* tv_id */
73 0, /* properties_required */
74 0, /* properties_provided */
75 0, /* properties_destroyed */
76 0, /* todo_flags_start */
9f8628ba
PB
77 0, /* todo_flags_finish */
78 0 /* letter */
6de9cd9a
DN
79};
80
7a388ee4
JH
81/* Gate: execute, or not, all of the non-trivial optimizations. */
82
83static bool
84gate_all_early_local_passes (void)
85{
86 /* Don't bother doing anything if the program has errors. */
87 return (!errorcount && !sorrycount);
88}
89
ef330312 90struct tree_opt_pass pass_early_local_passes =
d63db217 91{
7a388ee4
JH
92 "early_local_cleanups", /* name */
93 gate_all_early_local_passes, /* gate */
d63db217
JH
94 NULL, /* execute */
95 NULL, /* sub */
96 NULL, /* next */
97 0, /* static_pass_number */
98 0, /* tv_id */
99 0, /* properties_required */
100 0, /* properties_provided */
101 0, /* properties_destroyed */
102 0, /* todo_flags_start */
f4b3ca72 103 TODO_remove_functions, /* todo_flags_finish */
d63db217
JH
104 0 /* letter */
105};
106
7a388ee4
JH
107static unsigned int
108execute_early_local_optimizations (void)
109{
110 if (flag_unit_at_a_time)
111 cgraph_state = CGRAPH_STATE_IPA_SSA;
112 return 0;
113}
114
115/* Gate: execute, or not, all of the non-trivial optimizations. */
116
117static bool
118gate_all_early_optimizations (void)
119{
120 return (optimize >= 1
121 /* Don't bother doing anything if the program has errors. */
122 && !(errorcount || sorrycount));
123}
124
125struct tree_opt_pass pass_all_early_optimizations =
126{
127 "early_optimizations", /* name */
128 gate_all_early_optimizations, /* gate */
129 execute_early_local_optimizations, /* execute */
130 NULL, /* sub */
131 NULL, /* next */
132 0, /* static_pass_number */
133 0, /* tv_id */
134 0, /* properties_required */
135 0, /* properties_provided */
136 0, /* properties_destroyed */
137 0, /* todo_flags_start */
138 0, /* todo_flags_finish */
139 0 /* letter */
140};
141
d63db217
JH
142/* Pass: cleanup the CFG just before expanding trees to RTL.
143 This is just a round of label cleanups and case node grouping
144 because after the tree optimizers have run such cleanups may
145 be necessary. */
146
c2924966 147static unsigned int
d63db217
JH
148execute_cleanup_cfg_pre_ipa (void)
149{
150 cleanup_tree_cfg ();
c2924966 151 return 0;
d63db217
JH
152}
153
ef330312 154struct tree_opt_pass pass_cleanup_cfg =
d63db217
JH
155{
156 "cleanup_cfg", /* name */
157 NULL, /* gate */
158 execute_cleanup_cfg_pre_ipa, /* execute */
159 NULL, /* sub */
160 NULL, /* next */
161 0, /* static_pass_number */
162 0, /* tv_id */
163 PROP_cfg, /* properties_required */
164 0, /* properties_provided */
165 0, /* properties_destroyed */
166 0, /* todo_flags_start */
167 TODO_dump_func, /* todo_flags_finish */
168 0 /* letter */
169};
170
171
165b54c3
SB
172/* Pass: cleanup the CFG just before expanding trees to RTL.
173 This is just a round of label cleanups and case node grouping
174 because after the tree optimizers have run such cleanups may
175 be necessary. */
176
c2924966 177static unsigned int
165b54c3
SB
178execute_cleanup_cfg_post_optimizing (void)
179{
1ff54bfb 180 fold_cond_expr_cond ();
165b54c3
SB
181 cleanup_tree_cfg ();
182 cleanup_dead_labels ();
183 group_case_labels ();
c2924966 184 return 0;
165b54c3
SB
185}
186
ef330312 187struct tree_opt_pass pass_cleanup_cfg_post_optimizing =
165b54c3 188{
edfaf675 189 "final_cleanup", /* name */
165b54c3
SB
190 NULL, /* gate */
191 execute_cleanup_cfg_post_optimizing, /* execute */
192 NULL, /* sub */
193 NULL, /* next */
194 0, /* static_pass_number */
195 0, /* tv_id */
196 PROP_cfg, /* properties_required */
197 0, /* properties_provided */
198 0, /* properties_destroyed */
199 0, /* todo_flags_start */
edfaf675 200 TODO_dump_func, /* todo_flags_finish */
9f8628ba 201 0 /* letter */
165b54c3
SB
202};
203
6de9cd9a
DN
204/* Pass: do the actions required to finish with tree-ssa optimization
205 passes. */
206
c2924966 207static unsigned int
242229bb 208execute_free_datastructures (void)
6de9cd9a 209{
6de9cd9a 210 free_dominance_info (CDI_DOMINATORS);
6844185d 211 free_dominance_info (CDI_POST_DOMINATORS);
6de9cd9a 212
a9b77cd1 213 /* Remove the ssa structures. */
7a388ee4
JH
214 if (cfun->gimple_df)
215 delete_tree_ssa ();
c2924966 216 return 0;
6844185d 217}
6de9cd9a 218
ef330312 219struct tree_opt_pass pass_free_datastructures =
6844185d
JH
220{
221 NULL, /* name */
222 NULL, /* gate */
223 execute_free_datastructures, /* execute */
224 NULL, /* sub */
225 NULL, /* next */
226 0, /* static_pass_number */
227 0, /* tv_id */
228 PROP_cfg, /* properties_required */
229 0, /* properties_provided */
230 0, /* properties_destroyed */
231 0, /* todo_flags_start */
232 0, /* todo_flags_finish */
233 0 /* letter */
234};
235/* Pass: free cfg annotations. */
236
c2924966 237static unsigned int
6844185d
JH
238execute_free_cfg_annotations (void)
239{
242229bb
JH
240 /* And get rid of annotations we no longer need. */
241 delete_tree_cfg_annotations ();
736432ee 242
c2924966 243 return 0;
6de9cd9a
DN
244}
245
ef330312 246struct tree_opt_pass pass_free_cfg_annotations =
6de9cd9a
DN
247{
248 NULL, /* name */
249 NULL, /* gate */
6844185d 250 execute_free_cfg_annotations, /* execute */
6de9cd9a
DN
251 NULL, /* sub */
252 NULL, /* next */
253 0, /* static_pass_number */
254 0, /* tv_id */
255 PROP_cfg, /* properties_required */
256 0, /* properties_provided */
242229bb 257 0, /* properties_destroyed */
6de9cd9a 258 0, /* todo_flags_start */
9f8628ba
PB
259 0, /* todo_flags_finish */
260 0 /* letter */
6de9cd9a 261};
4f6c2131 262
4f6c2131 263/* Pass: fixup_cfg. IPA passes, compilation of earlier functions or inlining
1084e689
JH
264 might have changed some properties, such as marked functions nothrow.
265 Remove redundant edges and basic blocks, and create new ones if necessary.
e21aff8a 266
873aa8f5
JH
267 This pass can't be executed as stand alone pass from pass manager, because
268 in between inlining and this fixup the verify_flow_info would fail. */
269
270unsigned int
e21aff8a
SB
271execute_fixup_cfg (void)
272{
273 basic_block bb;
274 block_stmt_iterator bsi;
59e50498 275 int todo = gimple_in_ssa_p (cfun) ? TODO_verify_ssa : 0;
e21aff8a 276
de6bd996
JH
277 cfun->after_inlining = true;
278
e21aff8a
SB
279 if (cfun->eh)
280 FOR_EACH_BB (bb)
281 {
282 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
283 {
284 tree stmt = bsi_stmt (bsi);
285 tree call = get_call_expr_in (stmt);
de6bd996 286 tree decl = call ? get_callee_fndecl (call) : NULL;
e21aff8a 287
de6bd996
JH
288 if (decl && call_expr_flags (call) & (ECF_CONST | ECF_PURE)
289 && TREE_SIDE_EFFECTS (call))
290 {
59e50498
JH
291 if (gimple_in_ssa_p (cfun))
292 {
873aa8f5 293 todo |= TODO_update_ssa | TODO_cleanup_cfg;
59e50498
JH
294 update_stmt (stmt);
295 }
de6bd996
JH
296 TREE_SIDE_EFFECTS (call) = 0;
297 }
298 if (decl && TREE_NOTHROW (decl))
299 TREE_NOTHROW (call) = 1;
e21aff8a
SB
300 if (!tree_could_throw_p (stmt) && lookup_stmt_eh_region (stmt))
301 remove_stmt_from_eh_region (stmt);
302 }
873aa8f5
JH
303 if (tree_purge_dead_eh_edges (bb))
304 todo |= TODO_cleanup_cfg;
e21aff8a 305 }
4f6c2131 306
4f6c2131
EB
307 /* Dump a textual representation of the flowgraph. */
308 if (dump_file)
309 dump_tree_cfg (dump_file, dump_flags);
310
59e50498 311 return todo;
e21aff8a
SB
312}
313
33c94679
DN
314/* Do the actions required to initialize internal data structures used
315 in tree-ssa optimization passes. */
316
c2924966 317static unsigned int
33c94679
DN
318execute_init_datastructures (void)
319{
320 /* Allocate hash tables, arrays and other structures. */
321 init_tree_ssa ();
c2924966 322 return 0;
33c94679
DN
323}
324
7a388ee4
JH
325/* Gate: initialize or not the SSA datastructures. */
326
327static bool
328gate_init_datastructures (void)
329{
330 return (optimize >= 1);
331}
332
ef330312 333struct tree_opt_pass pass_init_datastructures =
33c94679
DN
334{
335 NULL, /* name */
7a388ee4 336 gate_init_datastructures, /* gate */
33c94679
DN
337 execute_init_datastructures, /* execute */
338 NULL, /* sub */
339 NULL, /* next */
340 0, /* static_pass_number */
341 0, /* tv_id */
342 PROP_cfg, /* properties_required */
343 0, /* properties_provided */
344 0, /* properties_destroyed */
345 0, /* todo_flags_start */
9f8628ba
PB
346 0, /* todo_flags_finish */
347 0 /* letter */
33c94679
DN
348};
349
e21aff8a
SB
350void
351tree_lowering_passes (tree fn)
352{
353 tree saved_current_function_decl = current_function_decl;
354
355 current_function_decl = fn;
356 push_cfun (DECL_STRUCT_FUNCTION (fn));
357 tree_register_cfg_hooks ();
358 bitmap_obstack_initialize (NULL);
359 execute_pass_list (all_lowering_passes);
7a388ee4
JH
360 if (optimize && cgraph_global_info_ready)
361 execute_pass_list (pass_early_local_passes.sub);
e21aff8a 362 free_dominance_info (CDI_POST_DOMINATORS);
7a388ee4 363 free_dominance_info (CDI_DOMINATORS);
e21aff8a
SB
364 compact_blocks ();
365 current_function_decl = saved_current_function_decl;
366 bitmap_obstack_release (NULL);
367 pop_cfun ();
368}
6de9cd9a 369\f
4985cde3
RH
370/* For functions-as-trees languages, this performs all optimization and
371 compilation for FNDECL. */
372
373void
0f0377f6 374tree_rest_of_compilation (tree fndecl)
4985cde3 375{
5d4854c8 376 location_t saved_loc;
ea99e0be 377 struct cgraph_node *node;
5d4854c8 378
4985cde3
RH
379 timevar_push (TV_EXPAND);
380
1e128c5f 381 gcc_assert (!flag_unit_at_a_time || cgraph_global_info_ready);
4985cde3 382
ea99e0be
JH
383 node = cgraph_node (fndecl);
384
de6bd996
JH
385 /* Initialize the default bitmap obstack. */
386 bitmap_obstack_initialize (NULL);
387
4985cde3
RH
388 /* Initialize the RTL code for the function. */
389 current_function_decl = fndecl;
873aa8f5 390 cfun = DECL_STRUCT_FUNCTION (fndecl);
5d4854c8 391 saved_loc = input_location;
f31686a3 392 input_location = DECL_SOURCE_LOCATION (fndecl);
4985cde3
RH
393 init_function_start (fndecl);
394
4985cde3
RH
395 /* Even though we're inside a function body, we still don't want to
396 call expand_expr to calculate the size of a variable-sized array.
397 We haven't necessarily assigned RTL to all variables yet, so it's
398 not safe to try to expand expressions involving them. */
4985cde3 399 cfun->x_dont_save_pending_sizes_p = 1;
de6bd996
JH
400
401 tree_register_cfg_hooks ();
4985cde3 402
7932a3db 403 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
2f8e398b
PB
404 /* Perform all tree transforms and optimizations. */
405 execute_pass_list (all_passes);
7932a3db
NS
406
407 bitmap_obstack_release (&reg_obstack);
4985cde3 408
7932a3db
NS
409 /* Release the default bitmap obstack. */
410 bitmap_obstack_release (NULL);
411
ea99e0be 412 DECL_SAVED_TREE (fndecl) = NULL;
6de9cd9a 413 cfun = 0;
4985cde3
RH
414
415 /* If requested, warn about function definitions where the function will
416 return a value (usually of some struct or union type) which itself will
417 take up a lot of stack space. */
418 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
419 {
420 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
421
422 if (ret_type && TYPE_SIZE_UNIT (ret_type)
423 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
424 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
425 larger_than_size))
426 {
4985cde3
RH
427 unsigned int size_as_int
428 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
429
430 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
dee15844
JM
431 warning (0, "size of return value of %q+D is %u bytes",
432 fndecl, size_as_int);
4985cde3 433 else
dee15844
JM
434 warning (0, "size of return value of %q+D is larger than %wd bytes",
435 fndecl, larger_than_size);
4985cde3
RH
436 }
437 }
438
0f0377f6 439 if (!flag_inline_trees)
4985cde3 440 {
d173e685 441 DECL_SAVED_TREE (fndecl) = NULL;
6de9cd9a
DN
442 if (DECL_STRUCT_FUNCTION (fndecl) == 0
443 && !cgraph_node (fndecl)->origin)
d173e685
JH
444 {
445 /* Stop pointing to the local nodes about to be freed.
446 But DECL_INITIAL must remain nonzero so we know this
447 was an actual function definition.
448 For a nested function, this is done in c_pop_function_context.
449 If rest_of_compilation set this to 0, leave it 0. */
450 if (DECL_INITIAL (fndecl) != 0)
451 DECL_INITIAL (fndecl) = error_mark_node;
d173e685 452 }
4985cde3
RH
453 }
454
5d4854c8
RH
455 input_location = saved_loc;
456
6de9cd9a 457 ggc_collect ();
4985cde3
RH
458 timevar_pop (TV_EXPAND);
459}