]>
Commit | Line | Data |
---|---|---|
f1ebdfc5 | 1 | /* Branch prediction routines for the GNU compiler. |
d1e082c2 | 2 | Copyright (C) 2000-2013 Free Software Foundation, Inc. |
f1ebdfc5 | 3 | |
bfdade77 | 4 | This file is part of GCC. |
f1ebdfc5 | 5 | |
bfdade77 RK |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
bfdade77 | 9 | version. |
f1ebdfc5 | 10 | |
bfdade77 RK |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
f1ebdfc5 | 15 | |
bfdade77 | 16 | You should have received a copy of the GNU General Public License |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
f1ebdfc5 JE |
19 | |
20 | /* References: | |
21 | ||
22 | [1] "Branch Prediction for Free" | |
23 | Ball and Larus; PLDI '93. | |
24 | [2] "Static Branch Frequency and Program Profile Analysis" | |
25 | Wu and Larus; MICRO-27. | |
26 | [3] "Corpus-based Static Branch Prediction" | |
3ef42a0c | 27 | Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */ |
f1ebdfc5 JE |
28 | |
29 | ||
30 | #include "config.h" | |
31 | #include "system.h" | |
4977bab6 ZW |
32 | #include "coretypes.h" |
33 | #include "tm.h" | |
f1ebdfc5 JE |
34 | #include "tree.h" |
35 | #include "rtl.h" | |
36 | #include "tm_p.h" | |
efc9bd41 | 37 | #include "hard-reg-set.h" |
f1ebdfc5 JE |
38 | #include "basic-block.h" |
39 | #include "insn-config.h" | |
40 | #include "regs.h" | |
f1ebdfc5 | 41 | #include "flags.h" |
f1ebdfc5 JE |
42 | #include "function.h" |
43 | #include "except.h" | |
718f9c0f | 44 | #include "diagnostic-core.h" |
f1ebdfc5 | 45 | #include "recog.h" |
f1ebdfc5 | 46 | #include "expr.h" |
4db384c9 | 47 | #include "predict.h" |
d79f9ec9 | 48 | #include "coverage.h" |
ac5e69da | 49 | #include "sreal.h" |
194734e9 JH |
50 | #include "params.h" |
51 | #include "target.h" | |
3d436d2a | 52 | #include "cfgloop.h" |
7a300452 | 53 | #include "tree-ssa.h" |
6de9cd9a | 54 | #include "ggc.h" |
6de9cd9a | 55 | #include "tree-pass.h" |
b6acab32 JH |
56 | #include "tree-scalar-evolution.h" |
57 | #include "cfgloop.h" | |
f06b0a10 | 58 | #include "pointer-set.h" |
8aa18a7d | 59 | |
fbe3b30b SB |
60 | /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, |
61 | 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */ | |
ac5e69da JZ |
62 | static sreal real_zero, real_one, real_almost_one, real_br_prob_base, |
63 | real_inv_br_prob_base, real_one_half, real_bb_freq_max; | |
f1ebdfc5 | 64 | |
b8698a0f | 65 | /* Random guesstimation given names. |
277b4867 | 66 | PROV_VERY_UNLIKELY should be small enough so basic block predicted |
84aacbfd | 67 | by it gets below HOT_BB_FREQUENCY_FRACTION. */ |
277b4867 | 68 | #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1) |
c66f079e | 69 | #define PROB_EVEN (REG_BR_PROB_BASE / 2) |
c66f079e RH |
70 | #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY) |
71 | #define PROB_ALWAYS (REG_BR_PROB_BASE) | |
f1ebdfc5 | 72 | |
79a490a9 | 73 | static void combine_predictions_for_insn (rtx, basic_block); |
6de9cd9a | 74 | static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int); |
3e4b9ad0 | 75 | static void predict_paths_leading_to (basic_block, enum br_predictor, enum prediction); |
5210bbc5 | 76 | static void predict_paths_leading_to_edge (edge, enum br_predictor, enum prediction); |
ed7a4b4b | 77 | static bool can_predict_insn_p (const_rtx); |
ee92cb46 | 78 | |
4db384c9 JH |
79 | /* Information we hold about each branch predictor. |
80 | Filled using information from predict.def. */ | |
bfdade77 | 81 | |
4db384c9 | 82 | struct predictor_info |
ee92cb46 | 83 | { |
8b60264b KG |
84 | const char *const name; /* Name used in the debugging dumps. */ |
85 | const int hitrate; /* Expected hitrate used by | |
86 | predict_insn_def call. */ | |
87 | const int flags; | |
4db384c9 | 88 | }; |
ee92cb46 | 89 | |
134d3a2e JH |
90 | /* Use given predictor without Dempster-Shaffer theory if it matches |
91 | using first_match heuristics. */ | |
92 | #define PRED_FLAG_FIRST_MATCH 1 | |
93 | ||
94 | /* Recompute hitrate in percent to our representation. */ | |
95 | ||
bfdade77 | 96 | #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100) |
134d3a2e JH |
97 | |
98 | #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS}, | |
bfdade77 | 99 | static const struct predictor_info predictor_info[]= { |
4db384c9 JH |
100 | #include "predict.def" |
101 | ||
dc297297 | 102 | /* Upper bound on predictors. */ |
134d3a2e | 103 | {NULL, 0, 0} |
4db384c9 JH |
104 | }; |
105 | #undef DEF_PREDICTOR | |
194734e9 | 106 | |
3250d724 | 107 | /* Return TRUE if frequency FREQ is considered to be hot. */ |
fb2fed03 JH |
108 | |
109 | static inline bool | |
2eb712b4 | 110 | maybe_hot_frequency_p (struct function *fun, int freq) |
3250d724 | 111 | { |
2eb712b4 | 112 | struct cgraph_node *node = cgraph_get_node (fun->decl); |
3250d724 JH |
113 | if (!profile_info || !flag_branch_probabilities) |
114 | { | |
5fefcf92 | 115 | if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED) |
3250d724 | 116 | return false; |
5fefcf92 | 117 | if (node->frequency == NODE_FREQUENCY_HOT) |
3250d724 JH |
118 | return true; |
119 | } | |
2eb712b4 | 120 | if (profile_status_for_function (fun) == PROFILE_ABSENT) |
c3702ff9 | 121 | return true; |
5fefcf92 | 122 | if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE |
2eb712b4 | 123 | && freq < (ENTRY_BLOCK_PTR_FOR_FUNCTION (fun)->frequency * 2 / 3)) |
5fefcf92 | 124 | return false; |
39c1b6db MP |
125 | if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0) |
126 | return false; | |
2eb712b4 MJ |
127 | if (freq < (ENTRY_BLOCK_PTR_FOR_FUNCTION (fun)->frequency |
128 | / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))) | |
3250d724 JH |
129 | return false; |
130 | return true; | |
131 | } | |
132 | ||
0208f7da JH |
133 | static gcov_type min_count = -1; |
134 | ||
135 | /* Determine the threshold for hot BB counts. */ | |
136 | ||
137 | gcov_type | |
138 | get_hot_bb_threshold () | |
139 | { | |
140 | gcov_working_set_t *ws; | |
141 | if (min_count == -1) | |
142 | { | |
143 | ws = find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE)); | |
144 | gcc_assert (ws); | |
145 | min_count = ws->min_counter; | |
146 | } | |
147 | return min_count; | |
148 | } | |
149 | ||
150 | /* Set the threshold for hot BB counts. */ | |
151 | ||
152 | void | |
153 | set_hot_bb_threshold (gcov_type min) | |
154 | { | |
155 | min_count = min; | |
156 | } | |
157 | ||
fb2fed03 JH |
158 | /* Return TRUE if frequency FREQ is considered to be hot. */ |
159 | ||
160 | static inline bool | |
2eb712b4 | 161 | maybe_hot_count_p (struct function *fun, gcov_type count) |
fb2fed03 | 162 | { |
b131b583 | 163 | if (fun && profile_status_for_function (fun) != PROFILE_READ) |
fb2fed03 JH |
164 | return true; |
165 | /* Code executed at most once is not hot. */ | |
166 | if (profile_info->runs >= count) | |
167 | return false; | |
0208f7da | 168 | return (count >= get_hot_bb_threshold ()); |
fb2fed03 JH |
169 | } |
170 | ||
194734e9 | 171 | /* Return true in case BB can be CPU intensive and should be optimized |
d55d8fc7 | 172 | for maximal performance. */ |
194734e9 JH |
173 | |
174 | bool | |
2eb712b4 | 175 | maybe_hot_bb_p (struct function *fun, const_basic_block bb) |
194734e9 | 176 | { |
2eb712b4 MJ |
177 | gcc_checking_assert (fun); |
178 | if (profile_status_for_function (fun) == PROFILE_READ) | |
179 | return maybe_hot_count_p (fun, bb->count); | |
180 | return maybe_hot_frequency_p (fun, bb->frequency); | |
3250d724 JH |
181 | } |
182 | ||
ca30a539 JH |
183 | /* Return true if the call can be hot. */ |
184 | ||
185 | bool | |
186 | cgraph_maybe_hot_edge_p (struct cgraph_edge *edge) | |
187 | { | |
188 | if (profile_info && flag_branch_probabilities | |
b131b583 TJ |
189 | && !maybe_hot_count_p (NULL, |
190 | edge->count)) | |
ca30a539 | 191 | return false; |
5fefcf92 | 192 | if (edge->caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED |
37678631 JH |
193 | || (edge->callee |
194 | && edge->callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)) | |
5fefcf92 | 195 | return false; |
22983f55 | 196 | if (edge->caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED |
37678631 JH |
197 | && (edge->callee |
198 | && edge->callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE)) | |
22983f55 | 199 | return false; |
5fefcf92 | 200 | if (optimize_size) |
ca30a539 | 201 | return false; |
5fefcf92 | 202 | if (edge->caller->frequency == NODE_FREQUENCY_HOT) |
ca30a539 | 203 | return true; |
5fefcf92 JH |
204 | if (edge->caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE |
205 | && edge->frequency < CGRAPH_FREQ_BASE * 3 / 2) | |
206 | return false; | |
39c1b6db MP |
207 | if (flag_guess_branch_prob) |
208 | { | |
209 | if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0 | |
210 | || edge->frequency <= (CGRAPH_FREQ_BASE | |
211 | / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))) | |
212 | return false; | |
213 | } | |
ca30a539 JH |
214 | return true; |
215 | } | |
216 | ||
3250d724 JH |
217 | /* Return true in case BB can be CPU intensive and should be optimized |
218 | for maximal performance. */ | |
219 | ||
220 | bool | |
221 | maybe_hot_edge_p (edge e) | |
222 | { | |
ba623ced | 223 | if (profile_status == PROFILE_READ) |
2eb712b4 MJ |
224 | return maybe_hot_count_p (cfun, e->count); |
225 | return maybe_hot_frequency_p (cfun, EDGE_FREQUENCY (e)); | |
194734e9 JH |
226 | } |
227 | ||
e6416b30 | 228 | |
194734e9 | 229 | /* Return true in case BB is probably never executed. */ |
e6416b30 | 230 | |
194734e9 | 231 | bool |
2eb712b4 | 232 | probably_never_executed_bb_p (struct function *fun, const_basic_block bb) |
194734e9 | 233 | { |
2eb712b4 | 234 | gcc_checking_assert (fun); |
61a74079 JH |
235 | if (profile_status_for_function (fun) == PROFILE_READ) |
236 | { | |
237 | if ((bb->count * 4 + profile_info->runs / 2) / profile_info->runs > 0) | |
238 | return false; | |
239 | if (!bb->frequency) | |
240 | return true; | |
241 | if (!ENTRY_BLOCK_PTR->frequency) | |
242 | return false; | |
243 | if (ENTRY_BLOCK_PTR->count && ENTRY_BLOCK_PTR->count < REG_BR_PROB_BASE) | |
244 | { | |
245 | return (RDIV (bb->frequency * ENTRY_BLOCK_PTR->count, | |
246 | ENTRY_BLOCK_PTR->frequency) | |
247 | < REG_BR_PROB_BASE / 4); | |
248 | } | |
249 | return true; | |
250 | } | |
52bf96d2 | 251 | if ((!profile_info || !flag_branch_probabilities) |
2eb712b4 | 252 | && (cgraph_get_node (fun->decl)->frequency |
581985d7 | 253 | == NODE_FREQUENCY_UNLIKELY_EXECUTED)) |
52bf96d2 | 254 | return true; |
194734e9 JH |
255 | return false; |
256 | } | |
257 | ||
600b5b1d TJ |
258 | |
259 | /* Return true in case edge E is probably never executed. */ | |
260 | ||
261 | bool | |
262 | probably_never_executed_edge_p (struct function *fun, edge e) | |
263 | { | |
264 | gcc_checking_assert (fun); | |
265 | if (profile_info && flag_branch_probabilities) | |
266 | return ((e->count + profile_info->runs / 2) / profile_info->runs) == 0; | |
267 | if ((!profile_info || !flag_branch_probabilities) | |
268 | && (cgraph_get_node (fun->decl)->frequency | |
269 | == NODE_FREQUENCY_UNLIKELY_EXECUTED)) | |
270 | return true; | |
271 | return false; | |
272 | } | |
273 | ||
e6416b30 | 274 | /* Return true if NODE should be optimized for size. */ |
bf08ebeb | 275 | |
3debdc1e | 276 | bool |
e6416b30 | 277 | cgraph_optimize_for_size_p (struct cgraph_node *node) |
bf08ebeb | 278 | { |
9f9ebcdf MJ |
279 | if (optimize_size) |
280 | return true; | |
9f9ebcdf MJ |
281 | if (node && (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)) |
282 | return true; | |
283 | else | |
284 | return false; | |
3debdc1e JH |
285 | } |
286 | ||
e6416b30 JH |
287 | /* Return true when current function should always be optimized for size. */ |
288 | ||
289 | bool | |
290 | optimize_function_for_size_p (struct function *fun) | |
291 | { | |
292 | if (optimize_size) | |
293 | return true; | |
294 | if (!fun || !fun->decl) | |
295 | return false; | |
296 | return cgraph_optimize_for_size_p (cgraph_get_node (fun->decl)); | |
297 | } | |
298 | ||
3debdc1e JH |
299 | /* Return true when current function should always be optimized for speed. */ |
300 | ||
301 | bool | |
302 | optimize_function_for_speed_p (struct function *fun) | |
303 | { | |
304 | return !optimize_function_for_size_p (fun); | |
bf08ebeb JH |
305 | } |
306 | ||
307 | /* Return TRUE when BB should be optimized for size. */ | |
308 | ||
309 | bool | |
cc870036 | 310 | optimize_bb_for_size_p (const_basic_block bb) |
bf08ebeb | 311 | { |
2eb712b4 | 312 | return optimize_function_for_size_p (cfun) || !maybe_hot_bb_p (cfun, bb); |
bf08ebeb JH |
313 | } |
314 | ||
315 | /* Return TRUE when BB should be optimized for speed. */ | |
316 | ||
317 | bool | |
cc870036 | 318 | optimize_bb_for_speed_p (const_basic_block bb) |
bf08ebeb JH |
319 | { |
320 | return !optimize_bb_for_size_p (bb); | |
321 | } | |
322 | ||
323 | /* Return TRUE when BB should be optimized for size. */ | |
324 | ||
325 | bool | |
326 | optimize_edge_for_size_p (edge e) | |
327 | { | |
3debdc1e | 328 | return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e); |
bf08ebeb JH |
329 | } |
330 | ||
331 | /* Return TRUE when BB should be optimized for speed. */ | |
332 | ||
333 | bool | |
334 | optimize_edge_for_speed_p (edge e) | |
335 | { | |
336 | return !optimize_edge_for_size_p (e); | |
337 | } | |
338 | ||
339 | /* Return TRUE when BB should be optimized for size. */ | |
340 | ||
341 | bool | |
342 | optimize_insn_for_size_p (void) | |
343 | { | |
3debdc1e | 344 | return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p; |
bf08ebeb JH |
345 | } |
346 | ||
347 | /* Return TRUE when BB should be optimized for speed. */ | |
348 | ||
349 | bool | |
350 | optimize_insn_for_speed_p (void) | |
351 | { | |
352 | return !optimize_insn_for_size_p (); | |
353 | } | |
354 | ||
cc870036 JH |
355 | /* Return TRUE when LOOP should be optimized for size. */ |
356 | ||
357 | bool | |
358 | optimize_loop_for_size_p (struct loop *loop) | |
359 | { | |
360 | return optimize_bb_for_size_p (loop->header); | |
361 | } | |
362 | ||
363 | /* Return TRUE when LOOP should be optimized for speed. */ | |
364 | ||
365 | bool | |
366 | optimize_loop_for_speed_p (struct loop *loop) | |
367 | { | |
368 | return optimize_bb_for_speed_p (loop->header); | |
369 | } | |
370 | ||
efd8f750 JH |
371 | /* Return TRUE when LOOP nest should be optimized for speed. */ |
372 | ||
373 | bool | |
374 | optimize_loop_nest_for_speed_p (struct loop *loop) | |
375 | { | |
376 | struct loop *l = loop; | |
377 | if (optimize_loop_for_speed_p (loop)) | |
378 | return true; | |
379 | l = loop->inner; | |
c16eb95f | 380 | while (l && l != loop) |
efd8f750 JH |
381 | { |
382 | if (optimize_loop_for_speed_p (l)) | |
383 | return true; | |
384 | if (l->inner) | |
385 | l = l->inner; | |
386 | else if (l->next) | |
387 | l = l->next; | |
388 | else | |
8bcf15f6 JH |
389 | { |
390 | while (l != loop && !l->next) | |
391 | l = loop_outer (l); | |
392 | if (l != loop) | |
393 | l = l->next; | |
394 | } | |
efd8f750 JH |
395 | } |
396 | return false; | |
397 | } | |
398 | ||
399 | /* Return TRUE when LOOP nest should be optimized for size. */ | |
400 | ||
401 | bool | |
402 | optimize_loop_nest_for_size_p (struct loop *loop) | |
403 | { | |
404 | return !optimize_loop_nest_for_speed_p (loop); | |
405 | } | |
406 | ||
3a4fd356 JH |
407 | /* Return true when edge E is likely to be well predictable by branch |
408 | predictor. */ | |
409 | ||
410 | bool | |
411 | predictable_edge_p (edge e) | |
412 | { | |
413 | if (profile_status == PROFILE_ABSENT) | |
414 | return false; | |
415 | if ((e->probability | |
416 | <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100) | |
417 | || (REG_BR_PROB_BASE - e->probability | |
418 | <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100)) | |
419 | return true; | |
420 | return false; | |
421 | } | |
422 | ||
423 | ||
bf08ebeb JH |
424 | /* Set RTL expansion for BB profile. */ |
425 | ||
426 | void | |
427 | rtl_profile_for_bb (basic_block bb) | |
428 | { | |
2eb712b4 | 429 | crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb); |
bf08ebeb JH |
430 | } |
431 | ||
432 | /* Set RTL expansion for edge profile. */ | |
433 | ||
434 | void | |
435 | rtl_profile_for_edge (edge e) | |
436 | { | |
437 | crtl->maybe_hot_insn_p = maybe_hot_edge_p (e); | |
438 | } | |
439 | ||
440 | /* Set RTL expansion to default mode (i.e. when profile info is not known). */ | |
441 | void | |
442 | default_rtl_profile (void) | |
443 | { | |
444 | crtl->maybe_hot_insn_p = true; | |
445 | } | |
446 | ||
969d70ca JH |
447 | /* Return true if the one of outgoing edges is already predicted by |
448 | PREDICTOR. */ | |
449 | ||
6de9cd9a | 450 | bool |
9678086d | 451 | rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor) |
969d70ca JH |
452 | { |
453 | rtx note; | |
a813c111 | 454 | if (!INSN_P (BB_END (bb))) |
969d70ca | 455 | return false; |
a813c111 | 456 | for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1)) |
969d70ca JH |
457 | if (REG_NOTE_KIND (note) == REG_BR_PRED |
458 | && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor) | |
459 | return true; | |
460 | return false; | |
461 | } | |
ee92cb46 | 462 | |
f06b0a10 ZD |
463 | /* This map contains for a basic block the list of predictions for the |
464 | outgoing edges. */ | |
465 | ||
466 | static struct pointer_map_t *bb_predictions; | |
467 | ||
fba4cb03 LB |
468 | /* Structure representing predictions in tree level. */ |
469 | ||
470 | struct edge_prediction { | |
471 | struct edge_prediction *ep_next; | |
472 | edge ep_edge; | |
473 | enum br_predictor ep_predictor; | |
474 | int ep_probability; | |
475 | }; | |
476 | ||
6de9cd9a DN |
477 | /* Return true if the one of outgoing edges is already predicted by |
478 | PREDICTOR. */ | |
479 | ||
480 | bool | |
726a989a | 481 | gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor) |
6de9cd9a | 482 | { |
4aab792d | 483 | struct edge_prediction *i; |
f06b0a10 ZD |
484 | void **preds = pointer_map_contains (bb_predictions, bb); |
485 | ||
486 | if (!preds) | |
487 | return false; | |
b8698a0f | 488 | |
d3bfe4de | 489 | for (i = (struct edge_prediction *) *preds; i; i = i->ep_next) |
59ced947 | 490 | if (i->ep_predictor == predictor) |
6de9cd9a DN |
491 | return true; |
492 | return false; | |
493 | } | |
494 | ||
2c9e13f3 | 495 | /* Return true when the probability of edge is reliable. |
b8698a0f | 496 | |
2c9e13f3 JH |
497 | The profile guessing code is good at predicting branch outcome (ie. |
498 | taken/not taken), that is predicted right slightly over 75% of time. | |
86c33cd0 | 499 | It is however notoriously poor on predicting the probability itself. |
2c9e13f3 JH |
500 | In general the profile appear a lot flatter (with probabilities closer |
501 | to 50%) than the reality so it is bad idea to use it to drive optimization | |
502 | such as those disabling dynamic branch prediction for well predictable | |
503 | branches. | |
504 | ||
505 | There are two exceptions - edges leading to noreturn edges and edges | |
506 | predicted by number of iterations heuristics are predicted well. This macro | |
507 | should be able to distinguish those, but at the moment it simply check for | |
508 | noreturn heuristic that is only one giving probability over 99% or bellow | |
86c33cd0 | 509 | 1%. In future we might want to propagate reliability information across the |
2c9e13f3 JH |
510 | CFG if we find this information useful on multiple places. */ |
511 | static bool | |
512 | probability_reliable_p (int prob) | |
513 | { | |
514 | return (profile_status == PROFILE_READ | |
515 | || (profile_status == PROFILE_GUESSED | |
516 | && (prob <= HITRATE (1) || prob >= HITRATE (99)))); | |
517 | } | |
518 | ||
519 | /* Same predicate as above, working on edges. */ | |
520 | bool | |
ed7a4b4b | 521 | edge_probability_reliable_p (const_edge e) |
2c9e13f3 JH |
522 | { |
523 | return probability_reliable_p (e->probability); | |
524 | } | |
525 | ||
526 | /* Same predicate as edge_probability_reliable_p, working on notes. */ | |
527 | bool | |
ed7a4b4b | 528 | br_prob_note_reliable_p (const_rtx note) |
2c9e13f3 JH |
529 | { |
530 | gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB); | |
531 | return probability_reliable_p (INTVAL (XEXP (note, 0))); | |
532 | } | |
533 | ||
7d6d381a | 534 | static void |
79a490a9 | 535 | predict_insn (rtx insn, enum br_predictor predictor, int probability) |
4db384c9 | 536 | { |
e16acfcd | 537 | gcc_assert (any_condjump_p (insn)); |
d50672ef JH |
538 | if (!flag_guess_branch_prob) |
539 | return; | |
bfdade77 | 540 | |
65c5f2a6 ILT |
541 | add_reg_note (insn, REG_BR_PRED, |
542 | gen_rtx_CONCAT (VOIDmode, | |
543 | GEN_INT ((int) predictor), | |
544 | GEN_INT ((int) probability))); | |
4db384c9 JH |
545 | } |
546 | ||
547 | /* Predict insn by given predictor. */ | |
bfdade77 | 548 | |
4db384c9 | 549 | void |
79a490a9 AJ |
550 | predict_insn_def (rtx insn, enum br_predictor predictor, |
551 | enum prediction taken) | |
4db384c9 JH |
552 | { |
553 | int probability = predictor_info[(int) predictor].hitrate; | |
bfdade77 | 554 | |
4db384c9 JH |
555 | if (taken != TAKEN) |
556 | probability = REG_BR_PROB_BASE - probability; | |
bfdade77 | 557 | |
4db384c9 | 558 | predict_insn (insn, predictor, probability); |
ee92cb46 JH |
559 | } |
560 | ||
561 | /* Predict edge E with given probability if possible. */ | |
bfdade77 | 562 | |
4db384c9 | 563 | void |
6de9cd9a | 564 | rtl_predict_edge (edge e, enum br_predictor predictor, int probability) |
ee92cb46 JH |
565 | { |
566 | rtx last_insn; | |
a813c111 | 567 | last_insn = BB_END (e->src); |
ee92cb46 JH |
568 | |
569 | /* We can store the branch prediction information only about | |
570 | conditional jumps. */ | |
571 | if (!any_condjump_p (last_insn)) | |
572 | return; | |
573 | ||
574 | /* We always store probability of branching. */ | |
575 | if (e->flags & EDGE_FALLTHRU) | |
576 | probability = REG_BR_PROB_BASE - probability; | |
577 | ||
4db384c9 JH |
578 | predict_insn (last_insn, predictor, probability); |
579 | } | |
580 | ||
6de9cd9a DN |
581 | /* Predict edge E with the given PROBABILITY. */ |
582 | void | |
726a989a | 583 | gimple_predict_edge (edge e, enum br_predictor predictor, int probability) |
6de9cd9a | 584 | { |
a00d11f0 | 585 | gcc_assert (profile_status != PROFILE_GUESSED); |
e0342c26 | 586 | if ((e->src != ENTRY_BLOCK_PTR && EDGE_COUNT (e->src->succs) > 1) |
a00d11f0 | 587 | && flag_guess_branch_prob && optimize) |
e0342c26 | 588 | { |
f06b0a10 ZD |
589 | struct edge_prediction *i = XNEW (struct edge_prediction); |
590 | void **preds = pointer_map_insert (bb_predictions, e->src); | |
6de9cd9a | 591 | |
d3bfe4de | 592 | i->ep_next = (struct edge_prediction *) *preds; |
f06b0a10 | 593 | *preds = i; |
59ced947 RÁE |
594 | i->ep_probability = probability; |
595 | i->ep_predictor = predictor; | |
596 | i->ep_edge = e; | |
e0342c26 | 597 | } |
6de9cd9a DN |
598 | } |
599 | ||
3809e990 JH |
600 | /* Remove all predictions on given basic block that are attached |
601 | to edge E. */ | |
602 | void | |
603 | remove_predictions_associated_with_edge (edge e) | |
604 | { | |
f06b0a10 | 605 | void **preds; |
b8698a0f | 606 | |
f06b0a10 ZD |
607 | if (!bb_predictions) |
608 | return; | |
609 | ||
610 | preds = pointer_map_contains (bb_predictions, e->src); | |
611 | ||
612 | if (preds) | |
3809e990 | 613 | { |
f06b0a10 ZD |
614 | struct edge_prediction **prediction = (struct edge_prediction **) preds; |
615 | struct edge_prediction *next; | |
616 | ||
3809e990 JH |
617 | while (*prediction) |
618 | { | |
59ced947 | 619 | if ((*prediction)->ep_edge == e) |
f06b0a10 ZD |
620 | { |
621 | next = (*prediction)->ep_next; | |
622 | free (*prediction); | |
623 | *prediction = next; | |
624 | } | |
3809e990 | 625 | else |
59ced947 | 626 | prediction = &((*prediction)->ep_next); |
3809e990 JH |
627 | } |
628 | } | |
629 | } | |
630 | ||
f06b0a10 ZD |
631 | /* Clears the list of predictions stored for BB. */ |
632 | ||
633 | static void | |
634 | clear_bb_predictions (basic_block bb) | |
635 | { | |
636 | void **preds = pointer_map_contains (bb_predictions, bb); | |
637 | struct edge_prediction *pred, *next; | |
638 | ||
639 | if (!preds) | |
640 | return; | |
641 | ||
d3bfe4de | 642 | for (pred = (struct edge_prediction *) *preds; pred; pred = next) |
f06b0a10 ZD |
643 | { |
644 | next = pred->ep_next; | |
645 | free (pred); | |
646 | } | |
647 | *preds = NULL; | |
648 | } | |
649 | ||
2ffa9932 JH |
650 | /* Return true when we can store prediction on insn INSN. |
651 | At the moment we represent predictions only on conditional | |
652 | jumps, not at computed jump or other complicated cases. */ | |
653 | static bool | |
ed7a4b4b | 654 | can_predict_insn_p (const_rtx insn) |
2ffa9932 | 655 | { |
4b4bf941 | 656 | return (JUMP_P (insn) |
2ffa9932 | 657 | && any_condjump_p (insn) |
628f6a4e | 658 | && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2); |
2ffa9932 JH |
659 | } |
660 | ||
4db384c9 | 661 | /* Predict edge E by given predictor if possible. */ |
bfdade77 | 662 | |
4db384c9 | 663 | void |
79a490a9 AJ |
664 | predict_edge_def (edge e, enum br_predictor predictor, |
665 | enum prediction taken) | |
4db384c9 JH |
666 | { |
667 | int probability = predictor_info[(int) predictor].hitrate; | |
668 | ||
669 | if (taken != TAKEN) | |
670 | probability = REG_BR_PROB_BASE - probability; | |
bfdade77 | 671 | |
4db384c9 JH |
672 | predict_edge (e, predictor, probability); |
673 | } | |
674 | ||
675 | /* Invert all branch predictions or probability notes in the INSN. This needs | |
676 | to be done each time we invert the condition used by the jump. */ | |
bfdade77 | 677 | |
4db384c9 | 678 | void |
79a490a9 | 679 | invert_br_probabilities (rtx insn) |
4db384c9 | 680 | { |
bfdade77 RK |
681 | rtx note; |
682 | ||
683 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | |
684 | if (REG_NOTE_KIND (note) == REG_BR_PROB) | |
685 | XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0))); | |
686 | else if (REG_NOTE_KIND (note) == REG_BR_PRED) | |
687 | XEXP (XEXP (note, 0), 1) | |
688 | = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1))); | |
4db384c9 JH |
689 | } |
690 | ||
691 | /* Dump information about the branch prediction to the output file. */ | |
bfdade77 | 692 | |
4db384c9 | 693 | static void |
6de9cd9a | 694 | dump_prediction (FILE *file, enum br_predictor predictor, int probability, |
79a490a9 | 695 | basic_block bb, int used) |
4db384c9 | 696 | { |
628f6a4e BE |
697 | edge e; |
698 | edge_iterator ei; | |
4db384c9 | 699 | |
6de9cd9a | 700 | if (!file) |
4db384c9 JH |
701 | return; |
702 | ||
628f6a4e BE |
703 | FOR_EACH_EDGE (e, ei, bb->succs) |
704 | if (! (e->flags & EDGE_FALLTHRU)) | |
705 | break; | |
4db384c9 | 706 | |
6de9cd9a | 707 | fprintf (file, " %s heuristics%s: %.1f%%", |
4db384c9 | 708 | predictor_info[predictor].name, |
bfdade77 | 709 | used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE); |
4db384c9 JH |
710 | |
711 | if (bb->count) | |
25c3a4ef | 712 | { |
6de9cd9a DN |
713 | fprintf (file, " exec "); |
714 | fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count); | |
fbc2782e DD |
715 | if (e) |
716 | { | |
6de9cd9a DN |
717 | fprintf (file, " hit "); |
718 | fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count); | |
719 | fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count); | |
fbc2782e | 720 | } |
25c3a4ef | 721 | } |
bfdade77 | 722 | |
6de9cd9a | 723 | fprintf (file, "\n"); |
4db384c9 JH |
724 | } |
725 | ||
229031d0 | 726 | /* We can not predict the probabilities of outgoing edges of bb. Set them |
87022a6b JH |
727 | evenly and hope for the best. */ |
728 | static void | |
729 | set_even_probabilities (basic_block bb) | |
730 | { | |
731 | int nedges = 0; | |
732 | edge e; | |
628f6a4e | 733 | edge_iterator ei; |
87022a6b | 734 | |
628f6a4e | 735 | FOR_EACH_EDGE (e, ei, bb->succs) |
87022a6b JH |
736 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) |
737 | nedges ++; | |
628f6a4e | 738 | FOR_EACH_EDGE (e, ei, bb->succs) |
87022a6b JH |
739 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) |
740 | e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges; | |
741 | else | |
742 | e->probability = 0; | |
743 | } | |
744 | ||
4db384c9 JH |
745 | /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB |
746 | note if not already present. Remove now useless REG_BR_PRED notes. */ | |
bfdade77 | 747 | |
4db384c9 | 748 | static void |
79a490a9 | 749 | combine_predictions_for_insn (rtx insn, basic_block bb) |
4db384c9 | 750 | { |
87022a6b JH |
751 | rtx prob_note; |
752 | rtx *pnote; | |
bfdade77 | 753 | rtx note; |
4db384c9 | 754 | int best_probability = PROB_EVEN; |
bbbbb16a | 755 | enum br_predictor best_predictor = END_PREDICTORS; |
134d3a2e JH |
756 | int combined_probability = REG_BR_PROB_BASE / 2; |
757 | int d; | |
d195b46f JH |
758 | bool first_match = false; |
759 | bool found = false; | |
4db384c9 | 760 | |
87022a6b JH |
761 | if (!can_predict_insn_p (insn)) |
762 | { | |
763 | set_even_probabilities (bb); | |
764 | return; | |
765 | } | |
766 | ||
767 | prob_note = find_reg_note (insn, REG_BR_PROB, 0); | |
768 | pnote = ®_NOTES (insn); | |
c263766c RH |
769 | if (dump_file) |
770 | fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn), | |
0b17ab2f | 771 | bb->index); |
4db384c9 JH |
772 | |
773 | /* We implement "first match" heuristics and use probability guessed | |
6de9cd9a | 774 | by predictor with smallest index. */ |
bfdade77 RK |
775 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) |
776 | if (REG_NOTE_KIND (note) == REG_BR_PRED) | |
777 | { | |
81f40b79 ILT |
778 | enum br_predictor predictor = ((enum br_predictor) |
779 | INTVAL (XEXP (XEXP (note, 0), 0))); | |
bfdade77 RK |
780 | int probability = INTVAL (XEXP (XEXP (note, 0), 1)); |
781 | ||
782 | found = true; | |
783 | if (best_predictor > predictor) | |
784 | best_probability = probability, best_predictor = predictor; | |
785 | ||
786 | d = (combined_probability * probability | |
787 | + (REG_BR_PROB_BASE - combined_probability) | |
788 | * (REG_BR_PROB_BASE - probability)); | |
789 | ||
790 | /* Use FP math to avoid overflows of 32bit integers. */ | |
571a03b8 JJ |
791 | if (d == 0) |
792 | /* If one probability is 0% and one 100%, avoid division by zero. */ | |
793 | combined_probability = REG_BR_PROB_BASE / 2; | |
794 | else | |
795 | combined_probability = (((double) combined_probability) * probability | |
796 | * REG_BR_PROB_BASE / d + 0.5); | |
bfdade77 RK |
797 | } |
798 | ||
799 | /* Decide which heuristic to use. In case we didn't match anything, | |
800 | use no_prediction heuristic, in case we did match, use either | |
d195b46f JH |
801 | first match or Dempster-Shaffer theory depending on the flags. */ |
802 | ||
134d3a2e | 803 | if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH) |
d195b46f JH |
804 | first_match = true; |
805 | ||
806 | if (!found) | |
6de9cd9a DN |
807 | dump_prediction (dump_file, PRED_NO_PREDICTION, |
808 | combined_probability, bb, true); | |
d195b46f JH |
809 | else |
810 | { | |
6de9cd9a DN |
811 | dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, |
812 | bb, !first_match); | |
813 | dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, | |
814 | bb, first_match); | |
d195b46f JH |
815 | } |
816 | ||
817 | if (first_match) | |
134d3a2e | 818 | combined_probability = best_probability; |
6de9cd9a | 819 | dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true); |
d195b46f JH |
820 | |
821 | while (*pnote) | |
822 | { | |
823 | if (REG_NOTE_KIND (*pnote) == REG_BR_PRED) | |
824 | { | |
81f40b79 ILT |
825 | enum br_predictor predictor = ((enum br_predictor) |
826 | INTVAL (XEXP (XEXP (*pnote, 0), 0))); | |
d195b46f JH |
827 | int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1)); |
828 | ||
6de9cd9a | 829 | dump_prediction (dump_file, predictor, probability, bb, |
d195b46f | 830 | !first_match || best_predictor == predictor); |
6a4d6760 | 831 | *pnote = XEXP (*pnote, 1); |
d195b46f JH |
832 | } |
833 | else | |
6a4d6760 | 834 | pnote = &XEXP (*pnote, 1); |
d195b46f | 835 | } |
bfdade77 | 836 | |
4db384c9 JH |
837 | if (!prob_note) |
838 | { | |
65c5f2a6 | 839 | add_reg_note (insn, REG_BR_PROB, GEN_INT (combined_probability)); |
bfdade77 | 840 | |
134d3a2e JH |
841 | /* Save the prediction into CFG in case we are seeing non-degenerated |
842 | conditional jump. */ | |
c5cbcccf | 843 | if (!single_succ_p (bb)) |
134d3a2e JH |
844 | { |
845 | BRANCH_EDGE (bb)->probability = combined_probability; | |
bfdade77 RK |
846 | FALLTHRU_EDGE (bb)->probability |
847 | = REG_BR_PROB_BASE - combined_probability; | |
134d3a2e | 848 | } |
4db384c9 | 849 | } |
c5cbcccf | 850 | else if (!single_succ_p (bb)) |
e53de54d JH |
851 | { |
852 | int prob = INTVAL (XEXP (prob_note, 0)); | |
853 | ||
854 | BRANCH_EDGE (bb)->probability = prob; | |
855 | FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob; | |
856 | } | |
857 | else | |
c5cbcccf | 858 | single_succ_edge (bb)->probability = REG_BR_PROB_BASE; |
ee92cb46 JH |
859 | } |
860 | ||
6de9cd9a DN |
861 | /* Combine predictions into single probability and store them into CFG. |
862 | Remove now useless prediction entries. */ | |
f1ebdfc5 | 863 | |
6de9cd9a | 864 | static void |
10d22567 | 865 | combine_predictions_for_bb (basic_block bb) |
f1ebdfc5 | 866 | { |
6de9cd9a | 867 | int best_probability = PROB_EVEN; |
bbbbb16a | 868 | enum br_predictor best_predictor = END_PREDICTORS; |
6de9cd9a DN |
869 | int combined_probability = REG_BR_PROB_BASE / 2; |
870 | int d; | |
871 | bool first_match = false; | |
872 | bool found = false; | |
873 | struct edge_prediction *pred; | |
874 | int nedges = 0; | |
875 | edge e, first = NULL, second = NULL; | |
628f6a4e | 876 | edge_iterator ei; |
f06b0a10 | 877 | void **preds; |
f1ebdfc5 | 878 | |
628f6a4e | 879 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
880 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) |
881 | { | |
628f6a4e | 882 | nedges ++; |
6de9cd9a DN |
883 | if (first && !second) |
884 | second = e; | |
885 | if (!first) | |
886 | first = e; | |
887 | } | |
888 | ||
b8698a0f | 889 | /* When there is no successor or only one choice, prediction is easy. |
6de9cd9a DN |
890 | |
891 | We are lazy for now and predict only basic blocks with two outgoing | |
892 | edges. It is possible to predict generic case too, but we have to | |
893 | ignore first match heuristics and do more involved combining. Implement | |
894 | this later. */ | |
895 | if (nedges != 2) | |
896 | { | |
87022a6b JH |
897 | if (!bb->count) |
898 | set_even_probabilities (bb); | |
f06b0a10 | 899 | clear_bb_predictions (bb); |
10d22567 ZD |
900 | if (dump_file) |
901 | fprintf (dump_file, "%i edges in bb %i predicted to even probabilities\n", | |
6de9cd9a DN |
902 | nedges, bb->index); |
903 | return; | |
904 | } | |
905 | ||
10d22567 ZD |
906 | if (dump_file) |
907 | fprintf (dump_file, "Predictions for bb %i\n", bb->index); | |
6de9cd9a | 908 | |
f06b0a10 ZD |
909 | preds = pointer_map_contains (bb_predictions, bb); |
910 | if (preds) | |
6de9cd9a | 911 | { |
f06b0a10 ZD |
912 | /* We implement "first match" heuristics and use probability guessed |
913 | by predictor with smallest index. */ | |
d3bfe4de | 914 | for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) |
f06b0a10 | 915 | { |
bbbbb16a | 916 | enum br_predictor predictor = pred->ep_predictor; |
f06b0a10 | 917 | int probability = pred->ep_probability; |
6de9cd9a | 918 | |
f06b0a10 ZD |
919 | if (pred->ep_edge != first) |
920 | probability = REG_BR_PROB_BASE - probability; | |
6de9cd9a | 921 | |
f06b0a10 | 922 | found = true; |
c0ee0021 JH |
923 | /* First match heuristics would be widly confused if we predicted |
924 | both directions. */ | |
f06b0a10 | 925 | if (best_predictor > predictor) |
c0ee0021 JH |
926 | { |
927 | struct edge_prediction *pred2; | |
928 | int prob = probability; | |
929 | ||
930 | for (pred2 = (struct edge_prediction *) *preds; pred2; pred2 = pred2->ep_next) | |
931 | if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor) | |
932 | { | |
933 | int probability2 = pred->ep_probability; | |
934 | ||
935 | if (pred2->ep_edge != first) | |
936 | probability2 = REG_BR_PROB_BASE - probability2; | |
937 | ||
b8698a0f | 938 | if ((probability < REG_BR_PROB_BASE / 2) != |
c0ee0021 JH |
939 | (probability2 < REG_BR_PROB_BASE / 2)) |
940 | break; | |
941 | ||
942 | /* If the same predictor later gave better result, go for it! */ | |
943 | if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability)) | |
944 | || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability))) | |
945 | prob = probability2; | |
946 | } | |
947 | if (!pred2) | |
948 | best_probability = prob, best_predictor = predictor; | |
949 | } | |
6de9cd9a | 950 | |
f06b0a10 ZD |
951 | d = (combined_probability * probability |
952 | + (REG_BR_PROB_BASE - combined_probability) | |
953 | * (REG_BR_PROB_BASE - probability)); | |
6de9cd9a | 954 | |
f06b0a10 ZD |
955 | /* Use FP math to avoid overflows of 32bit integers. */ |
956 | if (d == 0) | |
957 | /* If one probability is 0% and one 100%, avoid division by zero. */ | |
958 | combined_probability = REG_BR_PROB_BASE / 2; | |
959 | else | |
960 | combined_probability = (((double) combined_probability) | |
961 | * probability | |
962 | * REG_BR_PROB_BASE / d + 0.5); | |
963 | } | |
6de9cd9a DN |
964 | } |
965 | ||
966 | /* Decide which heuristic to use. In case we didn't match anything, | |
967 | use no_prediction heuristic, in case we did match, use either | |
968 | first match or Dempster-Shaffer theory depending on the flags. */ | |
969 | ||
970 | if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH) | |
971 | first_match = true; | |
972 | ||
973 | if (!found) | |
10d22567 | 974 | dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb, true); |
6de9cd9a DN |
975 | else |
976 | { | |
10d22567 | 977 | dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb, |
6de9cd9a | 978 | !first_match); |
10d22567 | 979 | dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb, |
6de9cd9a DN |
980 | first_match); |
981 | } | |
982 | ||
983 | if (first_match) | |
984 | combined_probability = best_probability; | |
10d22567 | 985 | dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true); |
6de9cd9a | 986 | |
f06b0a10 | 987 | if (preds) |
6de9cd9a | 988 | { |
d3bfe4de | 989 | for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) |
f06b0a10 | 990 | { |
bbbbb16a | 991 | enum br_predictor predictor = pred->ep_predictor; |
f06b0a10 | 992 | int probability = pred->ep_probability; |
6de9cd9a | 993 | |
f06b0a10 ZD |
994 | if (pred->ep_edge != EDGE_SUCC (bb, 0)) |
995 | probability = REG_BR_PROB_BASE - probability; | |
996 | dump_prediction (dump_file, predictor, probability, bb, | |
997 | !first_match || best_predictor == predictor); | |
998 | } | |
6de9cd9a | 999 | } |
f06b0a10 | 1000 | clear_bb_predictions (bb); |
6de9cd9a | 1001 | |
87022a6b JH |
1002 | if (!bb->count) |
1003 | { | |
1004 | first->probability = combined_probability; | |
1005 | second->probability = REG_BR_PROB_BASE - combined_probability; | |
1006 | } | |
6de9cd9a DN |
1007 | } |
1008 | ||
9c04723a DC |
1009 | /* Check if T1 and T2 satisfy the IV_COMPARE condition. |
1010 | Return the SSA_NAME if the condition satisfies, NULL otherwise. | |
1011 | ||
1012 | T1 and T2 should be one of the following cases: | |
1013 | 1. T1 is SSA_NAME, T2 is NULL | |
1014 | 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4] | |
1015 | 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */ | |
1016 | ||
1017 | static tree | |
1018 | strips_small_constant (tree t1, tree t2) | |
1019 | { | |
1020 | tree ret = NULL; | |
1021 | int value = 0; | |
1022 | ||
1023 | if (!t1) | |
1024 | return NULL; | |
1025 | else if (TREE_CODE (t1) == SSA_NAME) | |
1026 | ret = t1; | |
1027 | else if (host_integerp (t1, 0)) | |
1028 | value = tree_low_cst (t1, 0); | |
1029 | else | |
1030 | return NULL; | |
1031 | ||
1032 | if (!t2) | |
1033 | return ret; | |
1034 | else if (host_integerp (t2, 0)) | |
1035 | value = tree_low_cst (t2, 0); | |
1036 | else if (TREE_CODE (t2) == SSA_NAME) | |
1037 | { | |
1038 | if (ret) | |
1039 | return NULL; | |
1040 | else | |
1041 | ret = t2; | |
1042 | } | |
1043 | ||
1044 | if (value <= 4 && value >= -4) | |
1045 | return ret; | |
1046 | else | |
1047 | return NULL; | |
1048 | } | |
1049 | ||
1050 | /* Return the SSA_NAME in T or T's operands. | |
1051 | Return NULL if SSA_NAME cannot be found. */ | |
1052 | ||
1053 | static tree | |
1054 | get_base_value (tree t) | |
1055 | { | |
1056 | if (TREE_CODE (t) == SSA_NAME) | |
1057 | return t; | |
1058 | ||
1059 | if (!BINARY_CLASS_P (t)) | |
1060 | return NULL; | |
1061 | ||
1062 | switch (TREE_OPERAND_LENGTH (t)) | |
1063 | { | |
1064 | case 1: | |
1065 | return strips_small_constant (TREE_OPERAND (t, 0), NULL); | |
1066 | case 2: | |
1067 | return strips_small_constant (TREE_OPERAND (t, 0), | |
1068 | TREE_OPERAND (t, 1)); | |
1069 | default: | |
1070 | return NULL; | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | /* Check the compare STMT in LOOP. If it compares an induction | |
1075 | variable to a loop invariant, return true, and save | |
1076 | LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP. | |
1077 | Otherwise return false and set LOOP_INVAIANT to NULL. */ | |
1078 | ||
1079 | static bool | |
1080 | is_comparison_with_loop_invariant_p (gimple stmt, struct loop *loop, | |
1081 | tree *loop_invariant, | |
1082 | enum tree_code *compare_code, | |
ecd4f20a | 1083 | tree *loop_step, |
9c04723a DC |
1084 | tree *loop_iv_base) |
1085 | { | |
1086 | tree op0, op1, bound, base; | |
1087 | affine_iv iv0, iv1; | |
1088 | enum tree_code code; | |
ecd4f20a | 1089 | tree step; |
9c04723a DC |
1090 | |
1091 | code = gimple_cond_code (stmt); | |
1092 | *loop_invariant = NULL; | |
1093 | ||
1094 | switch (code) | |
1095 | { | |
1096 | case GT_EXPR: | |
1097 | case GE_EXPR: | |
1098 | case NE_EXPR: | |
1099 | case LT_EXPR: | |
1100 | case LE_EXPR: | |
1101 | case EQ_EXPR: | |
1102 | break; | |
1103 | ||
1104 | default: | |
1105 | return false; | |
1106 | } | |
1107 | ||
1108 | op0 = gimple_cond_lhs (stmt); | |
1109 | op1 = gimple_cond_rhs (stmt); | |
1110 | ||
1111 | if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST) | |
1112 | || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST)) | |
1113 | return false; | |
1114 | if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true)) | |
1115 | return false; | |
1116 | if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true)) | |
1117 | return false; | |
1118 | if (TREE_CODE (iv0.step) != INTEGER_CST | |
1119 | || TREE_CODE (iv1.step) != INTEGER_CST) | |
1120 | return false; | |
1121 | if ((integer_zerop (iv0.step) && integer_zerop (iv1.step)) | |
1122 | || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step))) | |
1123 | return false; | |
1124 | ||
1125 | if (integer_zerop (iv0.step)) | |
1126 | { | |
1127 | if (code != NE_EXPR && code != EQ_EXPR) | |
1128 | code = invert_tree_comparison (code, false); | |
1129 | bound = iv0.base; | |
1130 | base = iv1.base; | |
1131 | if (host_integerp (iv1.step, 0)) | |
ecd4f20a | 1132 | step = iv1.step; |
9c04723a DC |
1133 | else |
1134 | return false; | |
1135 | } | |
1136 | else | |
1137 | { | |
1138 | bound = iv1.base; | |
1139 | base = iv0.base; | |
1140 | if (host_integerp (iv0.step, 0)) | |
ecd4f20a | 1141 | step = iv0.step; |
9c04723a DC |
1142 | else |
1143 | return false; | |
1144 | } | |
1145 | ||
1146 | if (TREE_CODE (bound) != INTEGER_CST) | |
1147 | bound = get_base_value (bound); | |
1148 | if (!bound) | |
1149 | return false; | |
1150 | if (TREE_CODE (base) != INTEGER_CST) | |
1151 | base = get_base_value (base); | |
1152 | if (!base) | |
1153 | return false; | |
1154 | ||
1155 | *loop_invariant = bound; | |
1156 | *compare_code = code; | |
1157 | *loop_step = step; | |
1158 | *loop_iv_base = base; | |
1159 | return true; | |
1160 | } | |
1161 | ||
1162 | /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */ | |
1163 | ||
1164 | static bool | |
1165 | expr_coherent_p (tree t1, tree t2) | |
1166 | { | |
1167 | gimple stmt; | |
1168 | tree ssa_name_1 = NULL; | |
1169 | tree ssa_name_2 = NULL; | |
1170 | ||
1171 | gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST); | |
1172 | gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST); | |
1173 | ||
1174 | if (t1 == t2) | |
1175 | return true; | |
1176 | ||
1177 | if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST) | |
1178 | return true; | |
1179 | if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST) | |
1180 | return false; | |
1181 | ||
1182 | /* Check to see if t1 is expressed/defined with t2. */ | |
1183 | stmt = SSA_NAME_DEF_STMT (t1); | |
1184 | gcc_assert (stmt != NULL); | |
1185 | if (is_gimple_assign (stmt)) | |
1186 | { | |
1187 | ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE); | |
1188 | if (ssa_name_1 && ssa_name_1 == t2) | |
1189 | return true; | |
1190 | } | |
1191 | ||
1192 | /* Check to see if t2 is expressed/defined with t1. */ | |
1193 | stmt = SSA_NAME_DEF_STMT (t2); | |
1194 | gcc_assert (stmt != NULL); | |
1195 | if (is_gimple_assign (stmt)) | |
1196 | { | |
1197 | ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE); | |
1198 | if (ssa_name_2 && ssa_name_2 == t1) | |
1199 | return true; | |
1200 | } | |
1201 | ||
1202 | /* Compare if t1 and t2's def_stmts are identical. */ | |
1203 | if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2) | |
1204 | return true; | |
1205 | else | |
1206 | return false; | |
1207 | } | |
1208 | ||
1209 | /* Predict branch probability of BB when BB contains a branch that compares | |
1210 | an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The | |
1211 | loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP. | |
1212 | ||
1213 | E.g. | |
1214 | for (int i = 0; i < bound; i++) { | |
1215 | if (i < bound - 2) | |
1216 | computation_1(); | |
1217 | else | |
1218 | computation_2(); | |
1219 | } | |
1220 | ||
1221 | In this loop, we will predict the branch inside the loop to be taken. */ | |
1222 | ||
1223 | static void | |
1224 | predict_iv_comparison (struct loop *loop, basic_block bb, | |
1225 | tree loop_bound_var, | |
1226 | tree loop_iv_base_var, | |
1227 | enum tree_code loop_bound_code, | |
1228 | int loop_bound_step) | |
1229 | { | |
1230 | gimple stmt; | |
1231 | tree compare_var, compare_base; | |
1232 | enum tree_code compare_code; | |
ecd4f20a | 1233 | tree compare_step_var; |
9c04723a DC |
1234 | edge then_edge; |
1235 | edge_iterator ei; | |
1236 | ||
1237 | if (predicted_by_p (bb, PRED_LOOP_ITERATIONS_GUESSED) | |
1238 | || predicted_by_p (bb, PRED_LOOP_ITERATIONS) | |
1239 | || predicted_by_p (bb, PRED_LOOP_EXIT)) | |
1240 | return; | |
1241 | ||
1242 | stmt = last_stmt (bb); | |
1243 | if (!stmt || gimple_code (stmt) != GIMPLE_COND) | |
1244 | return; | |
1245 | if (!is_comparison_with_loop_invariant_p (stmt, loop, &compare_var, | |
1246 | &compare_code, | |
ecd4f20a | 1247 | &compare_step_var, |
9c04723a DC |
1248 | &compare_base)) |
1249 | return; | |
1250 | ||
1251 | /* Find the taken edge. */ | |
1252 | FOR_EACH_EDGE (then_edge, ei, bb->succs) | |
1253 | if (then_edge->flags & EDGE_TRUE_VALUE) | |
1254 | break; | |
1255 | ||
1256 | /* When comparing an IV to a loop invariant, NE is more likely to be | |
1257 | taken while EQ is more likely to be not-taken. */ | |
1258 | if (compare_code == NE_EXPR) | |
1259 | { | |
1260 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1261 | return; | |
1262 | } | |
1263 | else if (compare_code == EQ_EXPR) | |
1264 | { | |
1265 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN); | |
1266 | return; | |
1267 | } | |
1268 | ||
1269 | if (!expr_coherent_p (loop_iv_base_var, compare_base)) | |
1270 | return; | |
1271 | ||
1272 | /* If loop bound, base and compare bound are all constants, we can | |
1273 | calculate the probability directly. */ | |
1274 | if (host_integerp (loop_bound_var, 0) | |
1275 | && host_integerp (compare_var, 0) | |
1276 | && host_integerp (compare_base, 0)) | |
1277 | { | |
1278 | int probability; | |
ecd4f20a MP |
1279 | bool of, overflow = false; |
1280 | double_int mod, compare_count, tem, loop_count; | |
1281 | ||
1282 | double_int loop_bound = tree_to_double_int (loop_bound_var); | |
1283 | double_int compare_bound = tree_to_double_int (compare_var); | |
1284 | double_int base = tree_to_double_int (compare_base); | |
1285 | double_int compare_step = tree_to_double_int (compare_step_var); | |
1286 | ||
1287 | /* (loop_bound - base) / compare_step */ | |
1288 | tem = loop_bound.sub_with_overflow (base, &of); | |
1289 | overflow |= of; | |
1290 | loop_count = tem.divmod_with_overflow (compare_step, | |
1291 | 0, TRUNC_DIV_EXPR, | |
1292 | &mod, &of); | |
1293 | overflow |= of; | |
1294 | ||
1295 | if ((!compare_step.is_negative ()) | |
9c04723a | 1296 | ^ (compare_code == LT_EXPR || compare_code == LE_EXPR)) |
ecd4f20a MP |
1297 | { |
1298 | /* (loop_bound - compare_bound) / compare_step */ | |
1299 | tem = loop_bound.sub_with_overflow (compare_bound, &of); | |
1300 | overflow |= of; | |
1301 | compare_count = tem.divmod_with_overflow (compare_step, | |
1302 | 0, TRUNC_DIV_EXPR, | |
1303 | &mod, &of); | |
1304 | overflow |= of; | |
1305 | } | |
9c04723a | 1306 | else |
ecd4f20a MP |
1307 | { |
1308 | /* (compare_bound - base) / compare_step */ | |
1309 | tem = compare_bound.sub_with_overflow (base, &of); | |
1310 | overflow |= of; | |
1311 | compare_count = tem.divmod_with_overflow (compare_step, | |
1312 | 0, TRUNC_DIV_EXPR, | |
1313 | &mod, &of); | |
1314 | overflow |= of; | |
1315 | } | |
9c04723a | 1316 | if (compare_code == LE_EXPR || compare_code == GE_EXPR) |
ecd4f20a | 1317 | ++compare_count; |
9c04723a | 1318 | if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR) |
ecd4f20a MP |
1319 | ++loop_count; |
1320 | if (compare_count.is_negative ()) | |
1321 | compare_count = double_int_zero; | |
1322 | if (loop_count.is_negative ()) | |
1323 | loop_count = double_int_zero; | |
1324 | if (loop_count.is_zero ()) | |
9c04723a | 1325 | probability = 0; |
ecd4f20a | 1326 | else if (compare_count.scmp (loop_count) == 1) |
9c04723a DC |
1327 | probability = REG_BR_PROB_BASE; |
1328 | else | |
ecd4f20a MP |
1329 | { |
1330 | /* If loop_count is too big, such that REG_BR_PROB_BASE * loop_count | |
1331 | could overflow, shift both loop_count and compare_count right | |
1332 | a bit so that it doesn't overflow. Note both counts are known not | |
1333 | to be negative at this point. */ | |
1334 | int clz_bits = clz_hwi (loop_count.high); | |
1335 | gcc_assert (REG_BR_PROB_BASE < 32768); | |
1336 | if (clz_bits < 16) | |
1337 | { | |
1338 | loop_count.arshift (16 - clz_bits, HOST_BITS_PER_DOUBLE_INT); | |
1339 | compare_count.arshift (16 - clz_bits, HOST_BITS_PER_DOUBLE_INT); | |
1340 | } | |
1341 | tem = compare_count.mul_with_sign (double_int::from_shwi | |
1342 | (REG_BR_PROB_BASE), true, &of); | |
1343 | gcc_assert (!of); | |
1344 | tem = tem.divmod (loop_count, true, TRUNC_DIV_EXPR, &mod); | |
1345 | probability = tem.to_uhwi (); | |
1346 | } | |
1347 | ||
1348 | if (!overflow) | |
1349 | predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability); | |
1350 | ||
9c04723a DC |
1351 | return; |
1352 | } | |
1353 | ||
1354 | if (expr_coherent_p (loop_bound_var, compare_var)) | |
1355 | { | |
1356 | if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR) | |
1357 | && (compare_code == LT_EXPR || compare_code == LE_EXPR)) | |
1358 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1359 | else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR) | |
1360 | && (compare_code == GT_EXPR || compare_code == GE_EXPR)) | |
1361 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1362 | else if (loop_bound_code == NE_EXPR) | |
1363 | { | |
1364 | /* If the loop backedge condition is "(i != bound)", we do | |
1365 | the comparison based on the step of IV: | |
1366 | * step < 0 : backedge condition is like (i > bound) | |
1367 | * step > 0 : backedge condition is like (i < bound) */ | |
1368 | gcc_assert (loop_bound_step != 0); | |
1369 | if (loop_bound_step > 0 | |
1370 | && (compare_code == LT_EXPR | |
1371 | || compare_code == LE_EXPR)) | |
1372 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1373 | else if (loop_bound_step < 0 | |
1374 | && (compare_code == GT_EXPR | |
1375 | || compare_code == GE_EXPR)) | |
1376 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1377 | else | |
1378 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN); | |
1379 | } | |
1380 | else | |
1381 | /* The branch is predicted not-taken if loop_bound_code is | |
1382 | opposite with compare_code. */ | |
1383 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN); | |
1384 | } | |
1385 | else if (expr_coherent_p (loop_iv_base_var, compare_var)) | |
1386 | { | |
1387 | /* For cases like: | |
1388 | for (i = s; i < h; i++) | |
1389 | if (i > s + 2) .... | |
1390 | The branch should be predicted taken. */ | |
1391 | if (loop_bound_step > 0 | |
1392 | && (compare_code == GT_EXPR || compare_code == GE_EXPR)) | |
1393 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1394 | else if (loop_bound_step < 0 | |
1395 | && (compare_code == LT_EXPR || compare_code == LE_EXPR)) | |
1396 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN); | |
1397 | else | |
1398 | predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN); | |
1399 | } | |
1400 | } | |
16fdb75f DC |
1401 | |
1402 | /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop | |
1403 | exits are resulted from short-circuit conditions that will generate an | |
1404 | if_tmp. E.g.: | |
1405 | ||
1406 | if (foo() || global > 10) | |
1407 | break; | |
1408 | ||
1409 | This will be translated into: | |
1410 | ||
1411 | BB3: | |
1412 | loop header... | |
1413 | BB4: | |
1414 | if foo() goto BB6 else goto BB5 | |
1415 | BB5: | |
1416 | if global > 10 goto BB6 else goto BB7 | |
1417 | BB6: | |
1418 | goto BB7 | |
1419 | BB7: | |
1420 | iftmp = (PHI 0(BB5), 1(BB6)) | |
1421 | if iftmp == 1 goto BB8 else goto BB3 | |
1422 | BB8: | |
1423 | outside of the loop... | |
1424 | ||
1425 | The edge BB7->BB8 is loop exit because BB8 is outside of the loop. | |
1426 | From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop | |
1427 | exits. This function takes BB7->BB8 as input, and finds out the extra loop | |
1428 | exits to predict them using PRED_LOOP_EXIT. */ | |
1429 | ||
1430 | static void | |
1431 | predict_extra_loop_exits (edge exit_edge) | |
1432 | { | |
1433 | unsigned i; | |
1434 | bool check_value_one; | |
1435 | gimple phi_stmt; | |
1436 | tree cmp_rhs, cmp_lhs; | |
1437 | gimple cmp_stmt = last_stmt (exit_edge->src); | |
1438 | ||
1439 | if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND) | |
1440 | return; | |
1441 | cmp_rhs = gimple_cond_rhs (cmp_stmt); | |
1442 | cmp_lhs = gimple_cond_lhs (cmp_stmt); | |
1443 | if (!TREE_CONSTANT (cmp_rhs) | |
1444 | || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs))) | |
1445 | return; | |
1446 | if (TREE_CODE (cmp_lhs) != SSA_NAME) | |
1447 | return; | |
1448 | ||
1449 | /* If check_value_one is true, only the phi_args with value '1' will lead | |
1450 | to loop exit. Otherwise, only the phi_args with value '0' will lead to | |
1451 | loop exit. */ | |
1452 | check_value_one = (((integer_onep (cmp_rhs)) | |
1453 | ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR)) | |
1454 | ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0)); | |
1455 | ||
1456 | phi_stmt = SSA_NAME_DEF_STMT (cmp_lhs); | |
1457 | if (!phi_stmt || gimple_code (phi_stmt) != GIMPLE_PHI) | |
1458 | return; | |
1459 | ||
1460 | for (i = 0; i < gimple_phi_num_args (phi_stmt); i++) | |
1461 | { | |
1462 | edge e1; | |
1463 | edge_iterator ei; | |
1464 | tree val = gimple_phi_arg_def (phi_stmt, i); | |
1465 | edge e = gimple_phi_arg_edge (phi_stmt, i); | |
1466 | ||
1467 | if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val))) | |
1468 | continue; | |
1469 | if ((check_value_one ^ integer_onep (val)) == 1) | |
1470 | continue; | |
1471 | if (EDGE_COUNT (e->src->succs) != 1) | |
1472 | { | |
1473 | predict_paths_leading_to_edge (e, PRED_LOOP_EXIT, NOT_TAKEN); | |
1474 | continue; | |
1475 | } | |
1476 | ||
1477 | FOR_EACH_EDGE (e1, ei, e->src->preds) | |
1478 | predict_paths_leading_to_edge (e1, PRED_LOOP_EXIT, NOT_TAKEN); | |
1479 | } | |
1480 | } | |
1481 | ||
d73be268 ZD |
1482 | /* Predict edge probabilities by exploiting loop structure. */ |
1483 | ||
6de9cd9a | 1484 | static void |
d73be268 | 1485 | predict_loops (void) |
6de9cd9a | 1486 | { |
42fd6772 ZD |
1487 | loop_iterator li; |
1488 | struct loop *loop; | |
0b92ff33 | 1489 | |
65169dcf JE |
1490 | /* Try to predict out blocks in a loop that are not part of a |
1491 | natural loop. */ | |
42fd6772 | 1492 | FOR_EACH_LOOP (li, loop, 0) |
f1ebdfc5 | 1493 | { |
2ecfd709 | 1494 | basic_block bb, *bbs; |
ca83d385 | 1495 | unsigned j, n_exits; |
9771b263 | 1496 | vec<edge> exits; |
992c31e6 | 1497 | struct tree_niter_desc niter_desc; |
ca83d385 | 1498 | edge ex; |
9c04723a DC |
1499 | struct nb_iter_bound *nb_iter; |
1500 | enum tree_code loop_bound_code = ERROR_MARK; | |
ecd4f20a | 1501 | tree loop_bound_step = NULL; |
9c04723a DC |
1502 | tree loop_bound_var = NULL; |
1503 | tree loop_iv_base = NULL; | |
1504 | gimple stmt = NULL; | |
f1ebdfc5 | 1505 | |
ca83d385 | 1506 | exits = get_loop_exit_edges (loop); |
9771b263 | 1507 | n_exits = exits.length (); |
f481cd49 JH |
1508 | if (!n_exits) |
1509 | { | |
9771b263 | 1510 | exits.release (); |
f481cd49 JH |
1511 | continue; |
1512 | } | |
0dd0e980 | 1513 | |
9771b263 | 1514 | FOR_EACH_VEC_ELT (exits, j, ex) |
b6acab32 | 1515 | { |
992c31e6 | 1516 | tree niter = NULL; |
4839cb59 ZD |
1517 | HOST_WIDE_INT nitercst; |
1518 | int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS); | |
1519 | int probability; | |
1520 | enum br_predictor predictor; | |
b6acab32 | 1521 | |
16fdb75f DC |
1522 | predict_extra_loop_exits (ex); |
1523 | ||
46deac6c | 1524 | if (number_of_iterations_exit (loop, ex, &niter_desc, false, false)) |
992c31e6 JH |
1525 | niter = niter_desc.niter; |
1526 | if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST) | |
ca83d385 | 1527 | niter = loop_niter_by_eval (loop, ex); |
b6acab32 | 1528 | |
992c31e6 JH |
1529 | if (TREE_CODE (niter) == INTEGER_CST) |
1530 | { | |
992c31e6 | 1531 | if (host_integerp (niter, 1) |
2aa579ad JJ |
1532 | && max |
1533 | && compare_tree_int (niter, max - 1) == -1) | |
4839cb59 | 1534 | nitercst = tree_low_cst (niter, 1) + 1; |
992c31e6 | 1535 | else |
4839cb59 ZD |
1536 | nitercst = max; |
1537 | predictor = PRED_LOOP_ITERATIONS; | |
1538 | } | |
1539 | /* If we have just one exit and we can derive some information about | |
1540 | the number of iterations of the loop from the statements inside | |
1541 | the loop, use it to predict this exit. */ | |
1542 | else if (n_exits == 1) | |
1543 | { | |
652c4c71 | 1544 | nitercst = estimated_stmt_executions_int (loop); |
4839cb59 ZD |
1545 | if (nitercst < 0) |
1546 | continue; | |
1547 | if (nitercst > max) | |
1548 | nitercst = max; | |
b6acab32 | 1549 | |
4839cb59 | 1550 | predictor = PRED_LOOP_ITERATIONS_GUESSED; |
992c31e6 | 1551 | } |
4839cb59 ZD |
1552 | else |
1553 | continue; | |
1554 | ||
2aa579ad JJ |
1555 | /* If the prediction for number of iterations is zero, do not |
1556 | predict the exit edges. */ | |
1557 | if (nitercst == 0) | |
1558 | continue; | |
1559 | ||
4839cb59 ZD |
1560 | probability = ((REG_BR_PROB_BASE + nitercst / 2) / nitercst); |
1561 | predict_edge (ex, predictor, probability); | |
b6acab32 | 1562 | } |
9771b263 | 1563 | exits.release (); |
3d436d2a | 1564 | |
9c04723a DC |
1565 | /* Find information about loop bound variables. */ |
1566 | for (nb_iter = loop->bounds; nb_iter; | |
1567 | nb_iter = nb_iter->next) | |
1568 | if (nb_iter->stmt | |
1569 | && gimple_code (nb_iter->stmt) == GIMPLE_COND) | |
1570 | { | |
1571 | stmt = nb_iter->stmt; | |
1572 | break; | |
1573 | } | |
1574 | if (!stmt && last_stmt (loop->header) | |
1575 | && gimple_code (last_stmt (loop->header)) == GIMPLE_COND) | |
1576 | stmt = last_stmt (loop->header); | |
1577 | if (stmt) | |
1578 | is_comparison_with_loop_invariant_p (stmt, loop, | |
1579 | &loop_bound_var, | |
1580 | &loop_bound_code, | |
1581 | &loop_bound_step, | |
1582 | &loop_iv_base); | |
1583 | ||
2ecfd709 | 1584 | bbs = get_loop_body (loop); |
6de9cd9a | 1585 | |
2ecfd709 ZD |
1586 | for (j = 0; j < loop->num_nodes; j++) |
1587 | { | |
1588 | int header_found = 0; | |
1589 | edge e; | |
628f6a4e | 1590 | edge_iterator ei; |
2ecfd709 ZD |
1591 | |
1592 | bb = bbs[j]; | |
bfdade77 | 1593 | |
969d70ca JH |
1594 | /* Bypass loop heuristics on continue statement. These |
1595 | statements construct loops via "non-loop" constructs | |
1596 | in the source language and are better to be handled | |
1597 | separately. */ | |
992c31e6 | 1598 | if (predicted_by_p (bb, PRED_CONTINUE)) |
969d70ca JH |
1599 | continue; |
1600 | ||
2ecfd709 ZD |
1601 | /* Loop branch heuristics - predict an edge back to a |
1602 | loop's head as taken. */ | |
9ff3d2de JL |
1603 | if (bb == loop->latch) |
1604 | { | |
1605 | e = find_edge (loop->latch, loop->header); | |
1606 | if (e) | |
1607 | { | |
1608 | header_found = 1; | |
1609 | predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN); | |
1610 | } | |
1611 | } | |
bfdade77 | 1612 | |
2ecfd709 | 1613 | /* Loop exit heuristics - predict an edge exiting the loop if the |
d55d8fc7 | 1614 | conditional has no loop header successors as not taken. */ |
4839cb59 ZD |
1615 | if (!header_found |
1616 | /* If we already used more reliable loop exit predictors, do not | |
1617 | bother with PRED_LOOP_EXIT. */ | |
1618 | && !predicted_by_p (bb, PRED_LOOP_ITERATIONS_GUESSED) | |
1619 | && !predicted_by_p (bb, PRED_LOOP_ITERATIONS)) | |
2c9e13f3 JH |
1620 | { |
1621 | /* For loop with many exits we don't want to predict all exits | |
1622 | with the pretty large probability, because if all exits are | |
1623 | considered in row, the loop would be predicted to iterate | |
1624 | almost never. The code to divide probability by number of | |
1625 | exits is very rough. It should compute the number of exits | |
1626 | taken in each patch through function (not the overall number | |
1627 | of exits that might be a lot higher for loops with wide switch | |
1628 | statements in them) and compute n-th square root. | |
1629 | ||
1630 | We limit the minimal probability by 2% to avoid | |
1631 | EDGE_PROBABILITY_RELIABLE from trusting the branch prediction | |
1632 | as this was causing regression in perl benchmark containing such | |
1633 | a wide loop. */ | |
b8698a0f | 1634 | |
2c9e13f3 JH |
1635 | int probability = ((REG_BR_PROB_BASE |
1636 | - predictor_info [(int) PRED_LOOP_EXIT].hitrate) | |
1637 | / n_exits); | |
1638 | if (probability < HITRATE (2)) | |
1639 | probability = HITRATE (2); | |
1640 | FOR_EACH_EDGE (e, ei, bb->succs) | |
1641 | if (e->dest->index < NUM_FIXED_BLOCKS | |
1642 | || !flow_bb_inside_loop_p (loop, e->dest)) | |
1643 | predict_edge (e, PRED_LOOP_EXIT, probability); | |
1644 | } | |
9c04723a DC |
1645 | if (loop_bound_var) |
1646 | predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base, | |
1647 | loop_bound_code, | |
ecd4f20a | 1648 | tree_low_cst (loop_bound_step, 0)); |
2ecfd709 | 1649 | } |
b8698a0f | 1650 | |
e0a21ab9 | 1651 | /* Free basic blocks from get_loop_body. */ |
36579663 | 1652 | free (bbs); |
f1ebdfc5 | 1653 | } |
6de9cd9a DN |
1654 | } |
1655 | ||
87022a6b JH |
1656 | /* Attempt to predict probabilities of BB outgoing edges using local |
1657 | properties. */ | |
1658 | static void | |
1659 | bb_estimate_probability_locally (basic_block bb) | |
1660 | { | |
1661 | rtx last_insn = BB_END (bb); | |
1662 | rtx cond; | |
1663 | ||
1664 | if (! can_predict_insn_p (last_insn)) | |
1665 | return; | |
1666 | cond = get_condition (last_insn, NULL, false, false); | |
1667 | if (! cond) | |
1668 | return; | |
1669 | ||
1670 | /* Try "pointer heuristic." | |
1671 | A comparison ptr == 0 is predicted as false. | |
1672 | Similarly, a comparison ptr1 == ptr2 is predicted as false. */ | |
1673 | if (COMPARISON_P (cond) | |
1674 | && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0))) | |
1675 | || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1))))) | |
1676 | { | |
1677 | if (GET_CODE (cond) == EQ) | |
1678 | predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN); | |
1679 | else if (GET_CODE (cond) == NE) | |
1680 | predict_insn_def (last_insn, PRED_POINTER, TAKEN); | |
1681 | } | |
1682 | else | |
1683 | ||
1684 | /* Try "opcode heuristic." | |
1685 | EQ tests are usually false and NE tests are usually true. Also, | |
1686 | most quantities are positive, so we can make the appropriate guesses | |
1687 | about signed comparisons against zero. */ | |
1688 | switch (GET_CODE (cond)) | |
1689 | { | |
1690 | case CONST_INT: | |
1691 | /* Unconditional branch. */ | |
1692 | predict_insn_def (last_insn, PRED_UNCONDITIONAL, | |
1693 | cond == const0_rtx ? NOT_TAKEN : TAKEN); | |
1694 | break; | |
1695 | ||
1696 | case EQ: | |
1697 | case UNEQ: | |
1698 | /* Floating point comparisons appears to behave in a very | |
1699 | unpredictable way because of special role of = tests in | |
1700 | FP code. */ | |
1701 | if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0)))) | |
1702 | ; | |
1703 | /* Comparisons with 0 are often used for booleans and there is | |
1704 | nothing useful to predict about them. */ | |
1705 | else if (XEXP (cond, 1) == const0_rtx | |
1706 | || XEXP (cond, 0) == const0_rtx) | |
1707 | ; | |
1708 | else | |
1709 | predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN); | |
1710 | break; | |
1711 | ||
1712 | case NE: | |
1713 | case LTGT: | |
1714 | /* Floating point comparisons appears to behave in a very | |
1715 | unpredictable way because of special role of = tests in | |
1716 | FP code. */ | |
1717 | if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0)))) | |
1718 | ; | |
1719 | /* Comparisons with 0 are often used for booleans and there is | |
1720 | nothing useful to predict about them. */ | |
1721 | else if (XEXP (cond, 1) == const0_rtx | |
1722 | || XEXP (cond, 0) == const0_rtx) | |
1723 | ; | |
1724 | else | |
1725 | predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN); | |
1726 | break; | |
1727 | ||
1728 | case ORDERED: | |
1729 | predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN); | |
1730 | break; | |
1731 | ||
1732 | case UNORDERED: | |
1733 | predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN); | |
1734 | break; | |
1735 | ||
1736 | case LE: | |
1737 | case LT: | |
1738 | if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx | |
1739 | || XEXP (cond, 1) == constm1_rtx) | |
1740 | predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN); | |
1741 | break; | |
1742 | ||
1743 | case GE: | |
1744 | case GT: | |
1745 | if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx | |
1746 | || XEXP (cond, 1) == constm1_rtx) | |
1747 | predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN); | |
1748 | break; | |
1749 | ||
1750 | default: | |
1751 | break; | |
1752 | } | |
1753 | } | |
1754 | ||
229031d0 | 1755 | /* Set edge->probability for each successor edge of BB. */ |
87022a6b JH |
1756 | void |
1757 | guess_outgoing_edge_probabilities (basic_block bb) | |
1758 | { | |
1759 | bb_estimate_probability_locally (bb); | |
1760 | combine_predictions_for_insn (BB_END (bb), bb); | |
1761 | } | |
6de9cd9a | 1762 | \f |
726a989a RB |
1763 | static tree expr_expected_value (tree, bitmap); |
1764 | ||
1765 | /* Helper function for expr_expected_value. */ | |
42f97fd2 JH |
1766 | |
1767 | static tree | |
c08472ea RH |
1768 | expr_expected_value_1 (tree type, tree op0, enum tree_code code, |
1769 | tree op1, bitmap visited) | |
42f97fd2 | 1770 | { |
726a989a RB |
1771 | gimple def; |
1772 | ||
1773 | if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) | |
42f97fd2 | 1774 | { |
726a989a RB |
1775 | if (TREE_CONSTANT (op0)) |
1776 | return op0; | |
1777 | ||
1778 | if (code != SSA_NAME) | |
1779 | return NULL_TREE; | |
1780 | ||
1781 | def = SSA_NAME_DEF_STMT (op0); | |
42f97fd2 JH |
1782 | |
1783 | /* If we were already here, break the infinite cycle. */ | |
fcaa4ca4 | 1784 | if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0))) |
42f97fd2 | 1785 | return NULL; |
42f97fd2 | 1786 | |
726a989a | 1787 | if (gimple_code (def) == GIMPLE_PHI) |
42f97fd2 JH |
1788 | { |
1789 | /* All the arguments of the PHI node must have the same constant | |
1790 | length. */ | |
726a989a | 1791 | int i, n = gimple_phi_num_args (def); |
42f97fd2 | 1792 | tree val = NULL, new_val; |
6de9cd9a | 1793 | |
726a989a | 1794 | for (i = 0; i < n; i++) |
42f97fd2 JH |
1795 | { |
1796 | tree arg = PHI_ARG_DEF (def, i); | |
1797 | ||
1798 | /* If this PHI has itself as an argument, we cannot | |
1799 | determine the string length of this argument. However, | |
1f838355 | 1800 | if we can find an expected constant value for the other |
42f97fd2 JH |
1801 | PHI args then we can still be sure that this is |
1802 | likely a constant. So be optimistic and just | |
1803 | continue with the next argument. */ | |
1804 | if (arg == PHI_RESULT (def)) | |
1805 | continue; | |
1806 | ||
1807 | new_val = expr_expected_value (arg, visited); | |
1808 | if (!new_val) | |
1809 | return NULL; | |
1810 | if (!val) | |
1811 | val = new_val; | |
1812 | else if (!operand_equal_p (val, new_val, false)) | |
1813 | return NULL; | |
1814 | } | |
1815 | return val; | |
1816 | } | |
726a989a | 1817 | if (is_gimple_assign (def)) |
42f97fd2 | 1818 | { |
726a989a RB |
1819 | if (gimple_assign_lhs (def) != op0) |
1820 | return NULL; | |
42f97fd2 | 1821 | |
726a989a RB |
1822 | return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)), |
1823 | gimple_assign_rhs1 (def), | |
1824 | gimple_assign_rhs_code (def), | |
1825 | gimple_assign_rhs2 (def), | |
1826 | visited); | |
1827 | } | |
1828 | ||
1829 | if (is_gimple_call (def)) | |
1830 | { | |
1831 | tree decl = gimple_call_fndecl (def); | |
1832 | if (!decl) | |
5039610b | 1833 | return NULL; |
c08472ea RH |
1834 | if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) |
1835 | switch (DECL_FUNCTION_CODE (decl)) | |
1836 | { | |
1837 | case BUILT_IN_EXPECT: | |
1838 | { | |
1839 | tree val; | |
1840 | if (gimple_call_num_args (def) != 2) | |
1841 | return NULL; | |
1842 | val = gimple_call_arg (def, 0); | |
1843 | if (TREE_CONSTANT (val)) | |
1844 | return val; | |
1845 | return gimple_call_arg (def, 1); | |
1846 | } | |
726a989a | 1847 | |
c08472ea RH |
1848 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: |
1849 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: | |
1850 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: | |
1851 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: | |
1852 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: | |
1853 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: | |
1854 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
1855 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
1856 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
1857 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
1858 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
1859 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
1860 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
1861 | /* Assume that any given atomic operation has low contention, | |
1862 | and thus the compare-and-swap operation succeeds. */ | |
1863 | return boolean_true_node; | |
726a989a | 1864 | } |
42f97fd2 | 1865 | } |
726a989a RB |
1866 | |
1867 | return NULL; | |
42f97fd2 | 1868 | } |
726a989a RB |
1869 | |
1870 | if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS) | |
42f97fd2 | 1871 | { |
726a989a RB |
1872 | tree res; |
1873 | op0 = expr_expected_value (op0, visited); | |
42f97fd2 JH |
1874 | if (!op0) |
1875 | return NULL; | |
726a989a | 1876 | op1 = expr_expected_value (op1, visited); |
42f97fd2 JH |
1877 | if (!op1) |
1878 | return NULL; | |
726a989a | 1879 | res = fold_build2 (code, type, op0, op1); |
42f97fd2 JH |
1880 | if (TREE_CONSTANT (res)) |
1881 | return res; | |
1882 | return NULL; | |
1883 | } | |
726a989a | 1884 | if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS) |
42f97fd2 | 1885 | { |
726a989a RB |
1886 | tree res; |
1887 | op0 = expr_expected_value (op0, visited); | |
42f97fd2 JH |
1888 | if (!op0) |
1889 | return NULL; | |
726a989a | 1890 | res = fold_build1 (code, type, op0); |
42f97fd2 JH |
1891 | if (TREE_CONSTANT (res)) |
1892 | return res; | |
1893 | return NULL; | |
1894 | } | |
1895 | return NULL; | |
1896 | } | |
726a989a | 1897 | |
b8698a0f | 1898 | /* Return constant EXPR will likely have at execution time, NULL if unknown. |
726a989a RB |
1899 | The function is used by builtin_expect branch predictor so the evidence |
1900 | must come from this construct and additional possible constant folding. | |
b8698a0f | 1901 | |
726a989a RB |
1902 | We may want to implement more involved value guess (such as value range |
1903 | propagation based prediction), but such tricks shall go to new | |
1904 | implementation. */ | |
1905 | ||
1906 | static tree | |
1907 | expr_expected_value (tree expr, bitmap visited) | |
1908 | { | |
1909 | enum tree_code code; | |
1910 | tree op0, op1; | |
1911 | ||
1912 | if (TREE_CONSTANT (expr)) | |
1913 | return expr; | |
1914 | ||
1915 | extract_ops_from_tree (expr, &code, &op0, &op1); | |
1916 | return expr_expected_value_1 (TREE_TYPE (expr), | |
1917 | op0, code, op1, visited); | |
1918 | } | |
1919 | ||
42f97fd2 | 1920 | \f |
7299cb99 JH |
1921 | /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements |
1922 | we no longer need. */ | |
1923 | static unsigned int | |
1924 | strip_predict_hints (void) | |
42f97fd2 JH |
1925 | { |
1926 | basic_block bb; | |
726a989a RB |
1927 | gimple ass_stmt; |
1928 | tree var; | |
1929 | ||
42f97fd2 JH |
1930 | FOR_EACH_BB (bb) |
1931 | { | |
726a989a | 1932 | gimple_stmt_iterator bi; |
7299cb99 | 1933 | for (bi = gsi_start_bb (bb); !gsi_end_p (bi);) |
42f97fd2 | 1934 | { |
726a989a | 1935 | gimple stmt = gsi_stmt (bi); |
726a989a | 1936 | |
7299cb99 JH |
1937 | if (gimple_code (stmt) == GIMPLE_PREDICT) |
1938 | { | |
1939 | gsi_remove (&bi, true); | |
1940 | continue; | |
1941 | } | |
1942 | else if (gimple_code (stmt) == GIMPLE_CALL) | |
42f97fd2 | 1943 | { |
7299cb99 | 1944 | tree fndecl = gimple_call_fndecl (stmt); |
726a989a | 1945 | |
7299cb99 JH |
1946 | if (fndecl |
1947 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL | |
1948 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT | |
1949 | && gimple_call_num_args (stmt) == 2) | |
1950 | { | |
1951 | var = gimple_call_lhs (stmt); | |
44cb6d4d RG |
1952 | if (var) |
1953 | { | |
1954 | ass_stmt | |
1955 | = gimple_build_assign (var, gimple_call_arg (stmt, 0)); | |
1956 | gsi_replace (&bi, ass_stmt, true); | |
1957 | } | |
1958 | else | |
1959 | { | |
1960 | gsi_remove (&bi, true); | |
1961 | continue; | |
1962 | } | |
7299cb99 | 1963 | } |
42f97fd2 | 1964 | } |
7299cb99 | 1965 | gsi_next (&bi); |
42f97fd2 JH |
1966 | } |
1967 | } | |
7299cb99 | 1968 | return 0; |
42f97fd2 JH |
1969 | } |
1970 | \f | |
6de9cd9a DN |
1971 | /* Predict using opcode of the last statement in basic block. */ |
1972 | static void | |
1973 | tree_predict_by_opcode (basic_block bb) | |
1974 | { | |
726a989a | 1975 | gimple stmt = last_stmt (bb); |
6de9cd9a | 1976 | edge then_edge; |
726a989a | 1977 | tree op0, op1; |
6de9cd9a | 1978 | tree type; |
42f97fd2 | 1979 | tree val; |
726a989a | 1980 | enum tree_code cmp; |
42f97fd2 | 1981 | bitmap visited; |
628f6a4e | 1982 | edge_iterator ei; |
6de9cd9a | 1983 | |
726a989a | 1984 | if (!stmt || gimple_code (stmt) != GIMPLE_COND) |
6de9cd9a | 1985 | return; |
628f6a4e | 1986 | FOR_EACH_EDGE (then_edge, ei, bb->succs) |
6de9cd9a | 1987 | if (then_edge->flags & EDGE_TRUE_VALUE) |
628f6a4e | 1988 | break; |
726a989a RB |
1989 | op0 = gimple_cond_lhs (stmt); |
1990 | op1 = gimple_cond_rhs (stmt); | |
1991 | cmp = gimple_cond_code (stmt); | |
6de9cd9a | 1992 | type = TREE_TYPE (op0); |
8bdbfff5 | 1993 | visited = BITMAP_ALLOC (NULL); |
726a989a | 1994 | val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, visited); |
8bdbfff5 | 1995 | BITMAP_FREE (visited); |
42f97fd2 JH |
1996 | if (val) |
1997 | { | |
1998 | if (integer_zerop (val)) | |
1999 | predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, NOT_TAKEN); | |
2000 | else | |
2001 | predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, TAKEN); | |
2002 | return; | |
2003 | } | |
6de9cd9a DN |
2004 | /* Try "pointer heuristic." |
2005 | A comparison ptr == 0 is predicted as false. | |
2006 | Similarly, a comparison ptr1 == ptr2 is predicted as false. */ | |
2007 | if (POINTER_TYPE_P (type)) | |
2008 | { | |
726a989a | 2009 | if (cmp == EQ_EXPR) |
6de9cd9a | 2010 | predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN); |
726a989a | 2011 | else if (cmp == NE_EXPR) |
6de9cd9a DN |
2012 | predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN); |
2013 | } | |
2014 | else | |
2015 | ||
2016 | /* Try "opcode heuristic." | |
2017 | EQ tests are usually false and NE tests are usually true. Also, | |
2018 | most quantities are positive, so we can make the appropriate guesses | |
2019 | about signed comparisons against zero. */ | |
726a989a | 2020 | switch (cmp) |
6de9cd9a DN |
2021 | { |
2022 | case EQ_EXPR: | |
2023 | case UNEQ_EXPR: | |
2024 | /* Floating point comparisons appears to behave in a very | |
2025 | unpredictable way because of special role of = tests in | |
2026 | FP code. */ | |
2027 | if (FLOAT_TYPE_P (type)) | |
2028 | ; | |
2029 | /* Comparisons with 0 are often used for booleans and there is | |
2030 | nothing useful to predict about them. */ | |
726a989a | 2031 | else if (integer_zerop (op0) || integer_zerop (op1)) |
6de9cd9a DN |
2032 | ; |
2033 | else | |
2034 | predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN); | |
2035 | break; | |
2036 | ||
2037 | case NE_EXPR: | |
d1a7edaf | 2038 | case LTGT_EXPR: |
6de9cd9a DN |
2039 | /* Floating point comparisons appears to behave in a very |
2040 | unpredictable way because of special role of = tests in | |
2041 | FP code. */ | |
2042 | if (FLOAT_TYPE_P (type)) | |
2043 | ; | |
2044 | /* Comparisons with 0 are often used for booleans and there is | |
2045 | nothing useful to predict about them. */ | |
2046 | else if (integer_zerop (op0) | |
726a989a | 2047 | || integer_zerop (op1)) |
6de9cd9a DN |
2048 | ; |
2049 | else | |
2050 | predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN); | |
2051 | break; | |
2052 | ||
2053 | case ORDERED_EXPR: | |
2054 | predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN); | |
2055 | break; | |
2056 | ||
2057 | case UNORDERED_EXPR: | |
2058 | predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN); | |
2059 | break; | |
2060 | ||
2061 | case LE_EXPR: | |
2062 | case LT_EXPR: | |
726a989a RB |
2063 | if (integer_zerop (op1) |
2064 | || integer_onep (op1) | |
2065 | || integer_all_onesp (op1) | |
2066 | || real_zerop (op1) | |
2067 | || real_onep (op1) | |
2068 | || real_minus_onep (op1)) | |
6de9cd9a DN |
2069 | predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN); |
2070 | break; | |
2071 | ||
2072 | case GE_EXPR: | |
2073 | case GT_EXPR: | |
726a989a RB |
2074 | if (integer_zerop (op1) |
2075 | || integer_onep (op1) | |
2076 | || integer_all_onesp (op1) | |
2077 | || real_zerop (op1) | |
2078 | || real_onep (op1) | |
2079 | || real_minus_onep (op1)) | |
6de9cd9a DN |
2080 | predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN); |
2081 | break; | |
2082 | ||
2083 | default: | |
2084 | break; | |
2085 | } | |
2086 | } | |
2087 | ||
bb033fd8 | 2088 | /* Try to guess whether the value of return means error code. */ |
726a989a | 2089 | |
bb033fd8 JH |
2090 | static enum br_predictor |
2091 | return_prediction (tree val, enum prediction *prediction) | |
2092 | { | |
2093 | /* VOID. */ | |
2094 | if (!val) | |
2095 | return PRED_NO_PREDICTION; | |
2096 | /* Different heuristics for pointers and scalars. */ | |
2097 | if (POINTER_TYPE_P (TREE_TYPE (val))) | |
2098 | { | |
2099 | /* NULL is usually not returned. */ | |
2100 | if (integer_zerop (val)) | |
2101 | { | |
2102 | *prediction = NOT_TAKEN; | |
2103 | return PRED_NULL_RETURN; | |
2104 | } | |
2105 | } | |
2106 | else if (INTEGRAL_TYPE_P (TREE_TYPE (val))) | |
2107 | { | |
2108 | /* Negative return values are often used to indicate | |
2109 | errors. */ | |
2110 | if (TREE_CODE (val) == INTEGER_CST | |
2111 | && tree_int_cst_sgn (val) < 0) | |
2112 | { | |
2113 | *prediction = NOT_TAKEN; | |
2114 | return PRED_NEGATIVE_RETURN; | |
2115 | } | |
2116 | /* Constant return values seems to be commonly taken. | |
2117 | Zero/one often represent booleans so exclude them from the | |
2118 | heuristics. */ | |
2119 | if (TREE_CONSTANT (val) | |
2120 | && (!integer_zerop (val) && !integer_onep (val))) | |
2121 | { | |
2122 | *prediction = TAKEN; | |
75b6bb62 | 2123 | return PRED_CONST_RETURN; |
bb033fd8 JH |
2124 | } |
2125 | } | |
2126 | return PRED_NO_PREDICTION; | |
2127 | } | |
2128 | ||
2129 | /* Find the basic block with return expression and look up for possible | |
2130 | return value trying to apply RETURN_PREDICTION heuristics. */ | |
2131 | static void | |
3e4b9ad0 | 2132 | apply_return_prediction (void) |
bb033fd8 | 2133 | { |
726a989a | 2134 | gimple return_stmt = NULL; |
bb033fd8 JH |
2135 | tree return_val; |
2136 | edge e; | |
726a989a | 2137 | gimple phi; |
bb033fd8 JH |
2138 | int phi_num_args, i; |
2139 | enum br_predictor pred; | |
2140 | enum prediction direction; | |
628f6a4e | 2141 | edge_iterator ei; |
bb033fd8 | 2142 | |
628f6a4e | 2143 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds) |
bb033fd8 JH |
2144 | { |
2145 | return_stmt = last_stmt (e->src); | |
8b11009b | 2146 | if (return_stmt |
726a989a | 2147 | && gimple_code (return_stmt) == GIMPLE_RETURN) |
bb033fd8 JH |
2148 | break; |
2149 | } | |
2150 | if (!e) | |
2151 | return; | |
726a989a | 2152 | return_val = gimple_return_retval (return_stmt); |
bb033fd8 JH |
2153 | if (!return_val) |
2154 | return; | |
bb033fd8 JH |
2155 | if (TREE_CODE (return_val) != SSA_NAME |
2156 | || !SSA_NAME_DEF_STMT (return_val) | |
726a989a | 2157 | || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI) |
bb033fd8 | 2158 | return; |
726a989a RB |
2159 | phi = SSA_NAME_DEF_STMT (return_val); |
2160 | phi_num_args = gimple_phi_num_args (phi); | |
bb033fd8 JH |
2161 | pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction); |
2162 | ||
2163 | /* Avoid the degenerate case where all return values form the function | |
2164 | belongs to same category (ie they are all positive constants) | |
2165 | so we can hardly say something about them. */ | |
2166 | for (i = 1; i < phi_num_args; i++) | |
2167 | if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction)) | |
2168 | break; | |
2169 | if (i != phi_num_args) | |
2170 | for (i = 0; i < phi_num_args; i++) | |
2171 | { | |
2172 | pred = return_prediction (PHI_ARG_DEF (phi, i), &direction); | |
2173 | if (pred != PRED_NO_PREDICTION) | |
5210bbc5 JH |
2174 | predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred, |
2175 | direction); | |
bb033fd8 JH |
2176 | } |
2177 | } | |
2178 | ||
2179 | /* Look for basic block that contains unlikely to happen events | |
2180 | (such as noreturn calls) and mark all paths leading to execution | |
2181 | of this basic blocks as unlikely. */ | |
2182 | ||
2183 | static void | |
2184 | tree_bb_level_predictions (void) | |
2185 | { | |
2186 | basic_block bb; | |
c0ee0021 JH |
2187 | bool has_return_edges = false; |
2188 | edge e; | |
2189 | edge_iterator ei; | |
2190 | ||
2191 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds) | |
2192 | if (!(e->flags & (EDGE_ABNORMAL | EDGE_FAKE | EDGE_EH))) | |
2193 | { | |
2194 | has_return_edges = true; | |
2195 | break; | |
2196 | } | |
bb033fd8 | 2197 | |
3e4b9ad0 | 2198 | apply_return_prediction (); |
bb033fd8 JH |
2199 | |
2200 | FOR_EACH_BB (bb) | |
2201 | { | |
726a989a | 2202 | gimple_stmt_iterator gsi; |
bb033fd8 | 2203 | |
7299cb99 | 2204 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
bb033fd8 | 2205 | { |
726a989a | 2206 | gimple stmt = gsi_stmt (gsi); |
52bf96d2 | 2207 | tree decl; |
daac0317 | 2208 | |
726a989a | 2209 | if (is_gimple_call (stmt)) |
bb033fd8 | 2210 | { |
c0ee0021 JH |
2211 | if ((gimple_call_flags (stmt) & ECF_NORETURN) |
2212 | && has_return_edges) | |
726a989a RB |
2213 | predict_paths_leading_to (bb, PRED_NORETURN, |
2214 | NOT_TAKEN); | |
2215 | decl = gimple_call_fndecl (stmt); | |
2216 | if (decl | |
2217 | && lookup_attribute ("cold", | |
2218 | DECL_ATTRIBUTES (decl))) | |
2219 | predict_paths_leading_to (bb, PRED_COLD_FUNCTION, | |
2220 | NOT_TAKEN); | |
bb033fd8 | 2221 | } |
726a989a RB |
2222 | else if (gimple_code (stmt) == GIMPLE_PREDICT) |
2223 | { | |
2224 | predict_paths_leading_to (bb, gimple_predict_predictor (stmt), | |
2225 | gimple_predict_outcome (stmt)); | |
7299cb99 JH |
2226 | /* Keep GIMPLE_PREDICT around so early inlining will propagate |
2227 | hints to callers. */ | |
726a989a | 2228 | } |
bb033fd8 JH |
2229 | } |
2230 | } | |
bb033fd8 JH |
2231 | } |
2232 | ||
f06b0a10 ZD |
2233 | #ifdef ENABLE_CHECKING |
2234 | ||
2235 | /* Callback for pointer_map_traverse, asserts that the pointer map is | |
2236 | empty. */ | |
2237 | ||
2238 | static bool | |
ac7d7749 | 2239 | assert_is_empty (const void *key ATTRIBUTE_UNUSED, void **value, |
f06b0a10 ZD |
2240 | void *data ATTRIBUTE_UNUSED) |
2241 | { | |
2242 | gcc_assert (!*value); | |
2243 | return false; | |
2244 | } | |
2245 | #endif | |
2246 | ||
8e88f9fd SP |
2247 | /* Predict branch probabilities and estimate profile for basic block BB. */ |
2248 | ||
2249 | static void | |
2250 | tree_estimate_probability_bb (basic_block bb) | |
2251 | { | |
2252 | edge e; | |
2253 | edge_iterator ei; | |
2254 | gimple last; | |
2255 | ||
2256 | FOR_EACH_EDGE (e, ei, bb->succs) | |
2257 | { | |
e45abe1f RH |
2258 | /* Predict edges to user labels with attributes. */ |
2259 | if (e->dest != EXIT_BLOCK_PTR) | |
2260 | { | |
2261 | gimple_stmt_iterator gi; | |
2262 | for (gi = gsi_start_bb (e->dest); !gsi_end_p (gi); gsi_next (&gi)) | |
2263 | { | |
2264 | gimple stmt = gsi_stmt (gi); | |
2265 | tree decl; | |
2266 | ||
2267 | if (gimple_code (stmt) != GIMPLE_LABEL) | |
2268 | break; | |
2269 | decl = gimple_label_label (stmt); | |
2270 | if (DECL_ARTIFICIAL (decl)) | |
2271 | continue; | |
2272 | ||
2273 | /* Finally, we have a user-defined label. */ | |
2274 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))) | |
2275 | predict_edge_def (e, PRED_COLD_LABEL, NOT_TAKEN); | |
2276 | else if (lookup_attribute ("hot", DECL_ATTRIBUTES (decl))) | |
2277 | predict_edge_def (e, PRED_HOT_LABEL, TAKEN); | |
2278 | } | |
2279 | } | |
2280 | ||
8e88f9fd SP |
2281 | /* Predict early returns to be probable, as we've already taken |
2282 | care for error returns and other cases are often used for | |
2283 | fast paths through function. | |
2284 | ||
2285 | Since we've already removed the return statements, we are | |
2286 | looking for CFG like: | |
2287 | ||
2288 | if (conditional) | |
2289 | { | |
2290 | .. | |
2291 | goto return_block | |
2292 | } | |
2293 | some other blocks | |
2294 | return_block: | |
2295 | return_stmt. */ | |
2296 | if (e->dest != bb->next_bb | |
2297 | && e->dest != EXIT_BLOCK_PTR | |
2298 | && single_succ_p (e->dest) | |
2299 | && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR | |
2300 | && (last = last_stmt (e->dest)) != NULL | |
2301 | && gimple_code (last) == GIMPLE_RETURN) | |
2302 | { | |
2303 | edge e1; | |
2304 | edge_iterator ei1; | |
2305 | ||
2306 | if (single_succ_p (bb)) | |
2307 | { | |
2308 | FOR_EACH_EDGE (e1, ei1, bb->preds) | |
2309 | if (!predicted_by_p (e1->src, PRED_NULL_RETURN) | |
2310 | && !predicted_by_p (e1->src, PRED_CONST_RETURN) | |
2311 | && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN)) | |
2312 | predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
2313 | } | |
2314 | else | |
2315 | if (!predicted_by_p (e->src, PRED_NULL_RETURN) | |
2316 | && !predicted_by_p (e->src, PRED_CONST_RETURN) | |
2317 | && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN)) | |
2318 | predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
2319 | } | |
2320 | ||
2321 | /* Look for block we are guarding (ie we dominate it, | |
2322 | but it doesn't postdominate us). */ | |
2323 | if (e->dest != EXIT_BLOCK_PTR && e->dest != bb | |
2324 | && dominated_by_p (CDI_DOMINATORS, e->dest, e->src) | |
2325 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest)) | |
2326 | { | |
2327 | gimple_stmt_iterator bi; | |
2328 | ||
2329 | /* The call heuristic claims that a guarded function call | |
2330 | is improbable. This is because such calls are often used | |
2331 | to signal exceptional situations such as printing error | |
2332 | messages. */ | |
2333 | for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi); | |
2334 | gsi_next (&bi)) | |
2335 | { | |
2336 | gimple stmt = gsi_stmt (bi); | |
2337 | if (is_gimple_call (stmt) | |
2338 | /* Constant and pure calls are hardly used to signalize | |
2339 | something exceptional. */ | |
2340 | && gimple_has_side_effects (stmt)) | |
2341 | { | |
2342 | predict_edge_def (e, PRED_CALL, NOT_TAKEN); | |
2343 | break; | |
2344 | } | |
2345 | } | |
2346 | } | |
2347 | } | |
2348 | tree_predict_by_opcode (bb); | |
2349 | } | |
2350 | ||
2351 | /* Predict branch probabilities and estimate profile of the tree CFG. | |
2352 | This function can be called from the loop optimizers to recompute | |
2353 | the profile information. */ | |
2354 | ||
2355 | void | |
6de9cd9a DN |
2356 | tree_estimate_probability (void) |
2357 | { | |
2358 | basic_block bb; | |
6de9cd9a | 2359 | |
bb033fd8 | 2360 | add_noreturn_fake_exit_edges (); |
6de9cd9a | 2361 | connect_infinite_loops_to_exit (); |
c7b852c8 ZD |
2362 | /* We use loop_niter_by_eval, which requires that the loops have |
2363 | preheaders. */ | |
2364 | create_preheaders (CP_SIMPLE_PREHEADERS); | |
6de9cd9a DN |
2365 | calculate_dominance_info (CDI_POST_DOMINATORS); |
2366 | ||
f06b0a10 | 2367 | bb_predictions = pointer_map_create (); |
bb033fd8 | 2368 | tree_bb_level_predictions (); |
4839cb59 | 2369 | record_loop_exits (); |
8e88f9fd | 2370 | |
0fc822d0 | 2371 | if (number_of_loops (cfun) > 1) |
d73be268 | 2372 | predict_loops (); |
6de9cd9a DN |
2373 | |
2374 | FOR_EACH_BB (bb) | |
8e88f9fd | 2375 | tree_estimate_probability_bb (bb); |
6de9cd9a | 2376 | |
6de9cd9a | 2377 | FOR_EACH_BB (bb) |
10d22567 | 2378 | combine_predictions_for_bb (bb); |
861f9cd0 | 2379 | |
f06b0a10 ZD |
2380 | #ifdef ENABLE_CHECKING |
2381 | pointer_map_traverse (bb_predictions, assert_is_empty, NULL); | |
2382 | #endif | |
2383 | pointer_map_destroy (bb_predictions); | |
2384 | bb_predictions = NULL; | |
2385 | ||
d73be268 | 2386 | estimate_bb_frequencies (); |
6de9cd9a | 2387 | free_dominance_info (CDI_POST_DOMINATORS); |
6809cbf9 | 2388 | remove_fake_exit_edges (); |
8e88f9fd SP |
2389 | } |
2390 | ||
2391 | /* Predict branch probabilities and estimate profile of the tree CFG. | |
2392 | This is the driver function for PASS_PROFILE. */ | |
2393 | ||
2394 | static unsigned int | |
2395 | tree_estimate_probability_driver (void) | |
2396 | { | |
2397 | unsigned nb_loops; | |
2398 | ||
aa36f90b | 2399 | loop_optimizer_init (LOOPS_NORMAL); |
8e88f9fd SP |
2400 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2401 | flow_loops_dump (dump_file, NULL, 0); | |
2402 | ||
2403 | mark_irreducible_loops (); | |
2404 | ||
0fc822d0 | 2405 | nb_loops = number_of_loops (cfun); |
8e88f9fd SP |
2406 | if (nb_loops > 1) |
2407 | scev_initialize (); | |
2408 | ||
2409 | tree_estimate_probability (); | |
2410 | ||
2411 | if (nb_loops > 1) | |
2412 | scev_finalize (); | |
2413 | ||
598ec7bd | 2414 | loop_optimizer_finalize (); |
6de9cd9a | 2415 | if (dump_file && (dump_flags & TDF_DETAILS)) |
726a989a | 2416 | gimple_dump_cfg (dump_file, dump_flags); |
878f99d2 JH |
2417 | if (profile_status == PROFILE_ABSENT) |
2418 | profile_status = PROFILE_GUESSED; | |
c2924966 | 2419 | return 0; |
f1ebdfc5 | 2420 | } |
994a57cd | 2421 | \f |
fa10beec | 2422 | /* Predict edges to successors of CUR whose sources are not postdominated by |
3e4b9ad0 | 2423 | BB by PRED and recurse to all postdominators. */ |
bb033fd8 JH |
2424 | |
2425 | static void | |
3e4b9ad0 JH |
2426 | predict_paths_for_bb (basic_block cur, basic_block bb, |
2427 | enum br_predictor pred, | |
0f3b7e9a JH |
2428 | enum prediction taken, |
2429 | bitmap visited) | |
bb033fd8 JH |
2430 | { |
2431 | edge e; | |
628f6a4e | 2432 | edge_iterator ei; |
3e4b9ad0 | 2433 | basic_block son; |
bb033fd8 | 2434 | |
3e4b9ad0 JH |
2435 | /* We are looking for all edges forming edge cut induced by |
2436 | set of all blocks postdominated by BB. */ | |
2437 | FOR_EACH_EDGE (e, ei, cur->preds) | |
2438 | if (e->src->index >= NUM_FIXED_BLOCKS | |
2439 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb)) | |
bb033fd8 | 2440 | { |
450997ef JH |
2441 | edge e2; |
2442 | edge_iterator ei2; | |
2443 | bool found = false; | |
2444 | ||
5210bbc5 JH |
2445 | /* Ignore fake edges and eh, we predict them as not taken anyway. */ |
2446 | if (e->flags & (EDGE_EH | EDGE_FAKE)) | |
450997ef | 2447 | continue; |
3e4b9ad0 | 2448 | gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb)); |
450997ef | 2449 | |
0f3b7e9a | 2450 | /* See if there is an edge from e->src that is not abnormal |
450997ef JH |
2451 | and does not lead to BB. */ |
2452 | FOR_EACH_EDGE (e2, ei2, e->src->succs) | |
2453 | if (e2 != e | |
5210bbc5 | 2454 | && !(e2->flags & (EDGE_EH | EDGE_FAKE)) |
450997ef JH |
2455 | && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)) |
2456 | { | |
2457 | found = true; | |
2458 | break; | |
2459 | } | |
2460 | ||
2461 | /* If there is non-abnormal path leaving e->src, predict edge | |
2462 | using predictor. Otherwise we need to look for paths | |
0f3b7e9a JH |
2463 | leading to e->src. |
2464 | ||
2465 | The second may lead to infinite loop in the case we are predicitng | |
2466 | regions that are only reachable by abnormal edges. We simply | |
2467 | prevent visiting given BB twice. */ | |
450997ef JH |
2468 | if (found) |
2469 | predict_edge_def (e, pred, taken); | |
993716bd | 2470 | else if (bitmap_set_bit (visited, e->src->index)) |
0f3b7e9a | 2471 | predict_paths_for_bb (e->src, e->src, pred, taken, visited); |
bb033fd8 | 2472 | } |
3e4b9ad0 JH |
2473 | for (son = first_dom_son (CDI_POST_DOMINATORS, cur); |
2474 | son; | |
2475 | son = next_dom_son (CDI_POST_DOMINATORS, son)) | |
0f3b7e9a | 2476 | predict_paths_for_bb (son, bb, pred, taken, visited); |
3e4b9ad0 | 2477 | } |
bb033fd8 | 2478 | |
3e4b9ad0 JH |
2479 | /* Sets branch probabilities according to PREDiction and |
2480 | FLAGS. */ | |
bb033fd8 | 2481 | |
3e4b9ad0 JH |
2482 | static void |
2483 | predict_paths_leading_to (basic_block bb, enum br_predictor pred, | |
2484 | enum prediction taken) | |
2485 | { | |
0f3b7e9a JH |
2486 | bitmap visited = BITMAP_ALLOC (NULL); |
2487 | predict_paths_for_bb (bb, bb, pred, taken, visited); | |
2488 | BITMAP_FREE (visited); | |
bb033fd8 | 2489 | } |
5210bbc5 JH |
2490 | |
2491 | /* Like predict_paths_leading_to but take edge instead of basic block. */ | |
2492 | ||
2493 | static void | |
2494 | predict_paths_leading_to_edge (edge e, enum br_predictor pred, | |
2495 | enum prediction taken) | |
2496 | { | |
2497 | bool has_nonloop_edge = false; | |
2498 | edge_iterator ei; | |
2499 | edge e2; | |
2500 | ||
2501 | basic_block bb = e->src; | |
2502 | FOR_EACH_EDGE (e2, ei, bb->succs) | |
2503 | if (e2->dest != e->src && e2->dest != e->dest | |
2504 | && !(e->flags & (EDGE_EH | EDGE_FAKE)) | |
2505 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest)) | |
2506 | { | |
2507 | has_nonloop_edge = true; | |
2508 | break; | |
2509 | } | |
2510 | if (!has_nonloop_edge) | |
0f3b7e9a JH |
2511 | { |
2512 | bitmap visited = BITMAP_ALLOC (NULL); | |
2513 | predict_paths_for_bb (bb, bb, pred, taken, visited); | |
2514 | BITMAP_FREE (visited); | |
2515 | } | |
5210bbc5 JH |
2516 | else |
2517 | predict_edge_def (e, pred, taken); | |
2518 | } | |
969d70ca | 2519 | \f |
57cb6d52 | 2520 | /* This is used to carry information about basic blocks. It is |
861f9cd0 JH |
2521 | attached to the AUX field of the standard CFG block. */ |
2522 | ||
2523 | typedef struct block_info_def | |
2524 | { | |
2525 | /* Estimated frequency of execution of basic_block. */ | |
ac5e69da | 2526 | sreal frequency; |
861f9cd0 JH |
2527 | |
2528 | /* To keep queue of basic blocks to process. */ | |
2529 | basic_block next; | |
2530 | ||
eaec9b3d | 2531 | /* Number of predecessors we need to visit first. */ |
754d9299 | 2532 | int npredecessors; |
861f9cd0 JH |
2533 | } *block_info; |
2534 | ||
2535 | /* Similar information for edges. */ | |
2536 | typedef struct edge_info_def | |
2537 | { | |
569b7f6a | 2538 | /* In case edge is a loopback edge, the probability edge will be reached |
861f9cd0 | 2539 | in case header is. Estimated number of iterations of the loop can be |
8aa18a7d | 2540 | then computed as 1 / (1 - back_edge_prob). */ |
ac5e69da | 2541 | sreal back_edge_prob; |
569b7f6a | 2542 | /* True if the edge is a loopback edge in the natural loop. */ |
2c45a16a | 2543 | unsigned int back_edge:1; |
861f9cd0 JH |
2544 | } *edge_info; |
2545 | ||
2546 | #define BLOCK_INFO(B) ((block_info) (B)->aux) | |
2547 | #define EDGE_INFO(E) ((edge_info) (E)->aux) | |
2548 | ||
2549 | /* Helper function for estimate_bb_frequencies. | |
598ec7bd ZD |
2550 | Propagate the frequencies in blocks marked in |
2551 | TOVISIT, starting in HEAD. */ | |
bfdade77 | 2552 | |
861f9cd0 | 2553 | static void |
598ec7bd | 2554 | propagate_freq (basic_block head, bitmap tovisit) |
861f9cd0 | 2555 | { |
e0082a72 ZD |
2556 | basic_block bb; |
2557 | basic_block last; | |
b9af0016 | 2558 | unsigned i; |
861f9cd0 JH |
2559 | edge e; |
2560 | basic_block nextbb; | |
8a998e0c | 2561 | bitmap_iterator bi; |
247a370b | 2562 | |
eaec9b3d | 2563 | /* For each basic block we need to visit count number of his predecessors |
247a370b | 2564 | we need to visit first. */ |
8a998e0c | 2565 | EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi) |
247a370b | 2566 | { |
8a998e0c JL |
2567 | edge_iterator ei; |
2568 | int count = 0; | |
2569 | ||
24bd1a0b | 2570 | bb = BASIC_BLOCK (i); |
bfdade77 | 2571 | |
8a998e0c JL |
2572 | FOR_EACH_EDGE (e, ei, bb->preds) |
2573 | { | |
2574 | bool visit = bitmap_bit_p (tovisit, e->src->index); | |
2575 | ||
2576 | if (visit && !(e->flags & EDGE_DFS_BACK)) | |
2577 | count++; | |
2578 | else if (visit && dump_file && !EDGE_INFO (e)->back_edge) | |
2579 | fprintf (dump_file, | |
2580 | "Irreducible region hit, ignoring edge to %i->%i\n", | |
2581 | e->src->index, bb->index); | |
247a370b | 2582 | } |
b9af0016 | 2583 | BLOCK_INFO (bb)->npredecessors = count; |
b35366ce JH |
2584 | /* When function never returns, we will never process exit block. */ |
2585 | if (!count && bb == EXIT_BLOCK_PTR) | |
2586 | bb->count = bb->frequency = 0; | |
247a370b | 2587 | } |
861f9cd0 | 2588 | |
8aa18a7d | 2589 | memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one)); |
e0082a72 ZD |
2590 | last = head; |
2591 | for (bb = head; bb; bb = nextbb) | |
861f9cd0 | 2592 | { |
628f6a4e | 2593 | edge_iterator ei; |
ac5e69da | 2594 | sreal cyclic_probability, frequency; |
8aa18a7d JH |
2595 | |
2596 | memcpy (&cyclic_probability, &real_zero, sizeof (real_zero)); | |
2597 | memcpy (&frequency, &real_zero, sizeof (real_zero)); | |
861f9cd0 JH |
2598 | |
2599 | nextbb = BLOCK_INFO (bb)->next; | |
2600 | BLOCK_INFO (bb)->next = NULL; | |
2601 | ||
2602 | /* Compute frequency of basic block. */ | |
2603 | if (bb != head) | |
2604 | { | |
247a370b | 2605 | #ifdef ENABLE_CHECKING |
628f6a4e | 2606 | FOR_EACH_EDGE (e, ei, bb->preds) |
e16acfcd NS |
2607 | gcc_assert (!bitmap_bit_p (tovisit, e->src->index) |
2608 | || (e->flags & EDGE_DFS_BACK)); | |
247a370b | 2609 | #endif |
861f9cd0 | 2610 | |
628f6a4e | 2611 | FOR_EACH_EDGE (e, ei, bb->preds) |
861f9cd0 | 2612 | if (EDGE_INFO (e)->back_edge) |
8aa18a7d | 2613 | { |
ac5e69da JZ |
2614 | sreal_add (&cyclic_probability, &cyclic_probability, |
2615 | &EDGE_INFO (e)->back_edge_prob); | |
8aa18a7d | 2616 | } |
247a370b | 2617 | else if (!(e->flags & EDGE_DFS_BACK)) |
8aa18a7d | 2618 | { |
ac5e69da | 2619 | sreal tmp; |
8aa18a7d JH |
2620 | |
2621 | /* frequency += (e->probability | |
2622 | * BLOCK_INFO (e->src)->frequency / | |
2623 | REG_BR_PROB_BASE); */ | |
2624 | ||
ac5e69da JZ |
2625 | sreal_init (&tmp, e->probability, 0); |
2626 | sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency); | |
2627 | sreal_mul (&tmp, &tmp, &real_inv_br_prob_base); | |
2628 | sreal_add (&frequency, &frequency, &tmp); | |
8aa18a7d JH |
2629 | } |
2630 | ||
ac5e69da JZ |
2631 | if (sreal_compare (&cyclic_probability, &real_zero) == 0) |
2632 | { | |
2633 | memcpy (&BLOCK_INFO (bb)->frequency, &frequency, | |
2634 | sizeof (frequency)); | |
2635 | } | |
fbe3b30b SB |
2636 | else |
2637 | { | |
ac5e69da JZ |
2638 | if (sreal_compare (&cyclic_probability, &real_almost_one) > 0) |
2639 | { | |
2640 | memcpy (&cyclic_probability, &real_almost_one, | |
2641 | sizeof (real_almost_one)); | |
2642 | } | |
861f9cd0 | 2643 | |
79a490a9 | 2644 | /* BLOCK_INFO (bb)->frequency = frequency |
ac5e69da | 2645 | / (1 - cyclic_probability) */ |
861f9cd0 | 2646 | |
ac5e69da JZ |
2647 | sreal_sub (&cyclic_probability, &real_one, &cyclic_probability); |
2648 | sreal_div (&BLOCK_INFO (bb)->frequency, | |
2649 | &frequency, &cyclic_probability); | |
fbe3b30b | 2650 | } |
861f9cd0 JH |
2651 | } |
2652 | ||
8a998e0c | 2653 | bitmap_clear_bit (tovisit, bb->index); |
861f9cd0 | 2654 | |
9ff3d2de JL |
2655 | e = find_edge (bb, head); |
2656 | if (e) | |
2657 | { | |
2658 | sreal tmp; | |
b8698a0f | 2659 | |
9ff3d2de JL |
2660 | /* EDGE_INFO (e)->back_edge_prob |
2661 | = ((e->probability * BLOCK_INFO (bb)->frequency) | |
2662 | / REG_BR_PROB_BASE); */ | |
b8698a0f | 2663 | |
9ff3d2de JL |
2664 | sreal_init (&tmp, e->probability, 0); |
2665 | sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency); | |
2666 | sreal_mul (&EDGE_INFO (e)->back_edge_prob, | |
2667 | &tmp, &real_inv_br_prob_base); | |
2668 | } | |
861f9cd0 | 2669 | |
57cb6d52 | 2670 | /* Propagate to successor blocks. */ |
628f6a4e | 2671 | FOR_EACH_EDGE (e, ei, bb->succs) |
247a370b | 2672 | if (!(e->flags & EDGE_DFS_BACK) |
754d9299 | 2673 | && BLOCK_INFO (e->dest)->npredecessors) |
861f9cd0 | 2674 | { |
754d9299 JM |
2675 | BLOCK_INFO (e->dest)->npredecessors--; |
2676 | if (!BLOCK_INFO (e->dest)->npredecessors) | |
247a370b JH |
2677 | { |
2678 | if (!nextbb) | |
2679 | nextbb = e->dest; | |
2680 | else | |
2681 | BLOCK_INFO (last)->next = e->dest; | |
b8698a0f | 2682 | |
247a370b JH |
2683 | last = e->dest; |
2684 | } | |
628f6a4e | 2685 | } |
861f9cd0 JH |
2686 | } |
2687 | } | |
2688 | ||
57cb6d52 | 2689 | /* Estimate probabilities of loopback edges in loops at same nest level. */ |
bfdade77 | 2690 | |
861f9cd0 | 2691 | static void |
598ec7bd | 2692 | estimate_loops_at_level (struct loop *first_loop) |
861f9cd0 | 2693 | { |
2ecfd709 | 2694 | struct loop *loop; |
861f9cd0 JH |
2695 | |
2696 | for (loop = first_loop; loop; loop = loop->next) | |
2697 | { | |
861f9cd0 | 2698 | edge e; |
2ecfd709 | 2699 | basic_block *bbs; |
3d436d2a | 2700 | unsigned i; |
598ec7bd | 2701 | bitmap tovisit = BITMAP_ALLOC (NULL); |
861f9cd0 | 2702 | |
598ec7bd | 2703 | estimate_loops_at_level (loop->inner); |
79a490a9 | 2704 | |
598ec7bd ZD |
2705 | /* Find current loop back edge and mark it. */ |
2706 | e = loop_latch_edge (loop); | |
2707 | EDGE_INFO (e)->back_edge = 1; | |
2ecfd709 ZD |
2708 | |
2709 | bbs = get_loop_body (loop); | |
2710 | for (i = 0; i < loop->num_nodes; i++) | |
8a998e0c | 2711 | bitmap_set_bit (tovisit, bbs[i]->index); |
2ecfd709 | 2712 | free (bbs); |
598ec7bd ZD |
2713 | propagate_freq (loop->header, tovisit); |
2714 | BITMAP_FREE (tovisit); | |
861f9cd0 JH |
2715 | } |
2716 | } | |
2717 | ||
2f8e468b | 2718 | /* Propagates frequencies through structure of loops. */ |
598ec7bd ZD |
2719 | |
2720 | static void | |
d73be268 | 2721 | estimate_loops (void) |
598ec7bd ZD |
2722 | { |
2723 | bitmap tovisit = BITMAP_ALLOC (NULL); | |
2724 | basic_block bb; | |
2725 | ||
2726 | /* Start by estimating the frequencies in the loops. */ | |
0fc822d0 | 2727 | if (number_of_loops (cfun) > 1) |
d73be268 | 2728 | estimate_loops_at_level (current_loops->tree_root->inner); |
598ec7bd ZD |
2729 | |
2730 | /* Now propagate the frequencies through all the blocks. */ | |
2731 | FOR_ALL_BB (bb) | |
2732 | { | |
2733 | bitmap_set_bit (tovisit, bb->index); | |
2734 | } | |
2735 | propagate_freq (ENTRY_BLOCK_PTR, tovisit); | |
2736 | BITMAP_FREE (tovisit); | |
2737 | } | |
2738 | ||
02307675 R |
2739 | /* Convert counts measured by profile driven feedback to frequencies. |
2740 | Return nonzero iff there was any nonzero execution count. */ | |
bfdade77 | 2741 | |
bbd236a1 | 2742 | int |
79a490a9 | 2743 | counts_to_freqs (void) |
861f9cd0 | 2744 | { |
02307675 | 2745 | gcov_type count_max, true_count_max = 0; |
e0082a72 | 2746 | basic_block bb; |
0b17ab2f | 2747 | |
43558bcc | 2748 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
02307675 | 2749 | true_count_max = MAX (bb->count, true_count_max); |
861f9cd0 | 2750 | |
02307675 | 2751 | count_max = MAX (true_count_max, 1); |
e0082a72 ZD |
2752 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
2753 | bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max; | |
6bad2617 | 2754 | |
02307675 | 2755 | return true_count_max; |
861f9cd0 JH |
2756 | } |
2757 | ||
bfdade77 RK |
2758 | /* Return true if function is likely to be expensive, so there is no point to |
2759 | optimize performance of prologue, epilogue or do inlining at the expense | |
d55d8fc7 | 2760 | of code size growth. THRESHOLD is the limit of number of instructions |
bfdade77 RK |
2761 | function can execute at average to be still considered not expensive. */ |
2762 | ||
6ab16dd9 | 2763 | bool |
79a490a9 | 2764 | expensive_function_p (int threshold) |
6ab16dd9 JH |
2765 | { |
2766 | unsigned int sum = 0; | |
e0082a72 | 2767 | basic_block bb; |
5197bd50 | 2768 | unsigned int limit; |
6ab16dd9 JH |
2769 | |
2770 | /* We can not compute accurately for large thresholds due to scaled | |
2771 | frequencies. */ | |
e16acfcd | 2772 | gcc_assert (threshold <= BB_FREQ_MAX); |
6ab16dd9 | 2773 | |
eaec9b3d | 2774 | /* Frequencies are out of range. This either means that function contains |
6ab16dd9 JH |
2775 | internal loop executing more than BB_FREQ_MAX times or profile feedback |
2776 | is available and function has not been executed at all. */ | |
2777 | if (ENTRY_BLOCK_PTR->frequency == 0) | |
2778 | return true; | |
6a4d6760 | 2779 | |
6ab16dd9 JH |
2780 | /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */ |
2781 | limit = ENTRY_BLOCK_PTR->frequency * threshold; | |
e0082a72 | 2782 | FOR_EACH_BB (bb) |
6ab16dd9 | 2783 | { |
6ab16dd9 JH |
2784 | rtx insn; |
2785 | ||
39718607 | 2786 | FOR_BB_INSNS (bb, insn) |
bfdade77 RK |
2787 | if (active_insn_p (insn)) |
2788 | { | |
2789 | sum += bb->frequency; | |
2790 | if (sum > limit) | |
2791 | return true; | |
6ab16dd9 JH |
2792 | } |
2793 | } | |
bfdade77 | 2794 | |
6ab16dd9 JH |
2795 | return false; |
2796 | } | |
2797 | ||
861f9cd0 | 2798 | /* Estimate basic blocks frequency by given branch probabilities. */ |
bfdade77 | 2799 | |
45a80bb9 | 2800 | void |
d73be268 | 2801 | estimate_bb_frequencies (void) |
861f9cd0 | 2802 | { |
e0082a72 | 2803 | basic_block bb; |
ac5e69da | 2804 | sreal freq_max; |
8aa18a7d | 2805 | |
0e7f7d87 | 2806 | if (profile_status != PROFILE_READ || !counts_to_freqs ()) |
194734e9 | 2807 | { |
c4f6b78e RE |
2808 | static int real_values_initialized = 0; |
2809 | ||
2810 | if (!real_values_initialized) | |
2811 | { | |
85bb9c2a | 2812 | real_values_initialized = 1; |
c4f6b78e RE |
2813 | sreal_init (&real_zero, 0, 0); |
2814 | sreal_init (&real_one, 1, 0); | |
2815 | sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0); | |
2816 | sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0); | |
2817 | sreal_init (&real_one_half, 1, -1); | |
2818 | sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base); | |
2819 | sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base); | |
2820 | } | |
861f9cd0 | 2821 | |
194734e9 | 2822 | mark_dfs_back_edges (); |
194734e9 | 2823 | |
c5cbcccf | 2824 | single_succ_edge (ENTRY_BLOCK_PTR)->probability = REG_BR_PROB_BASE; |
194734e9 JH |
2825 | |
2826 | /* Set up block info for each basic block. */ | |
2827 | alloc_aux_for_blocks (sizeof (struct block_info_def)); | |
2828 | alloc_aux_for_edges (sizeof (struct edge_info_def)); | |
e0082a72 | 2829 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
861f9cd0 | 2830 | { |
861f9cd0 | 2831 | edge e; |
628f6a4e | 2832 | edge_iterator ei; |
194734e9 | 2833 | |
628f6a4e | 2834 | FOR_EACH_EDGE (e, ei, bb->succs) |
861f9cd0 | 2835 | { |
ac5e69da JZ |
2836 | sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0); |
2837 | sreal_mul (&EDGE_INFO (e)->back_edge_prob, | |
2838 | &EDGE_INFO (e)->back_edge_prob, | |
2839 | &real_inv_br_prob_base); | |
861f9cd0 | 2840 | } |
861f9cd0 | 2841 | } |
bfdade77 | 2842 | |
194734e9 JH |
2843 | /* First compute probabilities locally for each loop from innermost |
2844 | to outermost to examine probabilities for back edges. */ | |
d73be268 | 2845 | estimate_loops (); |
861f9cd0 | 2846 | |
194734e9 | 2847 | memcpy (&freq_max, &real_zero, sizeof (real_zero)); |
e0082a72 | 2848 | FOR_EACH_BB (bb) |
ac5e69da JZ |
2849 | if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0) |
2850 | memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max)); | |
fbe3b30b | 2851 | |
ac5e69da | 2852 | sreal_div (&freq_max, &real_bb_freq_max, &freq_max); |
e0082a72 | 2853 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
8aa18a7d | 2854 | { |
ac5e69da | 2855 | sreal tmp; |
bfdade77 | 2856 | |
ac5e69da JZ |
2857 | sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max); |
2858 | sreal_add (&tmp, &tmp, &real_one_half); | |
2859 | bb->frequency = sreal_to_int (&tmp); | |
194734e9 | 2860 | } |
bfdade77 | 2861 | |
194734e9 JH |
2862 | free_aux_for_blocks (); |
2863 | free_aux_for_edges (); | |
2864 | } | |
2865 | compute_function_frequency (); | |
194734e9 | 2866 | } |
861f9cd0 | 2867 | |
194734e9 | 2868 | /* Decide whether function is hot, cold or unlikely executed. */ |
965b98d0 | 2869 | void |
79a490a9 | 2870 | compute_function_frequency (void) |
194734e9 | 2871 | { |
e0082a72 | 2872 | basic_block bb; |
581985d7 | 2873 | struct cgraph_node *node = cgraph_get_node (current_function_decl); |
daf5c770 | 2874 | |
844db5d0 JH |
2875 | if (DECL_STATIC_CONSTRUCTOR (current_function_decl) |
2876 | || MAIN_NAME_P (DECL_NAME (current_function_decl))) | |
2877 | node->only_called_at_startup = true; | |
2878 | if (DECL_STATIC_DESTRUCTOR (current_function_decl)) | |
2879 | node->only_called_at_exit = true; | |
e0082a72 | 2880 | |
daf5c770 | 2881 | if (profile_status != PROFILE_READ) |
52bf96d2 | 2882 | { |
5fefcf92 | 2883 | int flags = flags_from_decl_or_type (current_function_decl); |
52bf96d2 JH |
2884 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)) |
2885 | != NULL) | |
5fefcf92 | 2886 | node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED; |
52bf96d2 JH |
2887 | else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl)) |
2888 | != NULL) | |
5fefcf92 JH |
2889 | node->frequency = NODE_FREQUENCY_HOT; |
2890 | else if (flags & ECF_NORETURN) | |
2891 | node->frequency = NODE_FREQUENCY_EXECUTED_ONCE; | |
2892 | else if (MAIN_NAME_P (DECL_NAME (current_function_decl))) | |
2893 | node->frequency = NODE_FREQUENCY_EXECUTED_ONCE; | |
2894 | else if (DECL_STATIC_CONSTRUCTOR (current_function_decl) | |
2895 | || DECL_STATIC_DESTRUCTOR (current_function_decl)) | |
2896 | node->frequency = NODE_FREQUENCY_EXECUTED_ONCE; | |
52bf96d2 JH |
2897 | return; |
2898 | } | |
daf5c770 JH |
2899 | |
2900 | /* Only first time try to drop function into unlikely executed. | |
2901 | After inlining the roundoff errors may confuse us. | |
2902 | Ipa-profile pass will drop functions only called from unlikely | |
2903 | functions to unlikely and that is most of what we care about. */ | |
2904 | if (!cfun->after_inlining) | |
2905 | node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED; | |
e0082a72 | 2906 | FOR_EACH_BB (bb) |
861f9cd0 | 2907 | { |
2eb712b4 | 2908 | if (maybe_hot_bb_p (cfun, bb)) |
194734e9 | 2909 | { |
5fefcf92 | 2910 | node->frequency = NODE_FREQUENCY_HOT; |
194734e9 JH |
2911 | return; |
2912 | } | |
2eb712b4 | 2913 | if (!probably_never_executed_bb_p (cfun, bb)) |
5fefcf92 | 2914 | node->frequency = NODE_FREQUENCY_NORMAL; |
861f9cd0 | 2915 | } |
194734e9 | 2916 | } |
861f9cd0 | 2917 | |
a00d11f0 JH |
2918 | static bool |
2919 | gate_estimate_probability (void) | |
2920 | { | |
2921 | return flag_guess_branch_prob; | |
2922 | } | |
6de9cd9a | 2923 | |
2e28e797 JH |
2924 | /* Build PREDICT_EXPR. */ |
2925 | tree | |
2926 | build_predict_expr (enum br_predictor predictor, enum prediction taken) | |
2927 | { | |
9d7e5c4d | 2928 | tree t = build1 (PREDICT_EXPR, void_type_node, |
9f616812 | 2929 | build_int_cst (integer_type_node, predictor)); |
bbbbb16a | 2930 | SET_PREDICT_EXPR_OUTCOME (t, taken); |
2e28e797 JH |
2931 | return t; |
2932 | } | |
2933 | ||
2934 | const char * | |
2935 | predictor_name (enum br_predictor predictor) | |
2936 | { | |
2937 | return predictor_info[predictor].name; | |
2938 | } | |
2939 | ||
27a4cd48 DM |
2940 | namespace { |
2941 | ||
2942 | const pass_data pass_data_profile = | |
2943 | { | |
2944 | GIMPLE_PASS, /* type */ | |
2945 | "profile_estimate", /* name */ | |
2946 | OPTGROUP_NONE, /* optinfo_flags */ | |
2947 | true, /* has_gate */ | |
2948 | true, /* has_execute */ | |
2949 | TV_BRANCH_PROB, /* tv_id */ | |
2950 | PROP_cfg, /* properties_required */ | |
2951 | 0, /* properties_provided */ | |
2952 | 0, /* properties_destroyed */ | |
2953 | 0, /* todo_flags_start */ | |
2954 | TODO_verify_ssa, /* todo_flags_finish */ | |
6de9cd9a | 2955 | }; |
7299cb99 | 2956 | |
27a4cd48 DM |
2957 | class pass_profile : public gimple_opt_pass |
2958 | { | |
2959 | public: | |
2960 | pass_profile(gcc::context *ctxt) | |
2961 | : gimple_opt_pass(pass_data_profile, ctxt) | |
2962 | {} | |
2963 | ||
2964 | /* opt_pass methods: */ | |
2965 | bool gate () { return gate_estimate_probability (); } | |
2966 | unsigned int execute () { return tree_estimate_probability_driver (); } | |
2967 | ||
2968 | }; // class pass_profile | |
2969 | ||
2970 | } // anon namespace | |
2971 | ||
2972 | gimple_opt_pass * | |
2973 | make_pass_profile (gcc::context *ctxt) | |
2974 | { | |
2975 | return new pass_profile (ctxt); | |
2976 | } | |
2977 | ||
2978 | namespace { | |
2979 | ||
2980 | const pass_data pass_data_strip_predict_hints = | |
2981 | { | |
2982 | GIMPLE_PASS, /* type */ | |
2983 | "*strip_predict_hints", /* name */ | |
2984 | OPTGROUP_NONE, /* optinfo_flags */ | |
2985 | false, /* has_gate */ | |
2986 | true, /* has_execute */ | |
2987 | TV_BRANCH_PROB, /* tv_id */ | |
2988 | PROP_cfg, /* properties_required */ | |
2989 | 0, /* properties_provided */ | |
2990 | 0, /* properties_destroyed */ | |
2991 | 0, /* todo_flags_start */ | |
2992 | TODO_verify_ssa, /* todo_flags_finish */ | |
7299cb99 | 2993 | }; |
b35366ce | 2994 | |
27a4cd48 DM |
2995 | class pass_strip_predict_hints : public gimple_opt_pass |
2996 | { | |
2997 | public: | |
2998 | pass_strip_predict_hints(gcc::context *ctxt) | |
2999 | : gimple_opt_pass(pass_data_strip_predict_hints, ctxt) | |
3000 | {} | |
3001 | ||
3002 | /* opt_pass methods: */ | |
3003 | opt_pass * clone () { return new pass_strip_predict_hints (ctxt_); } | |
3004 | unsigned int execute () { return strip_predict_hints (); } | |
3005 | ||
3006 | }; // class pass_strip_predict_hints | |
3007 | ||
3008 | } // anon namespace | |
3009 | ||
3010 | gimple_opt_pass * | |
3011 | make_pass_strip_predict_hints (gcc::context *ctxt) | |
3012 | { | |
3013 | return new pass_strip_predict_hints (ctxt); | |
3014 | } | |
3015 | ||
b35366ce JH |
3016 | /* Rebuild function frequencies. Passes are in general expected to |
3017 | maintain profile by hand, however in some cases this is not possible: | |
3018 | for example when inlining several functions with loops freuqencies might run | |
3019 | out of scale and thus needs to be recomputed. */ | |
3020 | ||
3021 | void | |
3022 | rebuild_frequencies (void) | |
3023 | { | |
a222c01a | 3024 | timevar_push (TV_REBUILD_FREQUENCIES); |
b35366ce JH |
3025 | if (profile_status == PROFILE_GUESSED) |
3026 | { | |
3027 | loop_optimizer_init (0); | |
3028 | add_noreturn_fake_exit_edges (); | |
3029 | mark_irreducible_loops (); | |
3030 | connect_infinite_loops_to_exit (); | |
3031 | estimate_bb_frequencies (); | |
3032 | remove_fake_exit_edges (); | |
3033 | loop_optimizer_finalize (); | |
3034 | } | |
3035 | else if (profile_status == PROFILE_READ) | |
3036 | counts_to_freqs (); | |
3037 | else | |
3038 | gcc_unreachable (); | |
a222c01a | 3039 | timevar_pop (TV_REBUILD_FREQUENCIES); |
b35366ce | 3040 | } |