comparison gcc/predict.c @ 67:f6334be47118

update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 22 Mar 2011 17:18:12 +0900
parents b7f97abdc517
children 04ced10e8804
comparison
equal deleted inserted replaced
65:65488c3d617d 67:f6334be47118
1 /* Branch prediction routines for the GNU compiler. 1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc. 3 Free Software Foundation, Inc.
4 4
5 This file is part of GCC. 5 This file is part of GCC.
6 6
7 GCC is free software; you can redistribute it and/or modify it under 7 GCC is free software; you can redistribute it and/or modify it under
41 #include "regs.h" 41 #include "regs.h"
42 #include "flags.h" 42 #include "flags.h"
43 #include "output.h" 43 #include "output.h"
44 #include "function.h" 44 #include "function.h"
45 #include "except.h" 45 #include "except.h"
46 #include "toplev.h" 46 #include "diagnostic-core.h"
47 #include "recog.h" 47 #include "recog.h"
48 #include "expr.h" 48 #include "expr.h"
49 #include "predict.h" 49 #include "predict.h"
50 #include "coverage.h" 50 #include "coverage.h"
51 #include "sreal.h" 51 #include "sreal.h"
75 #define PROB_ALWAYS (REG_BR_PROB_BASE) 75 #define PROB_ALWAYS (REG_BR_PROB_BASE)
76 76
77 static void combine_predictions_for_insn (rtx, basic_block); 77 static void combine_predictions_for_insn (rtx, basic_block);
78 static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int); 78 static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int);
79 static void predict_paths_leading_to (basic_block, enum br_predictor, enum prediction); 79 static void predict_paths_leading_to (basic_block, enum br_predictor, enum prediction);
80 static void choose_function_section (void); 80 static void predict_paths_leading_to_edge (edge, enum br_predictor, enum prediction);
81 static bool can_predict_insn_p (const_rtx); 81 static bool can_predict_insn_p (const_rtx);
82 82
83 /* Information we hold about each branch predictor. 83 /* Information we hold about each branch predictor.
84 Filled using information from predict.def. */ 84 Filled using information from predict.def. */
85 85
124 if (profile_status == PROFILE_ABSENT) 124 if (profile_status == PROFILE_ABSENT)
125 return true; 125 return true;
126 if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE 126 if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
127 && freq <= (ENTRY_BLOCK_PTR->frequency * 2 / 3)) 127 && freq <= (ENTRY_BLOCK_PTR->frequency * 2 / 3))
128 return false; 128 return false;
129 if (freq < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)) 129 if (freq < ENTRY_BLOCK_PTR->frequency / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
130 return false; 130 return false;
131 return true; 131 return true;
132 } 132 }
133 133
134 /* Return TRUE if frequency FREQ is considered to be hot. */ 134 /* Return TRUE if frequency FREQ is considered to be hot. */
165 && (edge->count 165 && (edge->count
166 <= profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION))) 166 <= profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
167 return false; 167 return false;
168 if (edge->caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED 168 if (edge->caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
169 || edge->callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED) 169 || edge->callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
170 return false;
171 if (edge->caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
172 && edge->callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE)
170 return false; 173 return false;
171 if (optimize_size) 174 if (optimize_size)
172 return false; 175 return false;
173 if (edge->caller->frequency == NODE_FREQUENCY_HOT) 176 if (edge->caller->frequency == NODE_FREQUENCY_HOT)
174 return true; 177 return true;
383 /* This map contains for a basic block the list of predictions for the 386 /* This map contains for a basic block the list of predictions for the
384 outgoing edges. */ 387 outgoing edges. */
385 388
386 static struct pointer_map_t *bb_predictions; 389 static struct pointer_map_t *bb_predictions;
387 390
391 /* Structure representing predictions in tree level. */
392
393 struct edge_prediction {
394 struct edge_prediction *ep_next;
395 edge ep_edge;
396 enum br_predictor ep_predictor;
397 int ep_probability;
398 };
399
388 /* Return true if the one of outgoing edges is already predicted by 400 /* Return true if the one of outgoing edges is already predicted by
389 PREDICTOR. */ 401 PREDICTOR. */
390 402
391 bool 403 bool
392 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor) 404 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
936 edge ex; 948 edge ex;
937 949
938 exits = get_loop_exit_edges (loop); 950 exits = get_loop_exit_edges (loop);
939 n_exits = VEC_length (edge, exits); 951 n_exits = VEC_length (edge, exits);
940 952
941 for (j = 0; VEC_iterate (edge, exits, j, ex); j++) 953 FOR_EACH_VEC_ELT (edge, exits, j, ex)
942 { 954 {
943 tree niter = NULL; 955 tree niter = NULL;
944 HOST_WIDE_INT nitercst; 956 HOST_WIDE_INT nitercst;
945 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS); 957 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
946 int probability; 958 int probability;
1174 return NULL_TREE; 1186 return NULL_TREE;
1175 1187
1176 def = SSA_NAME_DEF_STMT (op0); 1188 def = SSA_NAME_DEF_STMT (op0);
1177 1189
1178 /* If we were already here, break the infinite cycle. */ 1190 /* If we were already here, break the infinite cycle. */
1179 if (bitmap_bit_p (visited, SSA_NAME_VERSION (op0))) 1191 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
1180 return NULL; 1192 return NULL;
1181 bitmap_set_bit (visited, SSA_NAME_VERSION (op0));
1182 1193
1183 if (gimple_code (def) == GIMPLE_PHI) 1194 if (gimple_code (def) == GIMPLE_PHI)
1184 { 1195 {
1185 /* All the arguments of the PHI node must have the same constant 1196 /* All the arguments of the PHI node must have the same constant
1186 length. */ 1197 length. */
1324 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL 1335 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
1325 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT 1336 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
1326 && gimple_call_num_args (stmt) == 2) 1337 && gimple_call_num_args (stmt) == 2)
1327 { 1338 {
1328 var = gimple_call_lhs (stmt); 1339 var = gimple_call_lhs (stmt);
1329 ass_stmt = gimple_build_assign (var, gimple_call_arg (stmt, 0)); 1340 if (var)
1330 1341 {
1331 gsi_replace (&bi, ass_stmt, true); 1342 ass_stmt
1343 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
1344 gsi_replace (&bi, ass_stmt, true);
1345 }
1346 else
1347 {
1348 gsi_remove (&bi, true);
1349 continue;
1350 }
1332 } 1351 }
1333 } 1352 }
1334 gsi_next (&bi); 1353 gsi_next (&bi);
1335 } 1354 }
1336 } 1355 }
1538 if (i != phi_num_args) 1557 if (i != phi_num_args)
1539 for (i = 0; i < phi_num_args; i++) 1558 for (i = 0; i < phi_num_args; i++)
1540 { 1559 {
1541 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction); 1560 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
1542 if (pred != PRED_NO_PREDICTION) 1561 if (pred != PRED_NO_PREDICTION)
1543 predict_paths_leading_to (gimple_phi_arg_edge (phi, i)->src, pred, 1562 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
1544 direction); 1563 direction);
1545 } 1564 }
1546 } 1565 }
1547 1566
1548 /* Look for basic block that contains unlikely to happen events 1567 /* Look for basic block that contains unlikely to happen events
1549 (such as noreturn calls) and mark all paths leading to execution 1568 (such as noreturn calls) and mark all paths leading to execution
1781 set of all blocks postdominated by BB. */ 1800 set of all blocks postdominated by BB. */
1782 FOR_EACH_EDGE (e, ei, cur->preds) 1801 FOR_EACH_EDGE (e, ei, cur->preds)
1783 if (e->src->index >= NUM_FIXED_BLOCKS 1802 if (e->src->index >= NUM_FIXED_BLOCKS
1784 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb)) 1803 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
1785 { 1804 {
1805 edge e2;
1806 edge_iterator ei2;
1807 bool found = false;
1808
1809 /* Ignore fake edges and eh, we predict them as not taken anyway. */
1810 if (e->flags & (EDGE_EH | EDGE_FAKE))
1811 continue;
1786 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb)); 1812 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
1787 predict_edge_def (e, pred, taken); 1813
1814 /* See if there is how many edge from e->src that is not abnormal
1815 and does not lead to BB. */
1816 FOR_EACH_EDGE (e2, ei2, e->src->succs)
1817 if (e2 != e
1818 && !(e2->flags & (EDGE_EH | EDGE_FAKE))
1819 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb))
1820 {
1821 found = true;
1822 break;
1823 }
1824
1825 /* If there is non-abnormal path leaving e->src, predict edge
1826 using predictor. Otherwise we need to look for paths
1827 leading to e->src. */
1828 if (found)
1829 predict_edge_def (e, pred, taken);
1830 else
1831 predict_paths_for_bb (e->src, e->src, pred, taken);
1788 } 1832 }
1789 for (son = first_dom_son (CDI_POST_DOMINATORS, cur); 1833 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
1790 son; 1834 son;
1791 son = next_dom_son (CDI_POST_DOMINATORS, son)) 1835 son = next_dom_son (CDI_POST_DOMINATORS, son))
1792 predict_paths_for_bb (son, bb, pred, taken); 1836 predict_paths_for_bb (son, bb, pred, taken);
1798 static void 1842 static void
1799 predict_paths_leading_to (basic_block bb, enum br_predictor pred, 1843 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
1800 enum prediction taken) 1844 enum prediction taken)
1801 { 1845 {
1802 predict_paths_for_bb (bb, bb, pred, taken); 1846 predict_paths_for_bb (bb, bb, pred, taken);
1847 }
1848
1849 /* Like predict_paths_leading_to but take edge instead of basic block. */
1850
1851 static void
1852 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
1853 enum prediction taken)
1854 {
1855 bool has_nonloop_edge = false;
1856 edge_iterator ei;
1857 edge e2;
1858
1859 basic_block bb = e->src;
1860 FOR_EACH_EDGE (e2, ei, bb->succs)
1861 if (e2->dest != e->src && e2->dest != e->dest
1862 && !(e->flags & (EDGE_EH | EDGE_FAKE))
1863 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
1864 {
1865 has_nonloop_edge = true;
1866 break;
1867 }
1868 if (!has_nonloop_edge)
1869 predict_paths_for_bb (bb, bb, pred, taken);
1870 else
1871 predict_edge_def (e, pred, taken);
1803 } 1872 }
1804 1873
1805 /* This is used to carry information about basic blocks. It is 1874 /* This is used to carry information about basic blocks. It is
1806 attached to the AUX field of the standard CFG block. */ 1875 attached to the AUX field of the standard CFG block. */
1807 1876
1850 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi) 1919 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
1851 { 1920 {
1852 edge_iterator ei; 1921 edge_iterator ei;
1853 int count = 0; 1922 int count = 0;
1854 1923
1855 /* The outermost "loop" includes the exit block, which we can not
1856 look up via BASIC_BLOCK. Detect this and use EXIT_BLOCK_PTR
1857 directly. Do the same for the entry block. */
1858 bb = BASIC_BLOCK (i); 1924 bb = BASIC_BLOCK (i);
1859 1925
1860 FOR_EACH_EDGE (e, ei, bb->preds) 1926 FOR_EACH_EDGE (e, ei, bb->preds)
1861 { 1927 {
1862 bool visit = bitmap_bit_p (tovisit, e->src->index); 1928 bool visit = bitmap_bit_p (tovisit, e->src->index);
1867 fprintf (dump_file, 1933 fprintf (dump_file,
1868 "Irreducible region hit, ignoring edge to %i->%i\n", 1934 "Irreducible region hit, ignoring edge to %i->%i\n",
1869 e->src->index, bb->index); 1935 e->src->index, bb->index);
1870 } 1936 }
1871 BLOCK_INFO (bb)->npredecessors = count; 1937 BLOCK_INFO (bb)->npredecessors = count;
1938 /* When function never returns, we will never process exit block. */
1939 if (!count && bb == EXIT_BLOCK_PTR)
1940 bb->count = bb->frequency = 0;
1872 } 1941 }
1873 1942
1874 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one)); 1943 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
1875 last = head; 1944 last = head;
1876 for (bb = head; bb; bb = nextbb) 1945 for (bb = head; bb; bb = nextbb)
2147 2216
2148 free_aux_for_blocks (); 2217 free_aux_for_blocks ();
2149 free_aux_for_edges (); 2218 free_aux_for_edges ();
2150 } 2219 }
2151 compute_function_frequency (); 2220 compute_function_frequency ();
2152 if (flag_reorder_functions)
2153 choose_function_section ();
2154 } 2221 }
2155 2222
2156 /* Decide whether function is hot, cold or unlikely executed. */ 2223 /* Decide whether function is hot, cold or unlikely executed. */
2157 void 2224 void
2158 compute_function_frequency (void) 2225 compute_function_frequency (void)
2159 { 2226 {
2160 basic_block bb; 2227 basic_block bb;
2161 struct cgraph_node *node = cgraph_node (current_function_decl); 2228 struct cgraph_node *node = cgraph_node (current_function_decl);
2229 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
2230 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
2231 node->only_called_at_startup = true;
2232 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
2233 node->only_called_at_exit = true;
2162 2234
2163 if (!profile_info || !flag_branch_probabilities) 2235 if (!profile_info || !flag_branch_probabilities)
2164 { 2236 {
2165 int flags = flags_from_decl_or_type (current_function_decl); 2237 int flags = flags_from_decl_or_type (current_function_decl);
2166 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)) 2238 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
2187 return; 2259 return;
2188 } 2260 }
2189 if (!probably_never_executed_bb_p (bb)) 2261 if (!probably_never_executed_bb_p (bb))
2190 node->frequency = NODE_FREQUENCY_NORMAL; 2262 node->frequency = NODE_FREQUENCY_NORMAL;
2191 } 2263 }
2192 }
2193
2194 /* Choose appropriate section for the function. */
2195 static void
2196 choose_function_section (void)
2197 {
2198 struct cgraph_node *node = cgraph_node (current_function_decl);
2199 if (DECL_SECTION_NAME (current_function_decl)
2200 || !targetm.have_named_sections
2201 /* Theoretically we can split the gnu.linkonce text section too,
2202 but this requires more work as the frequency needs to match
2203 for all generated objects so we need to merge the frequency
2204 of all instances. For now just never set frequency for these. */
2205 || DECL_ONE_ONLY (current_function_decl))
2206 return;
2207
2208 /* If we are doing the partitioning optimization, let the optimization
2209 choose the correct section into which to put things. */
2210
2211 if (flag_reorder_blocks_and_partition)
2212 return;
2213
2214 if (node->frequency == NODE_FREQUENCY_HOT)
2215 DECL_SECTION_NAME (current_function_decl) =
2216 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
2217 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2218 DECL_SECTION_NAME (current_function_decl) =
2219 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
2220 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
2221 } 2264 }
2222 2265
2223 static bool 2266 static bool
2224 gate_estimate_probability (void) 2267 gate_estimate_probability (void)
2225 { 2268 {
2277 0, /* properties_destroyed */ 2320 0, /* properties_destroyed */
2278 0, /* todo_flags_start */ 2321 0, /* todo_flags_start */
2279 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */ 2322 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
2280 } 2323 }
2281 }; 2324 };
2325
2326 /* Rebuild function frequencies. Passes are in general expected to
2327 maintain profile by hand, however in some cases this is not possible:
2328 for example when inlining several functions with loops freuqencies might run
2329 out of scale and thus needs to be recomputed. */
2330
2331 void
2332 rebuild_frequencies (void)
2333 {
2334 timevar_push (TV_REBUILD_FREQUENCIES);
2335 if (profile_status == PROFILE_GUESSED)
2336 {
2337 loop_optimizer_init (0);
2338 add_noreturn_fake_exit_edges ();
2339 mark_irreducible_loops ();
2340 connect_infinite_loops_to_exit ();
2341 estimate_bb_frequencies ();
2342 remove_fake_exit_edges ();
2343 loop_optimizer_finalize ();
2344 }
2345 else if (profile_status == PROFILE_READ)
2346 counts_to_freqs ();
2347 else
2348 gcc_unreachable ();
2349 timevar_pop (TV_REBUILD_FREQUENCIES);
2350 }