]>
Commit | Line | Data |
---|---|---|
f1ebdfc5 | 1 | /* Branch prediction routines for the GNU compiler. |
fa10beec | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008 |
e42febca | 3 | Free Software Foundation, Inc. |
f1ebdfc5 | 4 | |
bfdade77 | 5 | This file is part of GCC. |
f1ebdfc5 | 6 | |
bfdade77 RK |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
bfdade77 | 10 | version. |
f1ebdfc5 | 11 | |
bfdade77 RK |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
f1ebdfc5 | 16 | |
bfdade77 | 17 | You should have received a copy of the GNU General Public License |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
f1ebdfc5 JE |
20 | |
21 | /* References: | |
22 | ||
23 | [1] "Branch Prediction for Free" | |
24 | Ball and Larus; PLDI '93. | |
25 | [2] "Static Branch Frequency and Program Profile Analysis" | |
26 | Wu and Larus; MICRO-27. | |
27 | [3] "Corpus-based Static Branch Prediction" | |
3ef42a0c | 28 | Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */ |
f1ebdfc5 JE |
29 | |
30 | ||
31 | #include "config.h" | |
32 | #include "system.h" | |
4977bab6 ZW |
33 | #include "coretypes.h" |
34 | #include "tm.h" | |
f1ebdfc5 JE |
35 | #include "tree.h" |
36 | #include "rtl.h" | |
37 | #include "tm_p.h" | |
efc9bd41 | 38 | #include "hard-reg-set.h" |
f1ebdfc5 JE |
39 | #include "basic-block.h" |
40 | #include "insn-config.h" | |
41 | #include "regs.h" | |
f1ebdfc5 JE |
42 | #include "flags.h" |
43 | #include "output.h" | |
44 | #include "function.h" | |
45 | #include "except.h" | |
46 | #include "toplev.h" | |
47 | #include "recog.h" | |
f1ebdfc5 | 48 | #include "expr.h" |
4db384c9 | 49 | #include "predict.h" |
d79f9ec9 | 50 | #include "coverage.h" |
ac5e69da | 51 | #include "sreal.h" |
194734e9 JH |
52 | #include "params.h" |
53 | #include "target.h" | |
3d436d2a | 54 | #include "cfgloop.h" |
6de9cd9a DN |
55 | #include "tree-flow.h" |
56 | #include "ggc.h" | |
57 | #include "tree-dump.h" | |
58 | #include "tree-pass.h" | |
59 | #include "timevar.h" | |
b6acab32 JH |
60 | #include "tree-scalar-evolution.h" |
61 | #include "cfgloop.h" | |
f06b0a10 | 62 | #include "pointer-set.h" |
8aa18a7d | 63 | |
fbe3b30b SB |
64 | /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, |
65 | 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */ | |
ac5e69da JZ |
66 | static sreal real_zero, real_one, real_almost_one, real_br_prob_base, |
67 | real_inv_br_prob_base, real_one_half, real_bb_freq_max; | |
f1ebdfc5 | 68 | |
c66f079e | 69 | /* Random guesstimation given names. */ |
b0ad2de2 | 70 | #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 100 - 1) |
c66f079e | 71 | #define PROB_EVEN (REG_BR_PROB_BASE / 2) |
c66f079e RH |
72 | #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY) |
73 | #define PROB_ALWAYS (REG_BR_PROB_BASE) | |
f1ebdfc5 | 74 | |
79a490a9 | 75 | static void combine_predictions_for_insn (rtx, basic_block); |
6de9cd9a | 76 | static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int); |
3e4b9ad0 | 77 | static void predict_paths_leading_to (basic_block, enum br_predictor, enum prediction); |
79a490a9 AJ |
78 | static void compute_function_frequency (void); |
79 | static void choose_function_section (void); | |
ed7a4b4b | 80 | static bool can_predict_insn_p (const_rtx); |
ee92cb46 | 81 | |
4db384c9 JH |
82 | /* Information we hold about each branch predictor. |
83 | Filled using information from predict.def. */ | |
bfdade77 | 84 | |
4db384c9 | 85 | struct predictor_info |
ee92cb46 | 86 | { |
8b60264b KG |
87 | const char *const name; /* Name used in the debugging dumps. */ |
88 | const int hitrate; /* Expected hitrate used by | |
89 | predict_insn_def call. */ | |
90 | const int flags; | |
4db384c9 | 91 | }; |
ee92cb46 | 92 | |
134d3a2e JH |
93 | /* Use given predictor without Dempster-Shaffer theory if it matches |
94 | using first_match heuristics. */ | |
95 | #define PRED_FLAG_FIRST_MATCH 1 | |
96 | ||
97 | /* Recompute hitrate in percent to our representation. */ | |
98 | ||
bfdade77 | 99 | #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100) |
134d3a2e JH |
100 | |
101 | #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS}, | |
bfdade77 | 102 | static const struct predictor_info predictor_info[]= { |
4db384c9 JH |
103 | #include "predict.def" |
104 | ||
dc297297 | 105 | /* Upper bound on predictors. */ |
134d3a2e | 106 | {NULL, 0, 0} |
4db384c9 JH |
107 | }; |
108 | #undef DEF_PREDICTOR | |
194734e9 | 109 | |
3250d724 JH |
110 | /* Return TRUE if frequency FREQ is considered to be hot. */ |
111 | static bool | |
112 | maybe_hot_frequency_p (int freq) | |
113 | { | |
114 | if (!profile_info || !flag_branch_probabilities) | |
115 | { | |
116 | if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED) | |
117 | return false; | |
118 | if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT) | |
119 | return true; | |
120 | } | |
121 | if (freq < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)) | |
122 | return false; | |
123 | return true; | |
124 | } | |
125 | ||
194734e9 | 126 | /* Return true in case BB can be CPU intensive and should be optimized |
d55d8fc7 | 127 | for maximal performance. */ |
194734e9 JH |
128 | |
129 | bool | |
ed7a4b4b | 130 | maybe_hot_bb_p (const_basic_block bb) |
194734e9 | 131 | { |
cdb23767 | 132 | if (profile_info && flag_branch_probabilities |
194734e9 | 133 | && (bb->count |
cdb23767 | 134 | < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION))) |
194734e9 | 135 | return false; |
3250d724 JH |
136 | return maybe_hot_frequency_p (bb->frequency); |
137 | } | |
138 | ||
139 | /* Return true in case BB can be CPU intensive and should be optimized | |
140 | for maximal performance. */ | |
141 | ||
142 | bool | |
143 | maybe_hot_edge_p (edge e) | |
144 | { | |
145 | if (profile_info && flag_branch_probabilities | |
146 | && (e->count | |
147 | < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION))) | |
194734e9 | 148 | return false; |
3250d724 | 149 | return maybe_hot_frequency_p (EDGE_FREQUENCY (e)); |
194734e9 JH |
150 | } |
151 | ||
152 | /* Return true in case BB is cold and should be optimized for size. */ | |
153 | ||
154 | bool | |
ed7a4b4b | 155 | probably_cold_bb_p (const_basic_block bb) |
194734e9 | 156 | { |
cdb23767 | 157 | if (profile_info && flag_branch_probabilities |
194734e9 | 158 | && (bb->count |
cdb23767 | 159 | < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION))) |
194734e9 | 160 | return true; |
52bf96d2 JH |
161 | if ((!profile_info || !flag_branch_probabilities) |
162 | && cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED) | |
163 | return true; | |
194734e9 JH |
164 | if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)) |
165 | return true; | |
166 | return false; | |
167 | } | |
168 | ||
169 | /* Return true in case BB is probably never executed. */ | |
170 | bool | |
ed7a4b4b | 171 | probably_never_executed_bb_p (const_basic_block bb) |
194734e9 | 172 | { |
cdb23767 NS |
173 | if (profile_info && flag_branch_probabilities) |
174 | return ((bb->count + profile_info->runs / 2) / profile_info->runs) == 0; | |
52bf96d2 JH |
175 | if ((!profile_info || !flag_branch_probabilities) |
176 | && cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED) | |
177 | return true; | |
194734e9 JH |
178 | return false; |
179 | } | |
180 | ||
969d70ca JH |
181 | /* Return true if the one of outgoing edges is already predicted by |
182 | PREDICTOR. */ | |
183 | ||
6de9cd9a | 184 | bool |
9678086d | 185 | rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor) |
969d70ca JH |
186 | { |
187 | rtx note; | |
a813c111 | 188 | if (!INSN_P (BB_END (bb))) |
969d70ca | 189 | return false; |
a813c111 | 190 | for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1)) |
969d70ca JH |
191 | if (REG_NOTE_KIND (note) == REG_BR_PRED |
192 | && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor) | |
193 | return true; | |
194 | return false; | |
195 | } | |
ee92cb46 | 196 | |
f06b0a10 ZD |
197 | /* This map contains for a basic block the list of predictions for the |
198 | outgoing edges. */ | |
199 | ||
200 | static struct pointer_map_t *bb_predictions; | |
201 | ||
6de9cd9a DN |
202 | /* Return true if the one of outgoing edges is already predicted by |
203 | PREDICTOR. */ | |
204 | ||
205 | bool | |
726a989a | 206 | gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor) |
6de9cd9a | 207 | { |
4aab792d | 208 | struct edge_prediction *i; |
f06b0a10 ZD |
209 | void **preds = pointer_map_contains (bb_predictions, bb); |
210 | ||
211 | if (!preds) | |
212 | return false; | |
213 | ||
d3bfe4de | 214 | for (i = (struct edge_prediction *) *preds; i; i = i->ep_next) |
59ced947 | 215 | if (i->ep_predictor == predictor) |
6de9cd9a DN |
216 | return true; |
217 | return false; | |
218 | } | |
219 | ||
2c9e13f3 JH |
220 | /* Return true when the probability of edge is reliable. |
221 | ||
222 | The profile guessing code is good at predicting branch outcome (ie. | |
223 | taken/not taken), that is predicted right slightly over 75% of time. | |
86c33cd0 | 224 | It is however notoriously poor on predicting the probability itself. |
2c9e13f3 JH |
225 | In general the profile appear a lot flatter (with probabilities closer |
226 | to 50%) than the reality so it is bad idea to use it to drive optimization | |
227 | such as those disabling dynamic branch prediction for well predictable | |
228 | branches. | |
229 | ||
230 | There are two exceptions - edges leading to noreturn edges and edges | |
231 | predicted by number of iterations heuristics are predicted well. This macro | |
232 | should be able to distinguish those, but at the moment it simply check for | |
233 | noreturn heuristic that is only one giving probability over 99% or bellow | |
86c33cd0 | 234 | 1%. In future we might want to propagate reliability information across the |
2c9e13f3 JH |
235 | CFG if we find this information useful on multiple places. */ |
236 | static bool | |
237 | probability_reliable_p (int prob) | |
238 | { | |
239 | return (profile_status == PROFILE_READ | |
240 | || (profile_status == PROFILE_GUESSED | |
241 | && (prob <= HITRATE (1) || prob >= HITRATE (99)))); | |
242 | } | |
243 | ||
244 | /* Same predicate as above, working on edges. */ | |
245 | bool | |
ed7a4b4b | 246 | edge_probability_reliable_p (const_edge e) |
2c9e13f3 JH |
247 | { |
248 | return probability_reliable_p (e->probability); | |
249 | } | |
250 | ||
251 | /* Same predicate as edge_probability_reliable_p, working on notes. */ | |
252 | bool | |
ed7a4b4b | 253 | br_prob_note_reliable_p (const_rtx note) |
2c9e13f3 JH |
254 | { |
255 | gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB); | |
256 | return probability_reliable_p (INTVAL (XEXP (note, 0))); | |
257 | } | |
258 | ||
7d6d381a | 259 | static void |
79a490a9 | 260 | predict_insn (rtx insn, enum br_predictor predictor, int probability) |
4db384c9 | 261 | { |
e16acfcd | 262 | gcc_assert (any_condjump_p (insn)); |
d50672ef JH |
263 | if (!flag_guess_branch_prob) |
264 | return; | |
bfdade77 | 265 | |
65c5f2a6 ILT |
266 | add_reg_note (insn, REG_BR_PRED, |
267 | gen_rtx_CONCAT (VOIDmode, | |
268 | GEN_INT ((int) predictor), | |
269 | GEN_INT ((int) probability))); | |
4db384c9 JH |
270 | } |
271 | ||
272 | /* Predict insn by given predictor. */ | |
bfdade77 | 273 | |
4db384c9 | 274 | void |
79a490a9 AJ |
275 | predict_insn_def (rtx insn, enum br_predictor predictor, |
276 | enum prediction taken) | |
4db384c9 JH |
277 | { |
278 | int probability = predictor_info[(int) predictor].hitrate; | |
bfdade77 | 279 | |
4db384c9 JH |
280 | if (taken != TAKEN) |
281 | probability = REG_BR_PROB_BASE - probability; | |
bfdade77 | 282 | |
4db384c9 | 283 | predict_insn (insn, predictor, probability); |
ee92cb46 JH |
284 | } |
285 | ||
286 | /* Predict edge E with given probability if possible. */ | |
bfdade77 | 287 | |
4db384c9 | 288 | void |
6de9cd9a | 289 | rtl_predict_edge (edge e, enum br_predictor predictor, int probability) |
ee92cb46 JH |
290 | { |
291 | rtx last_insn; | |
a813c111 | 292 | last_insn = BB_END (e->src); |
ee92cb46 JH |
293 | |
294 | /* We can store the branch prediction information only about | |
295 | conditional jumps. */ | |
296 | if (!any_condjump_p (last_insn)) | |
297 | return; | |
298 | ||
299 | /* We always store probability of branching. */ | |
300 | if (e->flags & EDGE_FALLTHRU) | |
301 | probability = REG_BR_PROB_BASE - probability; | |
302 | ||
4db384c9 JH |
303 | predict_insn (last_insn, predictor, probability); |
304 | } | |
305 | ||
6de9cd9a DN |
306 | /* Predict edge E with the given PROBABILITY. */ |
307 | void | |
726a989a | 308 | gimple_predict_edge (edge e, enum br_predictor predictor, int probability) |
6de9cd9a | 309 | { |
a00d11f0 | 310 | gcc_assert (profile_status != PROFILE_GUESSED); |
e0342c26 | 311 | if ((e->src != ENTRY_BLOCK_PTR && EDGE_COUNT (e->src->succs) > 1) |
a00d11f0 | 312 | && flag_guess_branch_prob && optimize) |
e0342c26 | 313 | { |
f06b0a10 ZD |
314 | struct edge_prediction *i = XNEW (struct edge_prediction); |
315 | void **preds = pointer_map_insert (bb_predictions, e->src); | |
6de9cd9a | 316 | |
d3bfe4de | 317 | i->ep_next = (struct edge_prediction *) *preds; |
f06b0a10 | 318 | *preds = i; |
59ced947 RÁE |
319 | i->ep_probability = probability; |
320 | i->ep_predictor = predictor; | |
321 | i->ep_edge = e; | |
e0342c26 | 322 | } |
6de9cd9a DN |
323 | } |
324 | ||
3809e990 JH |
325 | /* Remove all predictions on given basic block that are attached |
326 | to edge E. */ | |
327 | void | |
328 | remove_predictions_associated_with_edge (edge e) | |
329 | { | |
f06b0a10 ZD |
330 | void **preds; |
331 | ||
332 | if (!bb_predictions) | |
333 | return; | |
334 | ||
335 | preds = pointer_map_contains (bb_predictions, e->src); | |
336 | ||
337 | if (preds) | |
3809e990 | 338 | { |
f06b0a10 ZD |
339 | struct edge_prediction **prediction = (struct edge_prediction **) preds; |
340 | struct edge_prediction *next; | |
341 | ||
3809e990 JH |
342 | while (*prediction) |
343 | { | |
59ced947 | 344 | if ((*prediction)->ep_edge == e) |
f06b0a10 ZD |
345 | { |
346 | next = (*prediction)->ep_next; | |
347 | free (*prediction); | |
348 | *prediction = next; | |
349 | } | |
3809e990 | 350 | else |
59ced947 | 351 | prediction = &((*prediction)->ep_next); |
3809e990 JH |
352 | } |
353 | } | |
354 | } | |
355 | ||
f06b0a10 ZD |
356 | /* Clears the list of predictions stored for BB. */ |
357 | ||
358 | static void | |
359 | clear_bb_predictions (basic_block bb) | |
360 | { | |
361 | void **preds = pointer_map_contains (bb_predictions, bb); | |
362 | struct edge_prediction *pred, *next; | |
363 | ||
364 | if (!preds) | |
365 | return; | |
366 | ||
d3bfe4de | 367 | for (pred = (struct edge_prediction *) *preds; pred; pred = next) |
f06b0a10 ZD |
368 | { |
369 | next = pred->ep_next; | |
370 | free (pred); | |
371 | } | |
372 | *preds = NULL; | |
373 | } | |
374 | ||
2ffa9932 JH |
375 | /* Return true when we can store prediction on insn INSN. |
376 | At the moment we represent predictions only on conditional | |
377 | jumps, not at computed jump or other complicated cases. */ | |
378 | static bool | |
ed7a4b4b | 379 | can_predict_insn_p (const_rtx insn) |
2ffa9932 | 380 | { |
4b4bf941 | 381 | return (JUMP_P (insn) |
2ffa9932 | 382 | && any_condjump_p (insn) |
628f6a4e | 383 | && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2); |
2ffa9932 JH |
384 | } |
385 | ||
4db384c9 | 386 | /* Predict edge E by given predictor if possible. */ |
bfdade77 | 387 | |
4db384c9 | 388 | void |
79a490a9 AJ |
389 | predict_edge_def (edge e, enum br_predictor predictor, |
390 | enum prediction taken) | |
4db384c9 JH |
391 | { |
392 | int probability = predictor_info[(int) predictor].hitrate; | |
393 | ||
394 | if (taken != TAKEN) | |
395 | probability = REG_BR_PROB_BASE - probability; | |
bfdade77 | 396 | |
4db384c9 JH |
397 | predict_edge (e, predictor, probability); |
398 | } | |
399 | ||
400 | /* Invert all branch predictions or probability notes in the INSN. This needs | |
401 | to be done each time we invert the condition used by the jump. */ | |
bfdade77 | 402 | |
4db384c9 | 403 | void |
79a490a9 | 404 | invert_br_probabilities (rtx insn) |
4db384c9 | 405 | { |
bfdade77 RK |
406 | rtx note; |
407 | ||
408 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | |
409 | if (REG_NOTE_KIND (note) == REG_BR_PROB) | |
410 | XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0))); | |
411 | else if (REG_NOTE_KIND (note) == REG_BR_PRED) | |
412 | XEXP (XEXP (note, 0), 1) | |
413 | = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1))); | |
4db384c9 JH |
414 | } |
415 | ||
416 | /* Dump information about the branch prediction to the output file. */ | |
bfdade77 | 417 | |
4db384c9 | 418 | static void |
6de9cd9a | 419 | dump_prediction (FILE *file, enum br_predictor predictor, int probability, |
79a490a9 | 420 | basic_block bb, int used) |
4db384c9 | 421 | { |
628f6a4e BE |
422 | edge e; |
423 | edge_iterator ei; | |
4db384c9 | 424 | |
6de9cd9a | 425 | if (!file) |
4db384c9 JH |
426 | return; |
427 | ||
628f6a4e BE |
428 | FOR_EACH_EDGE (e, ei, bb->succs) |
429 | if (! (e->flags & EDGE_FALLTHRU)) | |
430 | break; | |
4db384c9 | 431 | |
6de9cd9a | 432 | fprintf (file, " %s heuristics%s: %.1f%%", |
4db384c9 | 433 | predictor_info[predictor].name, |
bfdade77 | 434 | used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE); |
4db384c9 JH |
435 | |
436 | if (bb->count) | |
25c3a4ef | 437 | { |
6de9cd9a DN |
438 | fprintf (file, " exec "); |
439 | fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count); | |
fbc2782e DD |
440 | if (e) |
441 | { | |
6de9cd9a DN |
442 | fprintf (file, " hit "); |
443 | fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count); | |
444 | fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count); | |
fbc2782e | 445 | } |
25c3a4ef | 446 | } |
bfdade77 | 447 | |
6de9cd9a | 448 | fprintf (file, "\n"); |
4db384c9 JH |
449 | } |
450 | ||
229031d0 | 451 | /* We can not predict the probabilities of outgoing edges of bb. Set them |
87022a6b JH |
452 | evenly and hope for the best. */ |
453 | static void | |
454 | set_even_probabilities (basic_block bb) | |
455 | { | |
456 | int nedges = 0; | |
457 | edge e; | |
628f6a4e | 458 | edge_iterator ei; |
87022a6b | 459 | |
628f6a4e | 460 | FOR_EACH_EDGE (e, ei, bb->succs) |
87022a6b JH |
461 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) |
462 | nedges ++; | |
628f6a4e | 463 | FOR_EACH_EDGE (e, ei, bb->succs) |
87022a6b JH |
464 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) |
465 | e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges; | |
466 | else | |
467 | e->probability = 0; | |
468 | } | |
469 | ||
4db384c9 JH |
470 | /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB |
471 | note if not already present. Remove now useless REG_BR_PRED notes. */ | |
bfdade77 | 472 | |
4db384c9 | 473 | static void |
79a490a9 | 474 | combine_predictions_for_insn (rtx insn, basic_block bb) |
4db384c9 | 475 | { |
87022a6b JH |
476 | rtx prob_note; |
477 | rtx *pnote; | |
bfdade77 | 478 | rtx note; |
4db384c9 JH |
479 | int best_probability = PROB_EVEN; |
480 | int best_predictor = END_PREDICTORS; | |
134d3a2e JH |
481 | int combined_probability = REG_BR_PROB_BASE / 2; |
482 | int d; | |
d195b46f JH |
483 | bool first_match = false; |
484 | bool found = false; | |
4db384c9 | 485 | |
87022a6b JH |
486 | if (!can_predict_insn_p (insn)) |
487 | { | |
488 | set_even_probabilities (bb); | |
489 | return; | |
490 | } | |
491 | ||
492 | prob_note = find_reg_note (insn, REG_BR_PROB, 0); | |
493 | pnote = ®_NOTES (insn); | |
c263766c RH |
494 | if (dump_file) |
495 | fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn), | |
0b17ab2f | 496 | bb->index); |
4db384c9 JH |
497 | |
498 | /* We implement "first match" heuristics and use probability guessed | |
6de9cd9a | 499 | by predictor with smallest index. */ |
bfdade77 RK |
500 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) |
501 | if (REG_NOTE_KIND (note) == REG_BR_PRED) | |
502 | { | |
503 | int predictor = INTVAL (XEXP (XEXP (note, 0), 0)); | |
504 | int probability = INTVAL (XEXP (XEXP (note, 0), 1)); | |
505 | ||
506 | found = true; | |
507 | if (best_predictor > predictor) | |
508 | best_probability = probability, best_predictor = predictor; | |
509 | ||
510 | d = (combined_probability * probability | |
511 | + (REG_BR_PROB_BASE - combined_probability) | |
512 | * (REG_BR_PROB_BASE - probability)); | |
513 | ||
514 | /* Use FP math to avoid overflows of 32bit integers. */ | |
571a03b8 JJ |
515 | if (d == 0) |
516 | /* If one probability is 0% and one 100%, avoid division by zero. */ | |
517 | combined_probability = REG_BR_PROB_BASE / 2; | |
518 | else | |
519 | combined_probability = (((double) combined_probability) * probability | |
520 | * REG_BR_PROB_BASE / d + 0.5); | |
bfdade77 RK |
521 | } |
522 | ||
523 | /* Decide which heuristic to use. In case we didn't match anything, | |
524 | use no_prediction heuristic, in case we did match, use either | |
d195b46f JH |
525 | first match or Dempster-Shaffer theory depending on the flags. */ |
526 | ||
134d3a2e | 527 | if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH) |
d195b46f JH |
528 | first_match = true; |
529 | ||
530 | if (!found) | |
6de9cd9a DN |
531 | dump_prediction (dump_file, PRED_NO_PREDICTION, |
532 | combined_probability, bb, true); | |
d195b46f JH |
533 | else |
534 | { | |
6de9cd9a DN |
535 | dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, |
536 | bb, !first_match); | |
537 | dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, | |
538 | bb, first_match); | |
d195b46f JH |
539 | } |
540 | ||
541 | if (first_match) | |
134d3a2e | 542 | combined_probability = best_probability; |
6de9cd9a | 543 | dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true); |
d195b46f JH |
544 | |
545 | while (*pnote) | |
546 | { | |
547 | if (REG_NOTE_KIND (*pnote) == REG_BR_PRED) | |
548 | { | |
549 | int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0)); | |
550 | int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1)); | |
551 | ||
6de9cd9a | 552 | dump_prediction (dump_file, predictor, probability, bb, |
d195b46f | 553 | !first_match || best_predictor == predictor); |
6a4d6760 | 554 | *pnote = XEXP (*pnote, 1); |
d195b46f JH |
555 | } |
556 | else | |
6a4d6760 | 557 | pnote = &XEXP (*pnote, 1); |
d195b46f | 558 | } |
bfdade77 | 559 | |
4db384c9 JH |
560 | if (!prob_note) |
561 | { | |
65c5f2a6 | 562 | add_reg_note (insn, REG_BR_PROB, GEN_INT (combined_probability)); |
bfdade77 | 563 | |
134d3a2e JH |
564 | /* Save the prediction into CFG in case we are seeing non-degenerated |
565 | conditional jump. */ | |
c5cbcccf | 566 | if (!single_succ_p (bb)) |
134d3a2e JH |
567 | { |
568 | BRANCH_EDGE (bb)->probability = combined_probability; | |
bfdade77 RK |
569 | FALLTHRU_EDGE (bb)->probability |
570 | = REG_BR_PROB_BASE - combined_probability; | |
134d3a2e | 571 | } |
4db384c9 | 572 | } |
c5cbcccf | 573 | else if (!single_succ_p (bb)) |
e53de54d JH |
574 | { |
575 | int prob = INTVAL (XEXP (prob_note, 0)); | |
576 | ||
577 | BRANCH_EDGE (bb)->probability = prob; | |
578 | FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob; | |
579 | } | |
580 | else | |
c5cbcccf | 581 | single_succ_edge (bb)->probability = REG_BR_PROB_BASE; |
ee92cb46 JH |
582 | } |
583 | ||
6de9cd9a DN |
584 | /* Combine predictions into single probability and store them into CFG. |
585 | Remove now useless prediction entries. */ | |
f1ebdfc5 | 586 | |
6de9cd9a | 587 | static void |
10d22567 | 588 | combine_predictions_for_bb (basic_block bb) |
f1ebdfc5 | 589 | { |
6de9cd9a DN |
590 | int best_probability = PROB_EVEN; |
591 | int best_predictor = END_PREDICTORS; | |
592 | int combined_probability = REG_BR_PROB_BASE / 2; | |
593 | int d; | |
594 | bool first_match = false; | |
595 | bool found = false; | |
596 | struct edge_prediction *pred; | |
597 | int nedges = 0; | |
598 | edge e, first = NULL, second = NULL; | |
628f6a4e | 599 | edge_iterator ei; |
f06b0a10 | 600 | void **preds; |
f1ebdfc5 | 601 | |
628f6a4e | 602 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
603 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) |
604 | { | |
628f6a4e | 605 | nedges ++; |
6de9cd9a DN |
606 | if (first && !second) |
607 | second = e; | |
608 | if (!first) | |
609 | first = e; | |
610 | } | |
611 | ||
612 | /* When there is no successor or only one choice, prediction is easy. | |
613 | ||
614 | We are lazy for now and predict only basic blocks with two outgoing | |
615 | edges. It is possible to predict generic case too, but we have to | |
616 | ignore first match heuristics and do more involved combining. Implement | |
617 | this later. */ | |
618 | if (nedges != 2) | |
619 | { | |
87022a6b JH |
620 | if (!bb->count) |
621 | set_even_probabilities (bb); | |
f06b0a10 | 622 | clear_bb_predictions (bb); |
10d22567 ZD |
623 | if (dump_file) |
624 | fprintf (dump_file, "%i edges in bb %i predicted to even probabilities\n", | |
6de9cd9a DN |
625 | nedges, bb->index); |
626 | return; | |
627 | } | |
628 | ||
10d22567 ZD |
629 | if (dump_file) |
630 | fprintf (dump_file, "Predictions for bb %i\n", bb->index); | |
6de9cd9a | 631 | |
f06b0a10 ZD |
632 | preds = pointer_map_contains (bb_predictions, bb); |
633 | if (preds) | |
6de9cd9a | 634 | { |
f06b0a10 ZD |
635 | /* We implement "first match" heuristics and use probability guessed |
636 | by predictor with smallest index. */ | |
d3bfe4de | 637 | for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) |
f06b0a10 ZD |
638 | { |
639 | int predictor = pred->ep_predictor; | |
640 | int probability = pred->ep_probability; | |
6de9cd9a | 641 | |
f06b0a10 ZD |
642 | if (pred->ep_edge != first) |
643 | probability = REG_BR_PROB_BASE - probability; | |
6de9cd9a | 644 | |
f06b0a10 ZD |
645 | found = true; |
646 | if (best_predictor > predictor) | |
647 | best_probability = probability, best_predictor = predictor; | |
6de9cd9a | 648 | |
f06b0a10 ZD |
649 | d = (combined_probability * probability |
650 | + (REG_BR_PROB_BASE - combined_probability) | |
651 | * (REG_BR_PROB_BASE - probability)); | |
6de9cd9a | 652 | |
f06b0a10 ZD |
653 | /* Use FP math to avoid overflows of 32bit integers. */ |
654 | if (d == 0) | |
655 | /* If one probability is 0% and one 100%, avoid division by zero. */ | |
656 | combined_probability = REG_BR_PROB_BASE / 2; | |
657 | else | |
658 | combined_probability = (((double) combined_probability) | |
659 | * probability | |
660 | * REG_BR_PROB_BASE / d + 0.5); | |
661 | } | |
6de9cd9a DN |
662 | } |
663 | ||
664 | /* Decide which heuristic to use. In case we didn't match anything, | |
665 | use no_prediction heuristic, in case we did match, use either | |
666 | first match or Dempster-Shaffer theory depending on the flags. */ | |
667 | ||
668 | if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH) | |
669 | first_match = true; | |
670 | ||
671 | if (!found) | |
10d22567 | 672 | dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb, true); |
6de9cd9a DN |
673 | else |
674 | { | |
10d22567 | 675 | dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb, |
6de9cd9a | 676 | !first_match); |
10d22567 | 677 | dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb, |
6de9cd9a DN |
678 | first_match); |
679 | } | |
680 | ||
681 | if (first_match) | |
682 | combined_probability = best_probability; | |
10d22567 | 683 | dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true); |
6de9cd9a | 684 | |
f06b0a10 | 685 | if (preds) |
6de9cd9a | 686 | { |
d3bfe4de | 687 | for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) |
f06b0a10 ZD |
688 | { |
689 | int predictor = pred->ep_predictor; | |
690 | int probability = pred->ep_probability; | |
6de9cd9a | 691 | |
f06b0a10 ZD |
692 | if (pred->ep_edge != EDGE_SUCC (bb, 0)) |
693 | probability = REG_BR_PROB_BASE - probability; | |
694 | dump_prediction (dump_file, predictor, probability, bb, | |
695 | !first_match || best_predictor == predictor); | |
696 | } | |
6de9cd9a | 697 | } |
f06b0a10 | 698 | clear_bb_predictions (bb); |
6de9cd9a | 699 | |
87022a6b JH |
700 | if (!bb->count) |
701 | { | |
702 | first->probability = combined_probability; | |
703 | second->probability = REG_BR_PROB_BASE - combined_probability; | |
704 | } | |
6de9cd9a DN |
705 | } |
706 | ||
d73be268 ZD |
707 | /* Predict edge probabilities by exploiting loop structure. */ |
708 | ||
6de9cd9a | 709 | static void |
d73be268 | 710 | predict_loops (void) |
6de9cd9a | 711 | { |
42fd6772 ZD |
712 | loop_iterator li; |
713 | struct loop *loop; | |
0b92ff33 | 714 | |
d73be268 | 715 | scev_initialize (); |
b6acab32 | 716 | |
65169dcf JE |
717 | /* Try to predict out blocks in a loop that are not part of a |
718 | natural loop. */ | |
42fd6772 | 719 | FOR_EACH_LOOP (li, loop, 0) |
f1ebdfc5 | 720 | { |
2ecfd709 | 721 | basic_block bb, *bbs; |
ca83d385 | 722 | unsigned j, n_exits; |
ca83d385 | 723 | VEC (edge, heap) *exits; |
992c31e6 | 724 | struct tree_niter_desc niter_desc; |
ca83d385 | 725 | edge ex; |
f1ebdfc5 | 726 | |
ca83d385 ZD |
727 | exits = get_loop_exit_edges (loop); |
728 | n_exits = VEC_length (edge, exits); | |
0dd0e980 | 729 | |
ca83d385 | 730 | for (j = 0; VEC_iterate (edge, exits, j, ex); j++) |
b6acab32 | 731 | { |
992c31e6 | 732 | tree niter = NULL; |
4839cb59 ZD |
733 | HOST_WIDE_INT nitercst; |
734 | int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS); | |
735 | int probability; | |
736 | enum br_predictor predictor; | |
b6acab32 | 737 | |
ca83d385 | 738 | if (number_of_iterations_exit (loop, ex, &niter_desc, false)) |
992c31e6 JH |
739 | niter = niter_desc.niter; |
740 | if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST) | |
ca83d385 | 741 | niter = loop_niter_by_eval (loop, ex); |
b6acab32 | 742 | |
992c31e6 JH |
743 | if (TREE_CODE (niter) == INTEGER_CST) |
744 | { | |
992c31e6 | 745 | if (host_integerp (niter, 1) |
7fb41a42 | 746 | && compare_tree_int (niter, max-1) == -1) |
4839cb59 | 747 | nitercst = tree_low_cst (niter, 1) + 1; |
992c31e6 | 748 | else |
4839cb59 ZD |
749 | nitercst = max; |
750 | predictor = PRED_LOOP_ITERATIONS; | |
751 | } | |
752 | /* If we have just one exit and we can derive some information about | |
753 | the number of iterations of the loop from the statements inside | |
754 | the loop, use it to predict this exit. */ | |
755 | else if (n_exits == 1) | |
756 | { | |
757 | nitercst = estimated_loop_iterations_int (loop, false); | |
758 | if (nitercst < 0) | |
759 | continue; | |
760 | if (nitercst > max) | |
761 | nitercst = max; | |
b6acab32 | 762 | |
4839cb59 | 763 | predictor = PRED_LOOP_ITERATIONS_GUESSED; |
992c31e6 | 764 | } |
4839cb59 ZD |
765 | else |
766 | continue; | |
767 | ||
768 | probability = ((REG_BR_PROB_BASE + nitercst / 2) / nitercst); | |
769 | predict_edge (ex, predictor, probability); | |
b6acab32 | 770 | } |
ca83d385 | 771 | VEC_free (edge, heap, exits); |
3d436d2a | 772 | |
2ecfd709 | 773 | bbs = get_loop_body (loop); |
6de9cd9a | 774 | |
2ecfd709 ZD |
775 | for (j = 0; j < loop->num_nodes; j++) |
776 | { | |
777 | int header_found = 0; | |
778 | edge e; | |
628f6a4e | 779 | edge_iterator ei; |
2ecfd709 ZD |
780 | |
781 | bb = bbs[j]; | |
bfdade77 | 782 | |
969d70ca JH |
783 | /* Bypass loop heuristics on continue statement. These |
784 | statements construct loops via "non-loop" constructs | |
785 | in the source language and are better to be handled | |
786 | separately. */ | |
992c31e6 | 787 | if (predicted_by_p (bb, PRED_CONTINUE)) |
969d70ca JH |
788 | continue; |
789 | ||
2ecfd709 ZD |
790 | /* Loop branch heuristics - predict an edge back to a |
791 | loop's head as taken. */ | |
9ff3d2de JL |
792 | if (bb == loop->latch) |
793 | { | |
794 | e = find_edge (loop->latch, loop->header); | |
795 | if (e) | |
796 | { | |
797 | header_found = 1; | |
798 | predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN); | |
799 | } | |
800 | } | |
bfdade77 | 801 | |
2ecfd709 | 802 | /* Loop exit heuristics - predict an edge exiting the loop if the |
d55d8fc7 | 803 | conditional has no loop header successors as not taken. */ |
4839cb59 ZD |
804 | if (!header_found |
805 | /* If we already used more reliable loop exit predictors, do not | |
806 | bother with PRED_LOOP_EXIT. */ | |
807 | && !predicted_by_p (bb, PRED_LOOP_ITERATIONS_GUESSED) | |
808 | && !predicted_by_p (bb, PRED_LOOP_ITERATIONS)) | |
2c9e13f3 JH |
809 | { |
810 | /* For loop with many exits we don't want to predict all exits | |
811 | with the pretty large probability, because if all exits are | |
812 | considered in row, the loop would be predicted to iterate | |
813 | almost never. The code to divide probability by number of | |
814 | exits is very rough. It should compute the number of exits | |
815 | taken in each patch through function (not the overall number | |
816 | of exits that might be a lot higher for loops with wide switch | |
817 | statements in them) and compute n-th square root. | |
818 | ||
819 | We limit the minimal probability by 2% to avoid | |
820 | EDGE_PROBABILITY_RELIABLE from trusting the branch prediction | |
821 | as this was causing regression in perl benchmark containing such | |
822 | a wide loop. */ | |
823 | ||
824 | int probability = ((REG_BR_PROB_BASE | |
825 | - predictor_info [(int) PRED_LOOP_EXIT].hitrate) | |
826 | / n_exits); | |
827 | if (probability < HITRATE (2)) | |
828 | probability = HITRATE (2); | |
829 | FOR_EACH_EDGE (e, ei, bb->succs) | |
830 | if (e->dest->index < NUM_FIXED_BLOCKS | |
831 | || !flow_bb_inside_loop_p (loop, e->dest)) | |
832 | predict_edge (e, PRED_LOOP_EXIT, probability); | |
833 | } | |
2ecfd709 | 834 | } |
36579663 | 835 | |
e0a21ab9 | 836 | /* Free basic blocks from get_loop_body. */ |
36579663 | 837 | free (bbs); |
f1ebdfc5 | 838 | } |
b6acab32 | 839 | |
992c31e6 | 840 | scev_finalize (); |
6de9cd9a DN |
841 | } |
842 | ||
87022a6b JH |
843 | /* Attempt to predict probabilities of BB outgoing edges using local |
844 | properties. */ | |
845 | static void | |
846 | bb_estimate_probability_locally (basic_block bb) | |
847 | { | |
848 | rtx last_insn = BB_END (bb); | |
849 | rtx cond; | |
850 | ||
851 | if (! can_predict_insn_p (last_insn)) | |
852 | return; | |
853 | cond = get_condition (last_insn, NULL, false, false); | |
854 | if (! cond) | |
855 | return; | |
856 | ||
857 | /* Try "pointer heuristic." | |
858 | A comparison ptr == 0 is predicted as false. | |
859 | Similarly, a comparison ptr1 == ptr2 is predicted as false. */ | |
860 | if (COMPARISON_P (cond) | |
861 | && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0))) | |
862 | || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1))))) | |
863 | { | |
864 | if (GET_CODE (cond) == EQ) | |
865 | predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN); | |
866 | else if (GET_CODE (cond) == NE) | |
867 | predict_insn_def (last_insn, PRED_POINTER, TAKEN); | |
868 | } | |
869 | else | |
870 | ||
871 | /* Try "opcode heuristic." | |
872 | EQ tests are usually false and NE tests are usually true. Also, | |
873 | most quantities are positive, so we can make the appropriate guesses | |
874 | about signed comparisons against zero. */ | |
875 | switch (GET_CODE (cond)) | |
876 | { | |
877 | case CONST_INT: | |
878 | /* Unconditional branch. */ | |
879 | predict_insn_def (last_insn, PRED_UNCONDITIONAL, | |
880 | cond == const0_rtx ? NOT_TAKEN : TAKEN); | |
881 | break; | |
882 | ||
883 | case EQ: | |
884 | case UNEQ: | |
885 | /* Floating point comparisons appears to behave in a very | |
886 | unpredictable way because of special role of = tests in | |
887 | FP code. */ | |
888 | if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0)))) | |
889 | ; | |
890 | /* Comparisons with 0 are often used for booleans and there is | |
891 | nothing useful to predict about them. */ | |
892 | else if (XEXP (cond, 1) == const0_rtx | |
893 | || XEXP (cond, 0) == const0_rtx) | |
894 | ; | |
895 | else | |
896 | predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN); | |
897 | break; | |
898 | ||
899 | case NE: | |
900 | case LTGT: | |
901 | /* Floating point comparisons appears to behave in a very | |
902 | unpredictable way because of special role of = tests in | |
903 | FP code. */ | |
904 | if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0)))) | |
905 | ; | |
906 | /* Comparisons with 0 are often used for booleans and there is | |
907 | nothing useful to predict about them. */ | |
908 | else if (XEXP (cond, 1) == const0_rtx | |
909 | || XEXP (cond, 0) == const0_rtx) | |
910 | ; | |
911 | else | |
912 | predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN); | |
913 | break; | |
914 | ||
915 | case ORDERED: | |
916 | predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN); | |
917 | break; | |
918 | ||
919 | case UNORDERED: | |
920 | predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN); | |
921 | break; | |
922 | ||
923 | case LE: | |
924 | case LT: | |
925 | if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx | |
926 | || XEXP (cond, 1) == constm1_rtx) | |
927 | predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN); | |
928 | break; | |
929 | ||
930 | case GE: | |
931 | case GT: | |
932 | if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx | |
933 | || XEXP (cond, 1) == constm1_rtx) | |
934 | predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN); | |
935 | break; | |
936 | ||
937 | default: | |
938 | break; | |
939 | } | |
940 | } | |
941 | ||
229031d0 | 942 | /* Set edge->probability for each successor edge of BB. */ |
87022a6b JH |
943 | void |
944 | guess_outgoing_edge_probabilities (basic_block bb) | |
945 | { | |
946 | bb_estimate_probability_locally (bb); | |
947 | combine_predictions_for_insn (BB_END (bb), bb); | |
948 | } | |
6de9cd9a | 949 | \f |
726a989a RB |
950 | static tree expr_expected_value (tree, bitmap); |
951 | ||
952 | /* Helper function for expr_expected_value. */ | |
42f97fd2 JH |
953 | |
954 | static tree | |
726a989a | 955 | expr_expected_value_1 (tree type, tree op0, enum tree_code code, tree op1, bitmap visited) |
42f97fd2 | 956 | { |
726a989a RB |
957 | gimple def; |
958 | ||
959 | if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) | |
42f97fd2 | 960 | { |
726a989a RB |
961 | if (TREE_CONSTANT (op0)) |
962 | return op0; | |
963 | ||
964 | if (code != SSA_NAME) | |
965 | return NULL_TREE; | |
966 | ||
967 | def = SSA_NAME_DEF_STMT (op0); | |
42f97fd2 JH |
968 | |
969 | /* If we were already here, break the infinite cycle. */ | |
726a989a | 970 | if (bitmap_bit_p (visited, SSA_NAME_VERSION (op0))) |
42f97fd2 | 971 | return NULL; |
726a989a | 972 | bitmap_set_bit (visited, SSA_NAME_VERSION (op0)); |
42f97fd2 | 973 | |
726a989a | 974 | if (gimple_code (def) == GIMPLE_PHI) |
42f97fd2 JH |
975 | { |
976 | /* All the arguments of the PHI node must have the same constant | |
977 | length. */ | |
726a989a | 978 | int i, n = gimple_phi_num_args (def); |
42f97fd2 | 979 | tree val = NULL, new_val; |
6de9cd9a | 980 | |
726a989a | 981 | for (i = 0; i < n; i++) |
42f97fd2 JH |
982 | { |
983 | tree arg = PHI_ARG_DEF (def, i); | |
984 | ||
985 | /* If this PHI has itself as an argument, we cannot | |
986 | determine the string length of this argument. However, | |
1f838355 | 987 | if we can find an expected constant value for the other |
42f97fd2 JH |
988 | PHI args then we can still be sure that this is |
989 | likely a constant. So be optimistic and just | |
990 | continue with the next argument. */ | |
991 | if (arg == PHI_RESULT (def)) | |
992 | continue; | |
993 | ||
994 | new_val = expr_expected_value (arg, visited); | |
995 | if (!new_val) | |
996 | return NULL; | |
997 | if (!val) | |
998 | val = new_val; | |
999 | else if (!operand_equal_p (val, new_val, false)) | |
1000 | return NULL; | |
1001 | } | |
1002 | return val; | |
1003 | } | |
726a989a | 1004 | if (is_gimple_assign (def)) |
42f97fd2 | 1005 | { |
726a989a RB |
1006 | if (gimple_assign_lhs (def) != op0) |
1007 | return NULL; | |
42f97fd2 | 1008 | |
726a989a RB |
1009 | return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)), |
1010 | gimple_assign_rhs1 (def), | |
1011 | gimple_assign_rhs_code (def), | |
1012 | gimple_assign_rhs2 (def), | |
1013 | visited); | |
1014 | } | |
1015 | ||
1016 | if (is_gimple_call (def)) | |
1017 | { | |
1018 | tree decl = gimple_call_fndecl (def); | |
1019 | if (!decl) | |
5039610b | 1020 | return NULL; |
726a989a RB |
1021 | if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL |
1022 | && DECL_FUNCTION_CODE (decl) == BUILT_IN_EXPECT) | |
1023 | { | |
1024 | tree val; | |
1025 | ||
1026 | if (gimple_call_num_args (def) != 2) | |
1027 | return NULL; | |
1028 | val = gimple_call_arg (def, 0); | |
1029 | if (TREE_CONSTANT (val)) | |
1030 | return val; | |
1031 | return gimple_call_arg (def, 1); | |
1032 | } | |
42f97fd2 | 1033 | } |
726a989a RB |
1034 | |
1035 | return NULL; | |
42f97fd2 | 1036 | } |
726a989a RB |
1037 | |
1038 | if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS) | |
42f97fd2 | 1039 | { |
726a989a RB |
1040 | tree res; |
1041 | op0 = expr_expected_value (op0, visited); | |
42f97fd2 JH |
1042 | if (!op0) |
1043 | return NULL; | |
726a989a | 1044 | op1 = expr_expected_value (op1, visited); |
42f97fd2 JH |
1045 | if (!op1) |
1046 | return NULL; | |
726a989a | 1047 | res = fold_build2 (code, type, op0, op1); |
42f97fd2 JH |
1048 | if (TREE_CONSTANT (res)) |
1049 | return res; | |
1050 | return NULL; | |
1051 | } | |
726a989a | 1052 | if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS) |
42f97fd2 | 1053 | { |
726a989a RB |
1054 | tree res; |
1055 | op0 = expr_expected_value (op0, visited); | |
42f97fd2 JH |
1056 | if (!op0) |
1057 | return NULL; | |
726a989a | 1058 | res = fold_build1 (code, type, op0); |
42f97fd2 JH |
1059 | if (TREE_CONSTANT (res)) |
1060 | return res; | |
1061 | return NULL; | |
1062 | } | |
1063 | return NULL; | |
1064 | } | |
726a989a RB |
1065 | |
1066 | /* Return constant EXPR will likely have at execution time, NULL if unknown. | |
1067 | The function is used by builtin_expect branch predictor so the evidence | |
1068 | must come from this construct and additional possible constant folding. | |
1069 | ||
1070 | We may want to implement more involved value guess (such as value range | |
1071 | propagation based prediction), but such tricks shall go to new | |
1072 | implementation. */ | |
1073 | ||
1074 | static tree | |
1075 | expr_expected_value (tree expr, bitmap visited) | |
1076 | { | |
1077 | enum tree_code code; | |
1078 | tree op0, op1; | |
1079 | ||
1080 | if (TREE_CONSTANT (expr)) | |
1081 | return expr; | |
1082 | ||
1083 | extract_ops_from_tree (expr, &code, &op0, &op1); | |
1084 | return expr_expected_value_1 (TREE_TYPE (expr), | |
1085 | op0, code, op1, visited); | |
1086 | } | |
1087 | ||
42f97fd2 JH |
1088 | \f |
1089 | /* Get rid of all builtin_expect calls we no longer need. */ | |
1090 | static void | |
1091 | strip_builtin_expect (void) | |
1092 | { | |
1093 | basic_block bb; | |
726a989a RB |
1094 | gimple ass_stmt; |
1095 | tree var; | |
1096 | ||
42f97fd2 JH |
1097 | FOR_EACH_BB (bb) |
1098 | { | |
726a989a RB |
1099 | gimple_stmt_iterator bi; |
1100 | for (bi = gsi_start_bb (bb); !gsi_end_p (bi); gsi_next (&bi)) | |
42f97fd2 | 1101 | { |
726a989a | 1102 | gimple stmt = gsi_stmt (bi); |
42f97fd2 | 1103 | tree fndecl; |
42f97fd2 | 1104 | |
726a989a RB |
1105 | if (gimple_code (stmt) != GIMPLE_CALL) |
1106 | continue; | |
1107 | ||
1108 | fndecl = gimple_call_fndecl (stmt); | |
1109 | ||
1110 | if (fndecl | |
8c96cd51 | 1111 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL |
42f97fd2 | 1112 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT |
726a989a | 1113 | && gimple_call_num_args (stmt) == 2) |
42f97fd2 | 1114 | { |
726a989a RB |
1115 | var = gimple_call_lhs (stmt); |
1116 | ass_stmt = gimple_build_assign (var, gimple_call_arg (stmt, 0)); | |
1117 | ||
1118 | gsi_replace (&bi, ass_stmt, true); | |
42f97fd2 JH |
1119 | } |
1120 | } | |
1121 | } | |
1122 | } | |
1123 | \f | |
6de9cd9a DN |
1124 | /* Predict using opcode of the last statement in basic block. */ |
1125 | static void | |
1126 | tree_predict_by_opcode (basic_block bb) | |
1127 | { | |
726a989a | 1128 | gimple stmt = last_stmt (bb); |
6de9cd9a | 1129 | edge then_edge; |
726a989a | 1130 | tree op0, op1; |
6de9cd9a | 1131 | tree type; |
42f97fd2 | 1132 | tree val; |
726a989a | 1133 | enum tree_code cmp; |
42f97fd2 | 1134 | bitmap visited; |
628f6a4e | 1135 | edge_iterator ei; |
6de9cd9a | 1136 | |
726a989a | 1137 | if (!stmt || gimple_code (stmt) != GIMPLE_COND) |
6de9cd9a | 1138 | return; |
628f6a4e | 1139 | FOR_EACH_EDGE (then_edge, ei, bb->succs) |
6de9cd9a | 1140 | if (then_edge->flags & EDGE_TRUE_VALUE) |
628f6a4e | 1141 | break; |
726a989a RB |
1142 | op0 = gimple_cond_lhs (stmt); |
1143 | op1 = gimple_cond_rhs (stmt); | |
1144 | cmp = gimple_cond_code (stmt); | |
6de9cd9a | 1145 | type = TREE_TYPE (op0); |
8bdbfff5 | 1146 | visited = BITMAP_ALLOC (NULL); |
726a989a | 1147 | val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, visited); |
8bdbfff5 | 1148 | BITMAP_FREE (visited); |
42f97fd2 JH |
1149 | if (val) |
1150 | { | |
1151 | if (integer_zerop (val)) | |
1152 | predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, NOT_TAKEN); | |
1153 | else | |
1154 | predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, TAKEN); | |
1155 | return; | |
1156 | } | |
6de9cd9a DN |
1157 | /* Try "pointer heuristic." |
1158 | A comparison ptr == 0 is predicted as false. | |
1159 | Similarly, a comparison ptr1 == ptr2 is predicted as false. */ | |
1160 | if (POINTER_TYPE_P (type)) | |
1161 | { | |
726a989a | 1162 | if (cmp == EQ_EXPR) |
6de9cd9a | 1163 | predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN); |
726a989a | 1164 | else if (cmp == NE_EXPR) |
6de9cd9a DN |
1165 | predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN); |
1166 | } | |
1167 | else | |
1168 | ||
1169 | /* Try "opcode heuristic." | |
1170 | EQ tests are usually false and NE tests are usually true. Also, | |
1171 | most quantities are positive, so we can make the appropriate guesses | |
1172 | about signed comparisons against zero. */ | |
726a989a | 1173 | switch (cmp) |
6de9cd9a DN |
1174 | { |
1175 | case EQ_EXPR: | |
1176 | case UNEQ_EXPR: | |
1177 | /* Floating point comparisons appears to behave in a very | |
1178 | unpredictable way because of special role of = tests in | |
1179 | FP code. */ | |
1180 | if (FLOAT_TYPE_P (type)) | |
1181 | ; | |
1182 | /* Comparisons with 0 are often used for booleans and there is | |
1183 | nothing useful to predict about them. */ | |
726a989a | 1184 | else if (integer_zerop (op0) || integer_zerop (op1)) |
6de9cd9a DN |
1185 | ; |
1186 | else | |
1187 | predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN); | |
1188 | break; | |
1189 | ||
1190 | case NE_EXPR: | |
d1a7edaf | 1191 | case LTGT_EXPR: |
6de9cd9a DN |
1192 | /* Floating point comparisons appears to behave in a very |
1193 | unpredictable way because of special role of = tests in | |
1194 | FP code. */ | |
1195 | if (FLOAT_TYPE_P (type)) | |
1196 | ; | |
1197 | /* Comparisons with 0 are often used for booleans and there is | |
1198 | nothing useful to predict about them. */ | |
1199 | else if (integer_zerop (op0) | |
726a989a | 1200 | || integer_zerop (op1)) |
6de9cd9a DN |
1201 | ; |
1202 | else | |
1203 | predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN); | |
1204 | break; | |
1205 | ||
1206 | case ORDERED_EXPR: | |
1207 | predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN); | |
1208 | break; | |
1209 | ||
1210 | case UNORDERED_EXPR: | |
1211 | predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN); | |
1212 | break; | |
1213 | ||
1214 | case LE_EXPR: | |
1215 | case LT_EXPR: | |
726a989a RB |
1216 | if (integer_zerop (op1) |
1217 | || integer_onep (op1) | |
1218 | || integer_all_onesp (op1) | |
1219 | || real_zerop (op1) | |
1220 | || real_onep (op1) | |
1221 | || real_minus_onep (op1)) | |
6de9cd9a DN |
1222 | predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN); |
1223 | break; | |
1224 | ||
1225 | case GE_EXPR: | |
1226 | case GT_EXPR: | |
726a989a RB |
1227 | if (integer_zerop (op1) |
1228 | || integer_onep (op1) | |
1229 | || integer_all_onesp (op1) | |
1230 | || real_zerop (op1) | |
1231 | || real_onep (op1) | |
1232 | || real_minus_onep (op1)) | |
6de9cd9a DN |
1233 | predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN); |
1234 | break; | |
1235 | ||
1236 | default: | |
1237 | break; | |
1238 | } | |
1239 | } | |
1240 | ||
bb033fd8 | 1241 | /* Try to guess whether the value of return means error code. */ |
726a989a | 1242 | |
bb033fd8 JH |
1243 | static enum br_predictor |
1244 | return_prediction (tree val, enum prediction *prediction) | |
1245 | { | |
1246 | /* VOID. */ | |
1247 | if (!val) | |
1248 | return PRED_NO_PREDICTION; | |
1249 | /* Different heuristics for pointers and scalars. */ | |
1250 | if (POINTER_TYPE_P (TREE_TYPE (val))) | |
1251 | { | |
1252 | /* NULL is usually not returned. */ | |
1253 | if (integer_zerop (val)) | |
1254 | { | |
1255 | *prediction = NOT_TAKEN; | |
1256 | return PRED_NULL_RETURN; | |
1257 | } | |
1258 | } | |
1259 | else if (INTEGRAL_TYPE_P (TREE_TYPE (val))) | |
1260 | { | |
1261 | /* Negative return values are often used to indicate | |
1262 | errors. */ | |
1263 | if (TREE_CODE (val) == INTEGER_CST | |
1264 | && tree_int_cst_sgn (val) < 0) | |
1265 | { | |
1266 | *prediction = NOT_TAKEN; | |
1267 | return PRED_NEGATIVE_RETURN; | |
1268 | } | |
1269 | /* Constant return values seems to be commonly taken. | |
1270 | Zero/one often represent booleans so exclude them from the | |
1271 | heuristics. */ | |
1272 | if (TREE_CONSTANT (val) | |
1273 | && (!integer_zerop (val) && !integer_onep (val))) | |
1274 | { | |
1275 | *prediction = TAKEN; | |
75b6bb62 | 1276 | return PRED_CONST_RETURN; |
bb033fd8 JH |
1277 | } |
1278 | } | |
1279 | return PRED_NO_PREDICTION; | |
1280 | } | |
1281 | ||
1282 | /* Find the basic block with return expression and look up for possible | |
1283 | return value trying to apply RETURN_PREDICTION heuristics. */ | |
1284 | static void | |
3e4b9ad0 | 1285 | apply_return_prediction (void) |
bb033fd8 | 1286 | { |
726a989a | 1287 | gimple return_stmt = NULL; |
bb033fd8 JH |
1288 | tree return_val; |
1289 | edge e; | |
726a989a | 1290 | gimple phi; |
bb033fd8 JH |
1291 | int phi_num_args, i; |
1292 | enum br_predictor pred; | |
1293 | enum prediction direction; | |
628f6a4e | 1294 | edge_iterator ei; |
bb033fd8 | 1295 | |
628f6a4e | 1296 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds) |
bb033fd8 JH |
1297 | { |
1298 | return_stmt = last_stmt (e->src); | |
8b11009b | 1299 | if (return_stmt |
726a989a | 1300 | && gimple_code (return_stmt) == GIMPLE_RETURN) |
bb033fd8 JH |
1301 | break; |
1302 | } | |
1303 | if (!e) | |
1304 | return; | |
726a989a | 1305 | return_val = gimple_return_retval (return_stmt); |
bb033fd8 JH |
1306 | if (!return_val) |
1307 | return; | |
bb033fd8 JH |
1308 | if (TREE_CODE (return_val) != SSA_NAME |
1309 | || !SSA_NAME_DEF_STMT (return_val) | |
726a989a | 1310 | || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI) |
bb033fd8 | 1311 | return; |
726a989a RB |
1312 | phi = SSA_NAME_DEF_STMT (return_val); |
1313 | phi_num_args = gimple_phi_num_args (phi); | |
bb033fd8 JH |
1314 | pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction); |
1315 | ||
1316 | /* Avoid the degenerate case where all return values form the function | |
1317 | belongs to same category (ie they are all positive constants) | |
1318 | so we can hardly say something about them. */ | |
1319 | for (i = 1; i < phi_num_args; i++) | |
1320 | if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction)) | |
1321 | break; | |
1322 | if (i != phi_num_args) | |
1323 | for (i = 0; i < phi_num_args; i++) | |
1324 | { | |
1325 | pred = return_prediction (PHI_ARG_DEF (phi, i), &direction); | |
1326 | if (pred != PRED_NO_PREDICTION) | |
726a989a | 1327 | predict_paths_leading_to (gimple_phi_arg_edge (phi, i)->src, pred, |
bb033fd8 JH |
1328 | direction); |
1329 | } | |
1330 | } | |
1331 | ||
1332 | /* Look for basic block that contains unlikely to happen events | |
1333 | (such as noreturn calls) and mark all paths leading to execution | |
1334 | of this basic blocks as unlikely. */ | |
1335 | ||
1336 | static void | |
1337 | tree_bb_level_predictions (void) | |
1338 | { | |
1339 | basic_block bb; | |
bb033fd8 | 1340 | |
3e4b9ad0 | 1341 | apply_return_prediction (); |
bb033fd8 JH |
1342 | |
1343 | FOR_EACH_BB (bb) | |
1344 | { | |
726a989a | 1345 | gimple_stmt_iterator gsi; |
bb033fd8 | 1346 | |
726a989a | 1347 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) |
bb033fd8 | 1348 | { |
726a989a | 1349 | gimple stmt = gsi_stmt (gsi); |
52bf96d2 | 1350 | tree decl; |
daac0317 | 1351 | |
726a989a | 1352 | if (is_gimple_call (stmt)) |
bb033fd8 | 1353 | { |
726a989a RB |
1354 | if (gimple_call_flags (stmt) & ECF_NORETURN) |
1355 | predict_paths_leading_to (bb, PRED_NORETURN, | |
1356 | NOT_TAKEN); | |
1357 | decl = gimple_call_fndecl (stmt); | |
1358 | if (decl | |
1359 | && lookup_attribute ("cold", | |
1360 | DECL_ATTRIBUTES (decl))) | |
1361 | predict_paths_leading_to (bb, PRED_COLD_FUNCTION, | |
1362 | NOT_TAKEN); | |
bb033fd8 | 1363 | } |
726a989a RB |
1364 | else if (gimple_code (stmt) == GIMPLE_PREDICT) |
1365 | { | |
1366 | predict_paths_leading_to (bb, gimple_predict_predictor (stmt), | |
1367 | gimple_predict_outcome (stmt)); | |
1368 | gsi_remove (&gsi, true); | |
1369 | continue; | |
1370 | } | |
1371 | ||
1372 | gsi_next (&gsi); | |
bb033fd8 JH |
1373 | } |
1374 | } | |
bb033fd8 JH |
1375 | } |
1376 | ||
f06b0a10 ZD |
1377 | #ifdef ENABLE_CHECKING |
1378 | ||
1379 | /* Callback for pointer_map_traverse, asserts that the pointer map is | |
1380 | empty. */ | |
1381 | ||
1382 | static bool | |
ac7d7749 | 1383 | assert_is_empty (const void *key ATTRIBUTE_UNUSED, void **value, |
f06b0a10 ZD |
1384 | void *data ATTRIBUTE_UNUSED) |
1385 | { | |
1386 | gcc_assert (!*value); | |
1387 | return false; | |
1388 | } | |
1389 | #endif | |
1390 | ||
6de9cd9a | 1391 | /* Predict branch probabilities and estimate profile of the tree CFG. */ |
c2924966 | 1392 | static unsigned int |
6de9cd9a DN |
1393 | tree_estimate_probability (void) |
1394 | { | |
1395 | basic_block bb; | |
6de9cd9a | 1396 | |
598ec7bd | 1397 | loop_optimizer_init (0); |
d51157de | 1398 | if (dump_file && (dump_flags & TDF_DETAILS)) |
d73be268 | 1399 | flow_loops_dump (dump_file, NULL, 0); |
6de9cd9a | 1400 | |
bb033fd8 | 1401 | add_noreturn_fake_exit_edges (); |
6de9cd9a | 1402 | connect_infinite_loops_to_exit (); |
c7b852c8 ZD |
1403 | /* We use loop_niter_by_eval, which requires that the loops have |
1404 | preheaders. */ | |
1405 | create_preheaders (CP_SIMPLE_PREHEADERS); | |
6de9cd9a DN |
1406 | calculate_dominance_info (CDI_POST_DOMINATORS); |
1407 | ||
f06b0a10 | 1408 | bb_predictions = pointer_map_create (); |
bb033fd8 JH |
1409 | tree_bb_level_predictions (); |
1410 | ||
d73be268 | 1411 | mark_irreducible_loops (); |
4839cb59 | 1412 | record_loop_exits (); |
d51157de | 1413 | if (number_of_loops () > 1) |
d73be268 | 1414 | predict_loops (); |
6de9cd9a DN |
1415 | |
1416 | FOR_EACH_BB (bb) | |
1417 | { | |
1418 | edge e; | |
628f6a4e | 1419 | edge_iterator ei; |
6de9cd9a | 1420 | |
628f6a4e | 1421 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a DN |
1422 | { |
1423 | /* Predict early returns to be probable, as we've already taken | |
bb033fd8 | 1424 | care for error returns and other cases are often used for |
dcb995f7 JH |
1425 | fast paths through function. |
1426 | ||
44c7bd63 | 1427 | Since we've already removed the return statements, we are |
dcb995f7 JH |
1428 | looking for CFG like: |
1429 | ||
44c7bd63 | 1430 | if (conditional) |
dcb995f7 JH |
1431 | { |
1432 | .. | |
1433 | goto return_block | |
1434 | } | |
1435 | some other blocks | |
1436 | return_block: | |
1437 | return_stmt. */ | |
1438 | if (e->dest != bb->next_bb | |
1439 | && e->dest != EXIT_BLOCK_PTR | |
1440 | && single_succ_p (e->dest) | |
1441 | && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR | |
726a989a | 1442 | && gimple_code (last_stmt (e->dest)) == GIMPLE_RETURN) |
bb033fd8 JH |
1443 | { |
1444 | edge e1; | |
628f6a4e | 1445 | edge_iterator ei1; |
bb033fd8 | 1446 | |
dcb995f7 JH |
1447 | if (single_succ_p (bb)) |
1448 | { | |
1449 | FOR_EACH_EDGE (e1, ei1, bb->preds) | |
1450 | if (!predicted_by_p (e1->src, PRED_NULL_RETURN) | |
1451 | && !predicted_by_p (e1->src, PRED_CONST_RETURN) | |
1452 | && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN)) | |
1453 | predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
1454 | } | |
1455 | else | |
1456 | if (!predicted_by_p (e->src, PRED_NULL_RETURN) | |
1457 | && !predicted_by_p (e->src, PRED_CONST_RETURN) | |
1458 | && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN)) | |
1459 | predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
bb033fd8 | 1460 | } |
6de9cd9a | 1461 | |
bb033fd8 | 1462 | /* Look for block we are guarding (ie we dominate it, |
6de9cd9a DN |
1463 | but it doesn't postdominate us). */ |
1464 | if (e->dest != EXIT_BLOCK_PTR && e->dest != bb | |
1465 | && dominated_by_p (CDI_DOMINATORS, e->dest, e->src) | |
1466 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest)) | |
1467 | { | |
726a989a | 1468 | gimple_stmt_iterator bi; |
6de9cd9a DN |
1469 | |
1470 | /* The call heuristic claims that a guarded function call | |
1471 | is improbable. This is because such calls are often used | |
1472 | to signal exceptional situations such as printing error | |
1473 | messages. */ | |
726a989a RB |
1474 | for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi); |
1475 | gsi_next (&bi)) | |
6de9cd9a | 1476 | { |
726a989a RB |
1477 | gimple stmt = gsi_stmt (bi); |
1478 | if (is_gimple_call (stmt) | |
6de9cd9a DN |
1479 | /* Constant and pure calls are hardly used to signalize |
1480 | something exceptional. */ | |
726a989a | 1481 | && gimple_has_side_effects (stmt)) |
6de9cd9a DN |
1482 | { |
1483 | predict_edge_def (e, PRED_CALL, NOT_TAKEN); | |
1484 | break; | |
1485 | } | |
1486 | } | |
1487 | } | |
1488 | } | |
1489 | tree_predict_by_opcode (bb); | |
1490 | } | |
1491 | FOR_EACH_BB (bb) | |
10d22567 | 1492 | combine_predictions_for_bb (bb); |
861f9cd0 | 1493 | |
f06b0a10 ZD |
1494 | #ifdef ENABLE_CHECKING |
1495 | pointer_map_traverse (bb_predictions, assert_is_empty, NULL); | |
1496 | #endif | |
1497 | pointer_map_destroy (bb_predictions); | |
1498 | bb_predictions = NULL; | |
1499 | ||
37818e7c | 1500 | strip_builtin_expect (); |
d73be268 | 1501 | estimate_bb_frequencies (); |
6de9cd9a | 1502 | free_dominance_info (CDI_POST_DOMINATORS); |
6809cbf9 | 1503 | remove_fake_exit_edges (); |
598ec7bd | 1504 | loop_optimizer_finalize (); |
6de9cd9a | 1505 | if (dump_file && (dump_flags & TDF_DETAILS)) |
726a989a | 1506 | gimple_dump_cfg (dump_file, dump_flags); |
878f99d2 JH |
1507 | if (profile_status == PROFILE_ABSENT) |
1508 | profile_status = PROFILE_GUESSED; | |
c2924966 | 1509 | return 0; |
f1ebdfc5 | 1510 | } |
994a57cd | 1511 | \f |
fa10beec | 1512 | /* Predict edges to successors of CUR whose sources are not postdominated by |
3e4b9ad0 | 1513 | BB by PRED and recurse to all postdominators. */ |
bb033fd8 JH |
1514 | |
1515 | static void | |
3e4b9ad0 JH |
1516 | predict_paths_for_bb (basic_block cur, basic_block bb, |
1517 | enum br_predictor pred, | |
1518 | enum prediction taken) | |
bb033fd8 JH |
1519 | { |
1520 | edge e; | |
628f6a4e | 1521 | edge_iterator ei; |
3e4b9ad0 | 1522 | basic_block son; |
bb033fd8 | 1523 | |
3e4b9ad0 JH |
1524 | /* We are looking for all edges forming edge cut induced by |
1525 | set of all blocks postdominated by BB. */ | |
1526 | FOR_EACH_EDGE (e, ei, cur->preds) | |
1527 | if (e->src->index >= NUM_FIXED_BLOCKS | |
1528 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb)) | |
bb033fd8 | 1529 | { |
3e4b9ad0 JH |
1530 | gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb)); |
1531 | predict_edge_def (e, pred, taken); | |
bb033fd8 | 1532 | } |
3e4b9ad0 JH |
1533 | for (son = first_dom_son (CDI_POST_DOMINATORS, cur); |
1534 | son; | |
1535 | son = next_dom_son (CDI_POST_DOMINATORS, son)) | |
1536 | predict_paths_for_bb (son, bb, pred, taken); | |
1537 | } | |
bb033fd8 | 1538 | |
3e4b9ad0 JH |
1539 | /* Sets branch probabilities according to PREDiction and |
1540 | FLAGS. */ | |
bb033fd8 | 1541 | |
3e4b9ad0 JH |
1542 | static void |
1543 | predict_paths_leading_to (basic_block bb, enum br_predictor pred, | |
1544 | enum prediction taken) | |
1545 | { | |
1546 | predict_paths_for_bb (bb, bb, pred, taken); | |
bb033fd8 | 1547 | } |
969d70ca | 1548 | \f |
57cb6d52 | 1549 | /* This is used to carry information about basic blocks. It is |
861f9cd0 JH |
1550 | attached to the AUX field of the standard CFG block. */ |
1551 | ||
1552 | typedef struct block_info_def | |
1553 | { | |
1554 | /* Estimated frequency of execution of basic_block. */ | |
ac5e69da | 1555 | sreal frequency; |
861f9cd0 JH |
1556 | |
1557 | /* To keep queue of basic blocks to process. */ | |
1558 | basic_block next; | |
1559 | ||
eaec9b3d | 1560 | /* Number of predecessors we need to visit first. */ |
754d9299 | 1561 | int npredecessors; |
861f9cd0 JH |
1562 | } *block_info; |
1563 | ||
1564 | /* Similar information for edges. */ | |
1565 | typedef struct edge_info_def | |
1566 | { | |
569b7f6a | 1567 | /* In case edge is a loopback edge, the probability edge will be reached |
861f9cd0 | 1568 | in case header is. Estimated number of iterations of the loop can be |
8aa18a7d | 1569 | then computed as 1 / (1 - back_edge_prob). */ |
ac5e69da | 1570 | sreal back_edge_prob; |
569b7f6a | 1571 | /* True if the edge is a loopback edge in the natural loop. */ |
2c45a16a | 1572 | unsigned int back_edge:1; |
861f9cd0 JH |
1573 | } *edge_info; |
1574 | ||
1575 | #define BLOCK_INFO(B) ((block_info) (B)->aux) | |
1576 | #define EDGE_INFO(E) ((edge_info) (E)->aux) | |
1577 | ||
1578 | /* Helper function for estimate_bb_frequencies. | |
598ec7bd ZD |
1579 | Propagate the frequencies in blocks marked in |
1580 | TOVISIT, starting in HEAD. */ | |
bfdade77 | 1581 | |
861f9cd0 | 1582 | static void |
598ec7bd | 1583 | propagate_freq (basic_block head, bitmap tovisit) |
861f9cd0 | 1584 | { |
e0082a72 ZD |
1585 | basic_block bb; |
1586 | basic_block last; | |
b9af0016 | 1587 | unsigned i; |
861f9cd0 JH |
1588 | edge e; |
1589 | basic_block nextbb; | |
8a998e0c | 1590 | bitmap_iterator bi; |
247a370b | 1591 | |
eaec9b3d | 1592 | /* For each basic block we need to visit count number of his predecessors |
247a370b | 1593 | we need to visit first. */ |
8a998e0c | 1594 | EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi) |
247a370b | 1595 | { |
8a998e0c JL |
1596 | edge_iterator ei; |
1597 | int count = 0; | |
1598 | ||
b9af0016 NS |
1599 | /* The outermost "loop" includes the exit block, which we can not |
1600 | look up via BASIC_BLOCK. Detect this and use EXIT_BLOCK_PTR | |
1601 | directly. Do the same for the entry block. */ | |
24bd1a0b | 1602 | bb = BASIC_BLOCK (i); |
bfdade77 | 1603 | |
8a998e0c JL |
1604 | FOR_EACH_EDGE (e, ei, bb->preds) |
1605 | { | |
1606 | bool visit = bitmap_bit_p (tovisit, e->src->index); | |
1607 | ||
1608 | if (visit && !(e->flags & EDGE_DFS_BACK)) | |
1609 | count++; | |
1610 | else if (visit && dump_file && !EDGE_INFO (e)->back_edge) | |
1611 | fprintf (dump_file, | |
1612 | "Irreducible region hit, ignoring edge to %i->%i\n", | |
1613 | e->src->index, bb->index); | |
247a370b | 1614 | } |
b9af0016 | 1615 | BLOCK_INFO (bb)->npredecessors = count; |
247a370b | 1616 | } |
861f9cd0 | 1617 | |
8aa18a7d | 1618 | memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one)); |
e0082a72 ZD |
1619 | last = head; |
1620 | for (bb = head; bb; bb = nextbb) | |
861f9cd0 | 1621 | { |
628f6a4e | 1622 | edge_iterator ei; |
ac5e69da | 1623 | sreal cyclic_probability, frequency; |
8aa18a7d JH |
1624 | |
1625 | memcpy (&cyclic_probability, &real_zero, sizeof (real_zero)); | |
1626 | memcpy (&frequency, &real_zero, sizeof (real_zero)); | |
861f9cd0 JH |
1627 | |
1628 | nextbb = BLOCK_INFO (bb)->next; | |
1629 | BLOCK_INFO (bb)->next = NULL; | |
1630 | ||
1631 | /* Compute frequency of basic block. */ | |
1632 | if (bb != head) | |
1633 | { | |
247a370b | 1634 | #ifdef ENABLE_CHECKING |
628f6a4e | 1635 | FOR_EACH_EDGE (e, ei, bb->preds) |
e16acfcd NS |
1636 | gcc_assert (!bitmap_bit_p (tovisit, e->src->index) |
1637 | || (e->flags & EDGE_DFS_BACK)); | |
247a370b | 1638 | #endif |
861f9cd0 | 1639 | |
628f6a4e | 1640 | FOR_EACH_EDGE (e, ei, bb->preds) |
861f9cd0 | 1641 | if (EDGE_INFO (e)->back_edge) |
8aa18a7d | 1642 | { |
ac5e69da JZ |
1643 | sreal_add (&cyclic_probability, &cyclic_probability, |
1644 | &EDGE_INFO (e)->back_edge_prob); | |
8aa18a7d | 1645 | } |
247a370b | 1646 | else if (!(e->flags & EDGE_DFS_BACK)) |
8aa18a7d | 1647 | { |
ac5e69da | 1648 | sreal tmp; |
8aa18a7d JH |
1649 | |
1650 | /* frequency += (e->probability | |
1651 | * BLOCK_INFO (e->src)->frequency / | |
1652 | REG_BR_PROB_BASE); */ | |
1653 | ||
ac5e69da JZ |
1654 | sreal_init (&tmp, e->probability, 0); |
1655 | sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency); | |
1656 | sreal_mul (&tmp, &tmp, &real_inv_br_prob_base); | |
1657 | sreal_add (&frequency, &frequency, &tmp); | |
8aa18a7d JH |
1658 | } |
1659 | ||
ac5e69da JZ |
1660 | if (sreal_compare (&cyclic_probability, &real_zero) == 0) |
1661 | { | |
1662 | memcpy (&BLOCK_INFO (bb)->frequency, &frequency, | |
1663 | sizeof (frequency)); | |
1664 | } | |
fbe3b30b SB |
1665 | else |
1666 | { | |
ac5e69da JZ |
1667 | if (sreal_compare (&cyclic_probability, &real_almost_one) > 0) |
1668 | { | |
1669 | memcpy (&cyclic_probability, &real_almost_one, | |
1670 | sizeof (real_almost_one)); | |
1671 | } | |
861f9cd0 | 1672 | |
79a490a9 | 1673 | /* BLOCK_INFO (bb)->frequency = frequency |
ac5e69da | 1674 | / (1 - cyclic_probability) */ |
861f9cd0 | 1675 | |
ac5e69da JZ |
1676 | sreal_sub (&cyclic_probability, &real_one, &cyclic_probability); |
1677 | sreal_div (&BLOCK_INFO (bb)->frequency, | |
1678 | &frequency, &cyclic_probability); | |
fbe3b30b | 1679 | } |
861f9cd0 JH |
1680 | } |
1681 | ||
8a998e0c | 1682 | bitmap_clear_bit (tovisit, bb->index); |
861f9cd0 | 1683 | |
9ff3d2de JL |
1684 | e = find_edge (bb, head); |
1685 | if (e) | |
1686 | { | |
1687 | sreal tmp; | |
628f6a4e | 1688 | |
9ff3d2de JL |
1689 | /* EDGE_INFO (e)->back_edge_prob |
1690 | = ((e->probability * BLOCK_INFO (bb)->frequency) | |
1691 | / REG_BR_PROB_BASE); */ | |
628f6a4e | 1692 | |
9ff3d2de JL |
1693 | sreal_init (&tmp, e->probability, 0); |
1694 | sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency); | |
1695 | sreal_mul (&EDGE_INFO (e)->back_edge_prob, | |
1696 | &tmp, &real_inv_br_prob_base); | |
1697 | } | |
861f9cd0 | 1698 | |
57cb6d52 | 1699 | /* Propagate to successor blocks. */ |
628f6a4e | 1700 | FOR_EACH_EDGE (e, ei, bb->succs) |
247a370b | 1701 | if (!(e->flags & EDGE_DFS_BACK) |
754d9299 | 1702 | && BLOCK_INFO (e->dest)->npredecessors) |
861f9cd0 | 1703 | { |
754d9299 JM |
1704 | BLOCK_INFO (e->dest)->npredecessors--; |
1705 | if (!BLOCK_INFO (e->dest)->npredecessors) | |
247a370b JH |
1706 | { |
1707 | if (!nextbb) | |
1708 | nextbb = e->dest; | |
1709 | else | |
1710 | BLOCK_INFO (last)->next = e->dest; | |
628f6a4e | 1711 | |
247a370b JH |
1712 | last = e->dest; |
1713 | } | |
628f6a4e | 1714 | } |
861f9cd0 JH |
1715 | } |
1716 | } | |
1717 | ||
57cb6d52 | 1718 | /* Estimate probabilities of loopback edges in loops at same nest level. */ |
bfdade77 | 1719 | |
861f9cd0 | 1720 | static void |
598ec7bd | 1721 | estimate_loops_at_level (struct loop *first_loop) |
861f9cd0 | 1722 | { |
2ecfd709 | 1723 | struct loop *loop; |
861f9cd0 JH |
1724 | |
1725 | for (loop = first_loop; loop; loop = loop->next) | |
1726 | { | |
861f9cd0 | 1727 | edge e; |
2ecfd709 | 1728 | basic_block *bbs; |
3d436d2a | 1729 | unsigned i; |
598ec7bd | 1730 | bitmap tovisit = BITMAP_ALLOC (NULL); |
861f9cd0 | 1731 | |
598ec7bd | 1732 | estimate_loops_at_level (loop->inner); |
79a490a9 | 1733 | |
598ec7bd ZD |
1734 | /* Find current loop back edge and mark it. */ |
1735 | e = loop_latch_edge (loop); | |
1736 | EDGE_INFO (e)->back_edge = 1; | |
2ecfd709 ZD |
1737 | |
1738 | bbs = get_loop_body (loop); | |
1739 | for (i = 0; i < loop->num_nodes; i++) | |
8a998e0c | 1740 | bitmap_set_bit (tovisit, bbs[i]->index); |
2ecfd709 | 1741 | free (bbs); |
598ec7bd ZD |
1742 | propagate_freq (loop->header, tovisit); |
1743 | BITMAP_FREE (tovisit); | |
861f9cd0 JH |
1744 | } |
1745 | } | |
1746 | ||
2f8e468b | 1747 | /* Propagates frequencies through structure of loops. */ |
598ec7bd ZD |
1748 | |
1749 | static void | |
d73be268 | 1750 | estimate_loops (void) |
598ec7bd ZD |
1751 | { |
1752 | bitmap tovisit = BITMAP_ALLOC (NULL); | |
1753 | basic_block bb; | |
1754 | ||
1755 | /* Start by estimating the frequencies in the loops. */ | |
d51157de | 1756 | if (number_of_loops () > 1) |
d73be268 | 1757 | estimate_loops_at_level (current_loops->tree_root->inner); |
598ec7bd ZD |
1758 | |
1759 | /* Now propagate the frequencies through all the blocks. */ | |
1760 | FOR_ALL_BB (bb) | |
1761 | { | |
1762 | bitmap_set_bit (tovisit, bb->index); | |
1763 | } | |
1764 | propagate_freq (ENTRY_BLOCK_PTR, tovisit); | |
1765 | BITMAP_FREE (tovisit); | |
1766 | } | |
1767 | ||
02307675 R |
1768 | /* Convert counts measured by profile driven feedback to frequencies. |
1769 | Return nonzero iff there was any nonzero execution count. */ | |
bfdade77 | 1770 | |
bbd236a1 | 1771 | int |
79a490a9 | 1772 | counts_to_freqs (void) |
861f9cd0 | 1773 | { |
02307675 | 1774 | gcov_type count_max, true_count_max = 0; |
e0082a72 | 1775 | basic_block bb; |
0b17ab2f | 1776 | |
e0082a72 | 1777 | FOR_EACH_BB (bb) |
02307675 | 1778 | true_count_max = MAX (bb->count, true_count_max); |
861f9cd0 | 1779 | |
02307675 | 1780 | count_max = MAX (true_count_max, 1); |
e0082a72 ZD |
1781 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
1782 | bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max; | |
6bad2617 | 1783 | |
02307675 | 1784 | return true_count_max; |
861f9cd0 JH |
1785 | } |
1786 | ||
bfdade77 RK |
1787 | /* Return true if function is likely to be expensive, so there is no point to |
1788 | optimize performance of prologue, epilogue or do inlining at the expense | |
d55d8fc7 | 1789 | of code size growth. THRESHOLD is the limit of number of instructions |
bfdade77 RK |
1790 | function can execute at average to be still considered not expensive. */ |
1791 | ||
6ab16dd9 | 1792 | bool |
79a490a9 | 1793 | expensive_function_p (int threshold) |
6ab16dd9 JH |
1794 | { |
1795 | unsigned int sum = 0; | |
e0082a72 | 1796 | basic_block bb; |
5197bd50 | 1797 | unsigned int limit; |
6ab16dd9 JH |
1798 | |
1799 | /* We can not compute accurately for large thresholds due to scaled | |
1800 | frequencies. */ | |
e16acfcd | 1801 | gcc_assert (threshold <= BB_FREQ_MAX); |
6ab16dd9 | 1802 | |
eaec9b3d | 1803 | /* Frequencies are out of range. This either means that function contains |
6ab16dd9 JH |
1804 | internal loop executing more than BB_FREQ_MAX times or profile feedback |
1805 | is available and function has not been executed at all. */ | |
1806 | if (ENTRY_BLOCK_PTR->frequency == 0) | |
1807 | return true; | |
6a4d6760 | 1808 | |
6ab16dd9 JH |
1809 | /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */ |
1810 | limit = ENTRY_BLOCK_PTR->frequency * threshold; | |
e0082a72 | 1811 | FOR_EACH_BB (bb) |
6ab16dd9 | 1812 | { |
6ab16dd9 JH |
1813 | rtx insn; |
1814 | ||
a813c111 | 1815 | for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); |
6ab16dd9 | 1816 | insn = NEXT_INSN (insn)) |
bfdade77 RK |
1817 | if (active_insn_p (insn)) |
1818 | { | |
1819 | sum += bb->frequency; | |
1820 | if (sum > limit) | |
1821 | return true; | |
6ab16dd9 JH |
1822 | } |
1823 | } | |
bfdade77 | 1824 | |
6ab16dd9 JH |
1825 | return false; |
1826 | } | |
1827 | ||
861f9cd0 | 1828 | /* Estimate basic blocks frequency by given branch probabilities. */ |
bfdade77 | 1829 | |
45a80bb9 | 1830 | void |
d73be268 | 1831 | estimate_bb_frequencies (void) |
861f9cd0 | 1832 | { |
e0082a72 | 1833 | basic_block bb; |
ac5e69da | 1834 | sreal freq_max; |
8aa18a7d | 1835 | |
02307675 | 1836 | if (!flag_branch_probabilities || !counts_to_freqs ()) |
194734e9 | 1837 | { |
c4f6b78e RE |
1838 | static int real_values_initialized = 0; |
1839 | ||
1840 | if (!real_values_initialized) | |
1841 | { | |
85bb9c2a | 1842 | real_values_initialized = 1; |
c4f6b78e RE |
1843 | sreal_init (&real_zero, 0, 0); |
1844 | sreal_init (&real_one, 1, 0); | |
1845 | sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0); | |
1846 | sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0); | |
1847 | sreal_init (&real_one_half, 1, -1); | |
1848 | sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base); | |
1849 | sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base); | |
1850 | } | |
861f9cd0 | 1851 | |
194734e9 | 1852 | mark_dfs_back_edges (); |
194734e9 | 1853 | |
c5cbcccf | 1854 | single_succ_edge (ENTRY_BLOCK_PTR)->probability = REG_BR_PROB_BASE; |
194734e9 JH |
1855 | |
1856 | /* Set up block info for each basic block. */ | |
1857 | alloc_aux_for_blocks (sizeof (struct block_info_def)); | |
1858 | alloc_aux_for_edges (sizeof (struct edge_info_def)); | |
e0082a72 | 1859 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
861f9cd0 | 1860 | { |
861f9cd0 | 1861 | edge e; |
628f6a4e | 1862 | edge_iterator ei; |
194734e9 | 1863 | |
628f6a4e | 1864 | FOR_EACH_EDGE (e, ei, bb->succs) |
861f9cd0 | 1865 | { |
ac5e69da JZ |
1866 | sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0); |
1867 | sreal_mul (&EDGE_INFO (e)->back_edge_prob, | |
1868 | &EDGE_INFO (e)->back_edge_prob, | |
1869 | &real_inv_br_prob_base); | |
861f9cd0 | 1870 | } |
861f9cd0 | 1871 | } |
bfdade77 | 1872 | |
194734e9 JH |
1873 | /* First compute probabilities locally for each loop from innermost |
1874 | to outermost to examine probabilities for back edges. */ | |
d73be268 | 1875 | estimate_loops (); |
861f9cd0 | 1876 | |
194734e9 | 1877 | memcpy (&freq_max, &real_zero, sizeof (real_zero)); |
e0082a72 | 1878 | FOR_EACH_BB (bb) |
ac5e69da JZ |
1879 | if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0) |
1880 | memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max)); | |
fbe3b30b | 1881 | |
ac5e69da | 1882 | sreal_div (&freq_max, &real_bb_freq_max, &freq_max); |
e0082a72 | 1883 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
8aa18a7d | 1884 | { |
ac5e69da | 1885 | sreal tmp; |
bfdade77 | 1886 | |
ac5e69da JZ |
1887 | sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max); |
1888 | sreal_add (&tmp, &tmp, &real_one_half); | |
1889 | bb->frequency = sreal_to_int (&tmp); | |
194734e9 | 1890 | } |
bfdade77 | 1891 | |
194734e9 JH |
1892 | free_aux_for_blocks (); |
1893 | free_aux_for_edges (); | |
1894 | } | |
1895 | compute_function_frequency (); | |
1896 | if (flag_reorder_functions) | |
1897 | choose_function_section (); | |
1898 | } | |
861f9cd0 | 1899 | |
194734e9 JH |
1900 | /* Decide whether function is hot, cold or unlikely executed. */ |
1901 | static void | |
79a490a9 | 1902 | compute_function_frequency (void) |
194734e9 | 1903 | { |
e0082a72 ZD |
1904 | basic_block bb; |
1905 | ||
cdb23767 | 1906 | if (!profile_info || !flag_branch_probabilities) |
52bf96d2 JH |
1907 | { |
1908 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)) | |
1909 | != NULL) | |
1910 | cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED; | |
1911 | else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl)) | |
1912 | != NULL) | |
1913 | cfun->function_frequency = FUNCTION_FREQUENCY_HOT; | |
1914 | return; | |
1915 | } | |
194734e9 | 1916 | cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED; |
e0082a72 | 1917 | FOR_EACH_BB (bb) |
861f9cd0 | 1918 | { |
194734e9 JH |
1919 | if (maybe_hot_bb_p (bb)) |
1920 | { | |
1921 | cfun->function_frequency = FUNCTION_FREQUENCY_HOT; | |
1922 | return; | |
1923 | } | |
1924 | if (!probably_never_executed_bb_p (bb)) | |
1925 | cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL; | |
861f9cd0 | 1926 | } |
194734e9 | 1927 | } |
861f9cd0 | 1928 | |
194734e9 JH |
1929 | /* Choose appropriate section for the function. */ |
1930 | static void | |
79a490a9 | 1931 | choose_function_section (void) |
194734e9 JH |
1932 | { |
1933 | if (DECL_SECTION_NAME (current_function_decl) | |
c07f146f JH |
1934 | || !targetm.have_named_sections |
1935 | /* Theoretically we can split the gnu.linkonce text section too, | |
79a490a9 | 1936 | but this requires more work as the frequency needs to match |
c07f146f JH |
1937 | for all generated objects so we need to merge the frequency |
1938 | of all instances. For now just never set frequency for these. */ | |
c728da61 | 1939 | || DECL_ONE_ONLY (current_function_decl)) |
194734e9 | 1940 | return; |
9fb32434 CT |
1941 | |
1942 | /* If we are doing the partitioning optimization, let the optimization | |
1943 | choose the correct section into which to put things. */ | |
1944 | ||
1945 | if (flag_reorder_blocks_and_partition) | |
1946 | return; | |
1947 | ||
194734e9 JH |
1948 | if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT) |
1949 | DECL_SECTION_NAME (current_function_decl) = | |
1950 | build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME); | |
1951 | if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED) | |
1952 | DECL_SECTION_NAME (current_function_decl) = | |
1953 | build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME), | |
1954 | UNLIKELY_EXECUTED_TEXT_SECTION_NAME); | |
861f9cd0 | 1955 | } |
6de9cd9a | 1956 | |
a00d11f0 JH |
1957 | static bool |
1958 | gate_estimate_probability (void) | |
1959 | { | |
1960 | return flag_guess_branch_prob; | |
1961 | } | |
6de9cd9a | 1962 | |
2e28e797 JH |
1963 | /* Build PREDICT_EXPR. */ |
1964 | tree | |
1965 | build_predict_expr (enum br_predictor predictor, enum prediction taken) | |
1966 | { | |
9d7e5c4d MM |
1967 | tree t = build1 (PREDICT_EXPR, void_type_node, |
1968 | build_int_cst (NULL, predictor)); | |
2e28e797 JH |
1969 | PREDICT_EXPR_OUTCOME (t) = taken; |
1970 | return t; | |
1971 | } | |
1972 | ||
1973 | const char * | |
1974 | predictor_name (enum br_predictor predictor) | |
1975 | { | |
1976 | return predictor_info[predictor].name; | |
1977 | } | |
1978 | ||
8ddbbcae | 1979 | struct gimple_opt_pass pass_profile = |
6de9cd9a | 1980 | { |
8ddbbcae JH |
1981 | { |
1982 | GIMPLE_PASS, | |
6de9cd9a | 1983 | "profile", /* name */ |
a00d11f0 | 1984 | gate_estimate_probability, /* gate */ |
6de9cd9a DN |
1985 | tree_estimate_probability, /* execute */ |
1986 | NULL, /* sub */ | |
1987 | NULL, /* next */ | |
1988 | 0, /* static_pass_number */ | |
1989 | TV_BRANCH_PROB, /* tv_id */ | |
1990 | PROP_cfg, /* properties_required */ | |
1991 | 0, /* properties_provided */ | |
1992 | 0, /* properties_destroyed */ | |
1993 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
1994 | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */ |
1995 | } | |
6de9cd9a | 1996 | }; |