]>
Commit | Line | Data |
---|---|---|
ac534736 | 1 | /* Top-level control of tree optimizations. |
7072a650 | 2 | Copyright 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 |
66647d44 | 3 | Free Software Foundation, Inc. |
6de9cd9a | 4 | Contributed by Diego Novillo <dnovillo@redhat.com> |
4985cde3 RH |
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 | |
9dcd6f09 | 10 | the Free Software Foundation; either version 3, or (at your option) |
4985cde3 RH |
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 | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
4985cde3 RH |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
6de9cd9a | 25 | #include "tm.h" |
4985cde3 | 26 | #include "tree.h" |
6de9cd9a DN |
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" | |
4985cde3 | 35 | #include "flags.h" |
6de9cd9a DN |
36 | #include "tree-flow.h" |
37 | #include "tree-dump.h" | |
4985cde3 | 38 | #include "timevar.h" |
4985cde3 | 39 | #include "function.h" |
6de9cd9a DN |
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" | |
4985cde3 | 47 | #include "ggc.h" |
6de9cd9a | 48 | #include "cgraph.h" |
9f8628ba | 49 | #include "graph.h" |
2b271002 | 50 | #include "cfgloop.h" |
e21aff8a | 51 | #include "except.h" |
6de9cd9a | 52 | |
6de9cd9a | 53 | |
6de9cd9a DN |
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 | |
b8698a0f | 60 | /* Don't bother doing anything if the program has errors. |
7a388ee4 JH |
61 | We have to pass down the queue if we already went into SSA */ |
62 | && (!(errorcount || sorrycount) || gimple_in_ssa_p (cfun))); | |
6de9cd9a DN |
63 | } |
64 | ||
8ddbbcae | 65 | struct gimple_opt_pass pass_all_optimizations = |
6de9cd9a | 66 | { |
8ddbbcae JH |
67 | { |
68 | GIMPLE_PASS, | |
cf400ddb | 69 | "*all_optimizations", /* name */ |
6de9cd9a DN |
70 | gate_all_optimizations, /* gate */ |
71 | NULL, /* execute */ | |
72 | NULL, /* sub */ | |
73 | NULL, /* next */ | |
74 | 0, /* static_pass_number */ | |
7072a650 | 75 | TV_NONE, /* tv_id */ |
6de9cd9a DN |
76 | 0, /* properties_required */ |
77 | 0, /* properties_provided */ | |
78 | 0, /* properties_destroyed */ | |
79 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
80 | 0 /* todo_flags_finish */ |
81 | } | |
6de9cd9a DN |
82 | }; |
83 | ||
7a388ee4 JH |
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. */ | |
d7f09764 | 90 | return (!errorcount && !sorrycount && !in_lto_p); |
7a388ee4 JH |
91 | } |
92 | ||
8ddbbcae | 93 | struct simple_ipa_opt_pass pass_early_local_passes = |
d63db217 | 94 | { |
8ddbbcae JH |
95 | { |
96 | SIMPLE_IPA_PASS, | |
7a388ee4 JH |
97 | "early_local_cleanups", /* name */ |
98 | gate_all_early_local_passes, /* gate */ | |
d63db217 JH |
99 | NULL, /* execute */ |
100 | NULL, /* sub */ | |
101 | NULL, /* next */ | |
102 | 0, /* static_pass_number */ | |
7072a650 | 103 | TV_NONE, /* tv_id */ |
d63db217 JH |
104 | 0, /* properties_required */ |
105 | 0, /* properties_provided */ | |
106 | 0, /* properties_destroyed */ | |
107 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
108 | TODO_remove_functions /* todo_flags_finish */ |
109 | } | |
d63db217 JH |
110 | }; |
111 | ||
7a388ee4 JH |
112 | static unsigned int |
113 | execute_early_local_optimizations (void) | |
114 | { | |
bad5e2b5 JH |
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. */ | |
7e8b322a | 119 | if (cgraph_state < CGRAPH_STATE_IPA_SSA) |
7a388ee4 JH |
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 | ||
8ddbbcae | 134 | struct gimple_opt_pass pass_all_early_optimizations = |
7a388ee4 | 135 | { |
8ddbbcae JH |
136 | { |
137 | GIMPLE_PASS, | |
7a388ee4 JH |
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 */ | |
7072a650 | 144 | TV_NONE, /* tv_id */ |
7a388ee4 JH |
145 | 0, /* properties_required */ |
146 | 0, /* properties_provided */ | |
147 | 0, /* properties_destroyed */ | |
148 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
149 | 0 /* todo_flags_finish */ |
150 | } | |
7a388ee4 JH |
151 | }; |
152 | ||
d63db217 JH |
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 | ||
c2924966 | 158 | static unsigned int |
d63db217 JH |
159 | execute_cleanup_cfg_pre_ipa (void) |
160 | { | |
161 | cleanup_tree_cfg (); | |
c2924966 | 162 | return 0; |
d63db217 JH |
163 | } |
164 | ||
8ddbbcae | 165 | struct gimple_opt_pass pass_cleanup_cfg = |
d63db217 | 166 | { |
8ddbbcae JH |
167 | { |
168 | GIMPLE_PASS, | |
d63db217 JH |
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 */ | |
7072a650 | 175 | TV_NONE, /* tv_id */ |
d63db217 JH |
176 | PROP_cfg, /* properties_required */ |
177 | 0, /* properties_provided */ | |
178 | 0, /* properties_destroyed */ | |
179 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
180 | TODO_dump_func /* todo_flags_finish */ |
181 | } | |
d63db217 JH |
182 | }; |
183 | ||
184 | ||
165b54c3 SB |
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 | ||
c2924966 | 190 | static unsigned int |
165b54c3 SB |
191 | execute_cleanup_cfg_post_optimizing (void) |
192 | { | |
1ff54bfb | 193 | fold_cond_expr_cond (); |
165b54c3 SB |
194 | cleanup_tree_cfg (); |
195 | cleanup_dead_labels (); | |
196 | group_case_labels (); | |
c2924966 | 197 | return 0; |
165b54c3 SB |
198 | } |
199 | ||
8ddbbcae | 200 | struct gimple_opt_pass pass_cleanup_cfg_post_optimizing = |
165b54c3 | 201 | { |
8ddbbcae JH |
202 | { |
203 | GIMPLE_PASS, | |
e0a42b0f | 204 | "optimized", /* name */ |
165b54c3 SB |
205 | NULL, /* gate */ |
206 | execute_cleanup_cfg_post_optimizing, /* execute */ | |
207 | NULL, /* sub */ | |
208 | NULL, /* next */ | |
209 | 0, /* static_pass_number */ | |
7072a650 | 210 | TV_NONE, /* tv_id */ |
165b54c3 SB |
211 | PROP_cfg, /* properties_required */ |
212 | 0, /* properties_provided */ | |
213 | 0, /* properties_destroyed */ | |
214 | 0, /* todo_flags_start */ | |
8ddbbcae | 215 | TODO_dump_func /* todo_flags_finish */ |
4e3825db | 216 | | TODO_remove_unused_locals |
8ddbbcae | 217 | } |
165b54c3 SB |
218 | }; |
219 | ||
6de9cd9a DN |
220 | /* Pass: do the actions required to finish with tree-ssa optimization |
221 | passes. */ | |
222 | ||
4e3825db | 223 | unsigned int |
242229bb | 224 | execute_free_datastructures (void) |
6de9cd9a | 225 | { |
6de9cd9a | 226 | free_dominance_info (CDI_DOMINATORS); |
6844185d | 227 | free_dominance_info (CDI_POST_DOMINATORS); |
6de9cd9a | 228 | |
4e3825db MM |
229 | /* And get rid of annotations we no longer need. */ |
230 | delete_tree_cfg_annotations (); | |
231 | ||
c2924966 | 232 | return 0; |
6844185d | 233 | } |
6de9cd9a | 234 | |
4f6c2131 | 235 | /* Pass: fixup_cfg. IPA passes, compilation of earlier functions or inlining |
1084e689 JH |
236 | might have changed some properties, such as marked functions nothrow. |
237 | Remove redundant edges and basic blocks, and create new ones if necessary. | |
e21aff8a | 238 | |
873aa8f5 JH |
239 | This pass can't be executed as stand alone pass from pass manager, because |
240 | in between inlining and this fixup the verify_flow_info would fail. */ | |
241 | ||
242 | unsigned int | |
e21aff8a SB |
243 | execute_fixup_cfg (void) |
244 | { | |
245 | basic_block bb; | |
726a989a | 246 | gimple_stmt_iterator gsi; |
59e50498 | 247 | int todo = gimple_in_ssa_p (cfun) ? TODO_verify_ssa : 0; |
965b98d0 JH |
248 | gcov_type count_scale; |
249 | edge e; | |
250 | edge_iterator ei; | |
251 | ||
252 | if (ENTRY_BLOCK_PTR->count) | |
253 | count_scale = (cgraph_node (current_function_decl)->count * REG_BR_PROB_BASE | |
254 | + ENTRY_BLOCK_PTR->count / 2) / ENTRY_BLOCK_PTR->count; | |
255 | else | |
256 | count_scale = REG_BR_PROB_BASE; | |
257 | ||
43558bcc JH |
258 | ENTRY_BLOCK_PTR->count = cgraph_node (current_function_decl)->count; |
259 | EXIT_BLOCK_PTR->count = (EXIT_BLOCK_PTR->count * count_scale | |
260 | + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE; | |
261 | ||
965b98d0 JH |
262 | FOR_EACH_BB (bb) |
263 | { | |
264 | bb->count = (bb->count * count_scale | |
265 | + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE; | |
266 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
267 | { | |
268 | gimple stmt = gsi_stmt (gsi); | |
269 | tree decl = is_gimple_call (stmt) | |
270 | ? gimple_call_fndecl (stmt) | |
271 | : NULL; | |
272 | ||
273 | if (decl | |
274 | && gimple_call_flags (stmt) & (ECF_CONST | |
b8698a0f | 275 | | ECF_PURE |
965b98d0 JH |
276 | | ECF_LOOPING_CONST_OR_PURE)) |
277 | { | |
278 | if (gimple_in_ssa_p (cfun)) | |
279 | { | |
280 | todo |= TODO_update_ssa | TODO_cleanup_cfg; | |
281 | mark_symbols_for_renaming (stmt); | |
282 | update_stmt (stmt); | |
283 | } | |
284 | } | |
285 | ||
286 | maybe_clean_eh_stmt (stmt); | |
287 | } | |
288 | ||
289 | if (gimple_purge_dead_eh_edges (bb)) | |
290 | todo |= TODO_cleanup_cfg; | |
291 | FOR_EACH_EDGE (e, ei, bb->succs) | |
292 | e->count = (e->count * count_scale | |
293 | + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE; | |
294 | } | |
295 | if (count_scale != REG_BR_PROB_BASE) | |
296 | compute_function_frequency (); | |
4f6c2131 | 297 | |
4f6c2131 EB |
298 | /* Dump a textual representation of the flowgraph. */ |
299 | if (dump_file) | |
726a989a | 300 | gimple_dump_cfg (dump_file, dump_flags); |
4f6c2131 | 301 | |
59e50498 | 302 | return todo; |
e21aff8a SB |
303 | } |
304 | ||
6f1873a1 JH |
305 | struct gimple_opt_pass pass_fixup_cfg = |
306 | { | |
307 | { | |
308 | GIMPLE_PASS, | |
e0a42b0f | 309 | "*free_cfg_annotations", /* name */ |
6f1873a1 | 310 | NULL, /* gate */ |
e0a42b0f | 311 | execute_fixup_cfg, /* execute */ |
6f1873a1 JH |
312 | NULL, /* sub */ |
313 | NULL, /* next */ | |
314 | 0, /* static_pass_number */ | |
7072a650 | 315 | TV_NONE, /* tv_id */ |
6f1873a1 JH |
316 | PROP_cfg, /* properties_required */ |
317 | 0, /* properties_provided */ | |
318 | 0, /* properties_destroyed */ | |
319 | 0, /* todo_flags_start */ | |
320 | 0 /* todo_flags_finish */ | |
321 | } | |
322 | }; | |
323 | ||
33c94679 DN |
324 | /* Do the actions required to initialize internal data structures used |
325 | in tree-ssa optimization passes. */ | |
326 | ||
c2924966 | 327 | static unsigned int |
33c94679 DN |
328 | execute_init_datastructures (void) |
329 | { | |
330 | /* Allocate hash tables, arrays and other structures. */ | |
5db9ba0c | 331 | init_tree_ssa (cfun); |
c2924966 | 332 | return 0; |
33c94679 DN |
333 | } |
334 | ||
8ddbbcae | 335 | struct gimple_opt_pass pass_init_datastructures = |
33c94679 | 336 | { |
8ddbbcae JH |
337 | { |
338 | GIMPLE_PASS, | |
e0a42b0f | 339 | "*init_datastructures", /* name */ |
c72321c9 | 340 | NULL, /* gate */ |
33c94679 DN |
341 | execute_init_datastructures, /* execute */ |
342 | NULL, /* sub */ | |
343 | NULL, /* next */ | |
344 | 0, /* static_pass_number */ | |
7072a650 | 345 | TV_NONE, /* tv_id */ |
33c94679 DN |
346 | PROP_cfg, /* properties_required */ |
347 | 0, /* properties_provided */ | |
348 | 0, /* properties_destroyed */ | |
349 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
350 | 0 /* todo_flags_finish */ |
351 | } | |
33c94679 DN |
352 | }; |
353 | ||
e21aff8a SB |
354 | void |
355 | tree_lowering_passes (tree fn) | |
356 | { | |
357 | tree saved_current_function_decl = current_function_decl; | |
358 | ||
359 | current_function_decl = fn; | |
360 | push_cfun (DECL_STRUCT_FUNCTION (fn)); | |
726a989a | 361 | gimple_register_cfg_hooks (); |
e21aff8a SB |
362 | bitmap_obstack_initialize (NULL); |
363 | execute_pass_list (all_lowering_passes); | |
7a388ee4 | 364 | if (optimize && cgraph_global_info_ready) |
8ddbbcae | 365 | execute_pass_list (pass_early_local_passes.pass.sub); |
e21aff8a | 366 | free_dominance_info (CDI_POST_DOMINATORS); |
7a388ee4 | 367 | free_dominance_info (CDI_DOMINATORS); |
e21aff8a SB |
368 | compact_blocks (); |
369 | current_function_decl = saved_current_function_decl; | |
370 | bitmap_obstack_release (NULL); | |
371 | pop_cfun (); | |
372 | } | |
6de9cd9a | 373 | \f |
4985cde3 RH |
374 | /* For functions-as-trees languages, this performs all optimization and |
375 | compilation for FNDECL. */ | |
376 | ||
377 | void | |
0f0377f6 | 378 | tree_rest_of_compilation (tree fndecl) |
4985cde3 | 379 | { |
5d4854c8 RH |
380 | location_t saved_loc; |
381 | ||
4985cde3 RH |
382 | timevar_push (TV_EXPAND); |
383 | ||
7e8b322a | 384 | gcc_assert (cgraph_global_info_ready); |
4985cde3 | 385 | |
de6bd996 JH |
386 | /* Initialize the default bitmap obstack. */ |
387 | bitmap_obstack_initialize (NULL); | |
388 | ||
4985cde3 RH |
389 | /* Initialize the RTL code for the function. */ |
390 | current_function_decl = 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. */ | |
e3b5732b | 399 | cfun->dont_save_pending_sizes_p = 1; |
b8698a0f | 400 | |
726a989a | 401 | gimple_register_cfg_hooks (); |
4985cde3 | 402 | |
7932a3db | 403 | bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/ |
d7f09764 DN |
404 | |
405 | execute_all_ipa_transforms (); | |
406 | ||
2f8e398b PB |
407 | /* Perform all tree transforms and optimizations. */ |
408 | execute_pass_list (all_passes); | |
b8698a0f | 409 | |
7932a3db | 410 | bitmap_obstack_release (®_obstack); |
4985cde3 | 411 | |
7932a3db NS |
412 | /* Release the default bitmap obstack. */ |
413 | bitmap_obstack_release (NULL); | |
b8698a0f | 414 | |
db2960f4 | 415 | set_cfun (NULL); |
4985cde3 RH |
416 | |
417 | /* If requested, warn about function definitions where the function will | |
418 | return a value (usually of some struct or union type) which itself will | |
419 | take up a lot of stack space. */ | |
420 | if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl)) | |
421 | { | |
422 | tree ret_type = TREE_TYPE (TREE_TYPE (fndecl)); | |
423 | ||
424 | if (ret_type && TYPE_SIZE_UNIT (ret_type) | |
425 | && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST | |
426 | && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type), | |
427 | larger_than_size)) | |
428 | { | |
4985cde3 RH |
429 | unsigned int size_as_int |
430 | = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type)); | |
431 | ||
432 | if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0) | |
e8fc888d | 433 | warning (OPT_Wlarger_than_eq, "size of return value of %q+D is %u bytes", |
dee15844 | 434 | fndecl, size_as_int); |
4985cde3 | 435 | else |
e8fc888d | 436 | warning (OPT_Wlarger_than_eq, "size of return value of %q+D is larger than %wd bytes", |
dee15844 | 437 | fndecl, larger_than_size); |
4985cde3 RH |
438 | } |
439 | } | |
440 | ||
726a989a | 441 | gimple_set_body (fndecl, NULL); |
7e8b322a JH |
442 | if (DECL_STRUCT_FUNCTION (fndecl) == 0 |
443 | && !cgraph_node (fndecl)->origin) | |
4985cde3 | 444 | { |
7e8b322a JH |
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; | |
4985cde3 RH |
452 | } |
453 | ||
5d4854c8 RH |
454 | input_location = saved_loc; |
455 | ||
6de9cd9a | 456 | ggc_collect (); |
4985cde3 RH |
457 | timevar_pop (TV_EXPAND); |
458 | } |