PR c++/55261
[official-gcc.git] / gcc / tree-optimize.c
blobe44756d120c505607c19f4cf74021728605419cd
1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "flags.h"
30 #include "tree-flow.h"
31 #include "function.h"
32 #include "langhooks.h"
33 #include "diagnostic-core.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "cgraph.h"
37 #include "tree-inline.h"
38 #include "tree-pass.h"
39 #include "ggc.h"
40 #include "cgraph.h"
41 #include "graph.h"
42 #include "cfgloop.h"
43 #include "except.h"
44 #include "plugin.h"
47 /* Pass: cleanup the CFG just before expanding trees to RTL.
48 This is just a round of label cleanups and case node grouping
49 because after the tree optimizers have run such cleanups may
50 be necessary. */
52 static unsigned int
53 execute_cleanup_cfg_post_optimizing (void)
55 unsigned int todo = 0;
56 if (cleanup_tree_cfg ())
57 todo |= TODO_update_ssa;
58 maybe_remove_unreachable_handlers ();
59 cleanup_dead_labels ();
60 group_case_labels ();
61 if ((flag_compare_debug_opt || flag_compare_debug)
62 && flag_dump_final_insns)
64 FILE *final_output = fopen (flag_dump_final_insns, "a");
66 if (!final_output)
68 error ("could not open final insn dump file %qs: %m",
69 flag_dump_final_insns);
70 flag_dump_final_insns = NULL;
72 else
74 int save_unnumbered = flag_dump_unnumbered;
75 int save_noaddr = flag_dump_noaddr;
77 flag_dump_noaddr = flag_dump_unnumbered = 1;
78 fprintf (final_output, "\n");
79 dump_enumerated_decls (final_output, dump_flags | TDF_NOUID);
80 flag_dump_noaddr = save_noaddr;
81 flag_dump_unnumbered = save_unnumbered;
82 if (fclose (final_output))
84 error ("could not close final insn dump file %qs: %m",
85 flag_dump_final_insns);
86 flag_dump_final_insns = NULL;
90 return todo;
93 struct gimple_opt_pass pass_cleanup_cfg_post_optimizing =
96 GIMPLE_PASS,
97 "optimized", /* name */
98 OPTGROUP_NONE, /* optinfo_flags */
99 NULL, /* gate */
100 execute_cleanup_cfg_post_optimizing, /* execute */
101 NULL, /* sub */
102 NULL, /* next */
103 0, /* static_pass_number */
104 TV_TREE_CLEANUP_CFG, /* tv_id */
105 PROP_cfg, /* properties_required */
106 0, /* properties_provided */
107 0, /* properties_destroyed */
108 0, /* todo_flags_start */
109 TODO_remove_unused_locals /* todo_flags_finish */
113 /* IPA passes, compilation of earlier functions or inlining
114 might have changed some properties, such as marked functions nothrow,
115 pure, const or noreturn.
116 Remove redundant edges and basic blocks, and create new ones if necessary.
118 This pass can't be executed as stand alone pass from pass manager, because
119 in between inlining and this fixup the verify_flow_info would fail. */
121 unsigned int
122 execute_fixup_cfg (void)
124 basic_block bb;
125 gimple_stmt_iterator gsi;
126 int todo = gimple_in_ssa_p (cfun) ? TODO_verify_ssa : 0;
127 gcov_type count_scale;
128 edge e;
129 edge_iterator ei;
131 if (ENTRY_BLOCK_PTR->count)
132 count_scale = ((cgraph_get_node (current_function_decl)->count
133 * REG_BR_PROB_BASE + ENTRY_BLOCK_PTR->count / 2)
134 / ENTRY_BLOCK_PTR->count);
135 else
136 count_scale = REG_BR_PROB_BASE;
138 ENTRY_BLOCK_PTR->count = cgraph_get_node (current_function_decl)->count;
139 EXIT_BLOCK_PTR->count = (EXIT_BLOCK_PTR->count * count_scale
140 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
142 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
143 e->count = (e->count * count_scale
144 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
146 FOR_EACH_BB (bb)
148 bb->count = (bb->count * count_scale
149 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
150 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
152 gimple stmt = gsi_stmt (gsi);
153 tree decl = is_gimple_call (stmt)
154 ? gimple_call_fndecl (stmt)
155 : NULL;
156 if (decl)
158 int flags = gimple_call_flags (stmt);
159 if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
161 if (gimple_purge_dead_abnormal_call_edges (bb))
162 todo |= TODO_cleanup_cfg;
164 if (gimple_in_ssa_p (cfun))
166 todo |= TODO_update_ssa | TODO_cleanup_cfg;
167 update_stmt (stmt);
171 if (flags & ECF_NORETURN
172 && fixup_noreturn_call (stmt))
173 todo |= TODO_cleanup_cfg;
176 if (maybe_clean_eh_stmt (stmt)
177 && gimple_purge_dead_eh_edges (bb))
178 todo |= TODO_cleanup_cfg;
181 FOR_EACH_EDGE (e, ei, bb->succs)
182 e->count = (e->count * count_scale
183 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
185 /* If we have a basic block with no successors that does not
186 end with a control statement or a noreturn call end it with
187 a call to __builtin_unreachable. This situation can occur
188 when inlining a noreturn call that does in fact return. */
189 if (EDGE_COUNT (bb->succs) == 0)
191 gimple stmt = last_stmt (bb);
192 if (!stmt
193 || (!is_ctrl_stmt (stmt)
194 && (!is_gimple_call (stmt)
195 || (gimple_call_flags (stmt) & ECF_NORETURN) == 0)))
197 stmt = gimple_build_call
198 (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
199 gimple_stmt_iterator gsi = gsi_last_bb (bb);
200 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
204 if (count_scale != REG_BR_PROB_BASE)
205 compute_function_frequency ();
207 /* We just processed all calls. */
208 if (cfun->gimple_df)
209 vec_free (MODIFIED_NORETURN_CALLS (cfun));
211 /* Dump a textual representation of the flowgraph. */
212 if (dump_file)
213 gimple_dump_cfg (dump_file, dump_flags);
215 return todo;
218 struct gimple_opt_pass pass_fixup_cfg =
221 GIMPLE_PASS,
222 "*free_cfg_annotations", /* name */
223 OPTGROUP_NONE, /* optinfo_flags */
224 NULL, /* gate */
225 execute_fixup_cfg, /* execute */
226 NULL, /* sub */
227 NULL, /* next */
228 0, /* static_pass_number */
229 TV_NONE, /* tv_id */
230 PROP_cfg, /* properties_required */
231 0, /* properties_provided */
232 0, /* properties_destroyed */
233 0, /* todo_flags_start */
234 0 /* todo_flags_finish */