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