]>
Commit | Line | Data |
---|---|---|
f1ebdfc5 | 1 | /* Branch prediction routines for the GNU compiler. |
2f89bbc1 | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004 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 | |
8 | Software Foundation; either version 2, or (at your option) any later | |
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 RK |
16 | You should have received a copy of the GNU General Public License |
17 | along with GCC; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
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" | |
8aa18a7d | 62 | |
fbe3b30b SB |
63 | /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, |
64 | 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */ | |
ac5e69da JZ |
65 | static sreal real_zero, real_one, real_almost_one, real_br_prob_base, |
66 | real_inv_br_prob_base, real_one_half, real_bb_freq_max; | |
f1ebdfc5 | 67 | |
c66f079e | 68 | /* Random guesstimation given names. */ |
c66f079e | 69 | #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1) |
c66f079e | 70 | #define PROB_EVEN (REG_BR_PROB_BASE / 2) |
c66f079e RH |
71 | #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY) |
72 | #define PROB_ALWAYS (REG_BR_PROB_BASE) | |
f1ebdfc5 | 73 | |
79a490a9 | 74 | static void combine_predictions_for_insn (rtx, basic_block); |
6de9cd9a | 75 | static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int); |
79a490a9 AJ |
76 | static void estimate_loops_at_level (struct loop *loop); |
77 | static void propagate_freq (struct loop *); | |
78 | static void estimate_bb_frequencies (struct loops *); | |
bb033fd8 | 79 | static void predict_paths_leading_to (basic_block, int *, enum br_predictor, enum prediction); |
79a490a9 AJ |
80 | static bool last_basic_block_p (basic_block); |
81 | static void compute_function_frequency (void); | |
82 | static void choose_function_section (void); | |
83 | static bool can_predict_insn_p (rtx); | |
ee92cb46 | 84 | |
4db384c9 JH |
85 | /* Information we hold about each branch predictor. |
86 | Filled using information from predict.def. */ | |
bfdade77 | 87 | |
4db384c9 | 88 | struct predictor_info |
ee92cb46 | 89 | { |
8b60264b KG |
90 | const char *const name; /* Name used in the debugging dumps. */ |
91 | const int hitrate; /* Expected hitrate used by | |
92 | predict_insn_def call. */ | |
93 | const int flags; | |
4db384c9 | 94 | }; |
ee92cb46 | 95 | |
134d3a2e JH |
96 | /* Use given predictor without Dempster-Shaffer theory if it matches |
97 | using first_match heuristics. */ | |
98 | #define PRED_FLAG_FIRST_MATCH 1 | |
99 | ||
100 | /* Recompute hitrate in percent to our representation. */ | |
101 | ||
bfdade77 | 102 | #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100) |
134d3a2e JH |
103 | |
104 | #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS}, | |
bfdade77 | 105 | static const struct predictor_info predictor_info[]= { |
4db384c9 JH |
106 | #include "predict.def" |
107 | ||
dc297297 | 108 | /* Upper bound on predictors. */ |
134d3a2e | 109 | {NULL, 0, 0} |
4db384c9 JH |
110 | }; |
111 | #undef DEF_PREDICTOR | |
194734e9 JH |
112 | |
113 | /* Return true in case BB can be CPU intensive and should be optimized | |
d55d8fc7 | 114 | for maximal performance. */ |
194734e9 JH |
115 | |
116 | bool | |
79a490a9 | 117 | maybe_hot_bb_p (basic_block bb) |
194734e9 | 118 | { |
cdb23767 | 119 | if (profile_info && flag_branch_probabilities |
194734e9 | 120 | && (bb->count |
cdb23767 | 121 | < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION))) |
194734e9 JH |
122 | return false; |
123 | if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)) | |
124 | return false; | |
125 | return true; | |
126 | } | |
127 | ||
128 | /* Return true in case BB is cold and should be optimized for size. */ | |
129 | ||
130 | bool | |
79a490a9 | 131 | probably_cold_bb_p (basic_block bb) |
194734e9 | 132 | { |
cdb23767 | 133 | if (profile_info && flag_branch_probabilities |
194734e9 | 134 | && (bb->count |
cdb23767 | 135 | < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION))) |
194734e9 JH |
136 | return true; |
137 | if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)) | |
138 | return true; | |
139 | return false; | |
140 | } | |
141 | ||
142 | /* Return true in case BB is probably never executed. */ | |
143 | bool | |
79a490a9 | 144 | probably_never_executed_bb_p (basic_block bb) |
194734e9 | 145 | { |
cdb23767 NS |
146 | if (profile_info && flag_branch_probabilities) |
147 | return ((bb->count + profile_info->runs / 2) / profile_info->runs) == 0; | |
194734e9 JH |
148 | return false; |
149 | } | |
150 | ||
969d70ca JH |
151 | /* Return true if the one of outgoing edges is already predicted by |
152 | PREDICTOR. */ | |
153 | ||
6de9cd9a DN |
154 | bool |
155 | rtl_predicted_by_p (basic_block bb, enum br_predictor predictor) | |
969d70ca JH |
156 | { |
157 | rtx note; | |
a813c111 | 158 | if (!INSN_P (BB_END (bb))) |
969d70ca | 159 | return false; |
a813c111 | 160 | for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1)) |
969d70ca JH |
161 | if (REG_NOTE_KIND (note) == REG_BR_PRED |
162 | && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor) | |
163 | return true; | |
164 | return false; | |
165 | } | |
ee92cb46 | 166 | |
6de9cd9a DN |
167 | /* Return true if the one of outgoing edges is already predicted by |
168 | PREDICTOR. */ | |
169 | ||
170 | bool | |
171 | tree_predicted_by_p (basic_block bb, enum br_predictor predictor) | |
172 | { | |
173 | struct edge_prediction *i = bb_ann (bb)->predictions; | |
174 | for (i = bb_ann (bb)->predictions; i; i = i->next) | |
175 | if (i->predictor == predictor) | |
176 | return true; | |
177 | return false; | |
178 | } | |
179 | ||
4db384c9 | 180 | void |
79a490a9 | 181 | predict_insn (rtx insn, enum br_predictor predictor, int probability) |
4db384c9 | 182 | { |
8127d0e0 NS |
183 | if (!any_condjump_p (insn)) |
184 | abort (); | |
d50672ef JH |
185 | if (!flag_guess_branch_prob) |
186 | return; | |
bfdade77 | 187 | |
ee92cb46 | 188 | REG_NOTES (insn) |
4db384c9 JH |
189 | = gen_rtx_EXPR_LIST (REG_BR_PRED, |
190 | gen_rtx_CONCAT (VOIDmode, | |
191 | GEN_INT ((int) predictor), | |
192 | GEN_INT ((int) probability)), | |
193 | REG_NOTES (insn)); | |
194 | } | |
195 | ||
196 | /* Predict insn by given predictor. */ | |
bfdade77 | 197 | |
4db384c9 | 198 | void |
79a490a9 AJ |
199 | predict_insn_def (rtx insn, enum br_predictor predictor, |
200 | enum prediction taken) | |
4db384c9 JH |
201 | { |
202 | int probability = predictor_info[(int) predictor].hitrate; | |
bfdade77 | 203 | |
4db384c9 JH |
204 | if (taken != TAKEN) |
205 | probability = REG_BR_PROB_BASE - probability; | |
bfdade77 | 206 | |
4db384c9 | 207 | predict_insn (insn, predictor, probability); |
ee92cb46 JH |
208 | } |
209 | ||
210 | /* Predict edge E with given probability if possible. */ | |
bfdade77 | 211 | |
4db384c9 | 212 | void |
6de9cd9a | 213 | rtl_predict_edge (edge e, enum br_predictor predictor, int probability) |
ee92cb46 JH |
214 | { |
215 | rtx last_insn; | |
a813c111 | 216 | last_insn = BB_END (e->src); |
ee92cb46 JH |
217 | |
218 | /* We can store the branch prediction information only about | |
219 | conditional jumps. */ | |
220 | if (!any_condjump_p (last_insn)) | |
221 | return; | |
222 | ||
223 | /* We always store probability of branching. */ | |
224 | if (e->flags & EDGE_FALLTHRU) | |
225 | probability = REG_BR_PROB_BASE - probability; | |
226 | ||
4db384c9 JH |
227 | predict_insn (last_insn, predictor, probability); |
228 | } | |
229 | ||
6de9cd9a DN |
230 | /* Predict edge E with the given PROBABILITY. */ |
231 | void | |
232 | tree_predict_edge (edge e, enum br_predictor predictor, int probability) | |
233 | { | |
234 | struct edge_prediction *i = ggc_alloc (sizeof (struct edge_prediction)); | |
235 | ||
236 | i->next = bb_ann (e->src)->predictions; | |
237 | bb_ann (e->src)->predictions = i; | |
238 | i->probability = probability; | |
239 | i->predictor = predictor; | |
240 | i->edge = e; | |
241 | } | |
242 | ||
2ffa9932 JH |
243 | /* Return true when we can store prediction on insn INSN. |
244 | At the moment we represent predictions only on conditional | |
245 | jumps, not at computed jump or other complicated cases. */ | |
246 | static bool | |
79a490a9 | 247 | can_predict_insn_p (rtx insn) |
2ffa9932 | 248 | { |
4b4bf941 | 249 | return (JUMP_P (insn) |
2ffa9932 JH |
250 | && any_condjump_p (insn) |
251 | && BLOCK_FOR_INSN (insn)->succ->succ_next); | |
252 | } | |
253 | ||
4db384c9 | 254 | /* Predict edge E by given predictor if possible. */ |
bfdade77 | 255 | |
4db384c9 | 256 | void |
79a490a9 AJ |
257 | predict_edge_def (edge e, enum br_predictor predictor, |
258 | enum prediction taken) | |
4db384c9 JH |
259 | { |
260 | int probability = predictor_info[(int) predictor].hitrate; | |
261 | ||
262 | if (taken != TAKEN) | |
263 | probability = REG_BR_PROB_BASE - probability; | |
bfdade77 | 264 | |
4db384c9 JH |
265 | predict_edge (e, predictor, probability); |
266 | } | |
267 | ||
268 | /* Invert all branch predictions or probability notes in the INSN. This needs | |
269 | to be done each time we invert the condition used by the jump. */ | |
bfdade77 | 270 | |
4db384c9 | 271 | void |
79a490a9 | 272 | invert_br_probabilities (rtx insn) |
4db384c9 | 273 | { |
bfdade77 RK |
274 | rtx note; |
275 | ||
276 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | |
277 | if (REG_NOTE_KIND (note) == REG_BR_PROB) | |
278 | XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0))); | |
279 | else if (REG_NOTE_KIND (note) == REG_BR_PRED) | |
280 | XEXP (XEXP (note, 0), 1) | |
281 | = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1))); | |
4db384c9 JH |
282 | } |
283 | ||
284 | /* Dump information about the branch prediction to the output file. */ | |
bfdade77 | 285 | |
4db384c9 | 286 | static void |
6de9cd9a | 287 | dump_prediction (FILE *file, enum br_predictor predictor, int probability, |
79a490a9 | 288 | basic_block bb, int used) |
4db384c9 JH |
289 | { |
290 | edge e = bb->succ; | |
291 | ||
6de9cd9a | 292 | if (!file) |
4db384c9 JH |
293 | return; |
294 | ||
fbc2782e | 295 | while (e && (e->flags & EDGE_FALLTHRU)) |
4db384c9 JH |
296 | e = e->succ_next; |
297 | ||
6de9cd9a | 298 | fprintf (file, " %s heuristics%s: %.1f%%", |
4db384c9 | 299 | predictor_info[predictor].name, |
bfdade77 | 300 | used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE); |
4db384c9 JH |
301 | |
302 | if (bb->count) | |
25c3a4ef | 303 | { |
6de9cd9a DN |
304 | fprintf (file, " exec "); |
305 | fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count); | |
fbc2782e DD |
306 | if (e) |
307 | { | |
6de9cd9a DN |
308 | fprintf (file, " hit "); |
309 | fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count); | |
310 | fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count); | |
fbc2782e | 311 | } |
25c3a4ef | 312 | } |
bfdade77 | 313 | |
6de9cd9a | 314 | fprintf (file, "\n"); |
4db384c9 JH |
315 | } |
316 | ||
229031d0 | 317 | /* We can not predict the probabilities of outgoing edges of bb. Set them |
87022a6b JH |
318 | evenly and hope for the best. */ |
319 | static void | |
320 | set_even_probabilities (basic_block bb) | |
321 | { | |
322 | int nedges = 0; | |
323 | edge e; | |
324 | ||
325 | for (e = bb->succ; e; e = e->succ_next) | |
326 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) | |
327 | nedges ++; | |
328 | for (e = bb->succ; e; e = e->succ_next) | |
329 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) | |
330 | e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges; | |
331 | else | |
332 | e->probability = 0; | |
333 | } | |
334 | ||
4db384c9 JH |
335 | /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB |
336 | note if not already present. Remove now useless REG_BR_PRED notes. */ | |
bfdade77 | 337 | |
4db384c9 | 338 | static void |
79a490a9 | 339 | combine_predictions_for_insn (rtx insn, basic_block bb) |
4db384c9 | 340 | { |
87022a6b JH |
341 | rtx prob_note; |
342 | rtx *pnote; | |
bfdade77 | 343 | rtx note; |
4db384c9 JH |
344 | int best_probability = PROB_EVEN; |
345 | int best_predictor = END_PREDICTORS; | |
134d3a2e JH |
346 | int combined_probability = REG_BR_PROB_BASE / 2; |
347 | int d; | |
d195b46f JH |
348 | bool first_match = false; |
349 | bool found = false; | |
4db384c9 | 350 | |
87022a6b JH |
351 | if (!can_predict_insn_p (insn)) |
352 | { | |
353 | set_even_probabilities (bb); | |
354 | return; | |
355 | } | |
356 | ||
357 | prob_note = find_reg_note (insn, REG_BR_PROB, 0); | |
358 | pnote = ®_NOTES (insn); | |
c263766c RH |
359 | if (dump_file) |
360 | fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn), | |
0b17ab2f | 361 | bb->index); |
4db384c9 JH |
362 | |
363 | /* We implement "first match" heuristics and use probability guessed | |
6de9cd9a | 364 | by predictor with smallest index. */ |
bfdade77 RK |
365 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) |
366 | if (REG_NOTE_KIND (note) == REG_BR_PRED) | |
367 | { | |
368 | int predictor = INTVAL (XEXP (XEXP (note, 0), 0)); | |
369 | int probability = INTVAL (XEXP (XEXP (note, 0), 1)); | |
370 | ||
371 | found = true; | |
372 | if (best_predictor > predictor) | |
373 | best_probability = probability, best_predictor = predictor; | |
374 | ||
375 | d = (combined_probability * probability | |
376 | + (REG_BR_PROB_BASE - combined_probability) | |
377 | * (REG_BR_PROB_BASE - probability)); | |
378 | ||
379 | /* Use FP math to avoid overflows of 32bit integers. */ | |
571a03b8 JJ |
380 | if (d == 0) |
381 | /* If one probability is 0% and one 100%, avoid division by zero. */ | |
382 | combined_probability = REG_BR_PROB_BASE / 2; | |
383 | else | |
384 | combined_probability = (((double) combined_probability) * probability | |
385 | * REG_BR_PROB_BASE / d + 0.5); | |
bfdade77 RK |
386 | } |
387 | ||
388 | /* Decide which heuristic to use. In case we didn't match anything, | |
389 | use no_prediction heuristic, in case we did match, use either | |
d195b46f JH |
390 | first match or Dempster-Shaffer theory depending on the flags. */ |
391 | ||
134d3a2e | 392 | if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH) |
d195b46f JH |
393 | first_match = true; |
394 | ||
395 | if (!found) | |
6de9cd9a DN |
396 | dump_prediction (dump_file, PRED_NO_PREDICTION, |
397 | combined_probability, bb, true); | |
d195b46f JH |
398 | else |
399 | { | |
6de9cd9a DN |
400 | dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, |
401 | bb, !first_match); | |
402 | dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, | |
403 | bb, first_match); | |
d195b46f JH |
404 | } |
405 | ||
406 | if (first_match) | |
134d3a2e | 407 | combined_probability = best_probability; |
6de9cd9a | 408 | dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true); |
d195b46f JH |
409 | |
410 | while (*pnote) | |
411 | { | |
412 | if (REG_NOTE_KIND (*pnote) == REG_BR_PRED) | |
413 | { | |
414 | int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0)); | |
415 | int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1)); | |
416 | ||
6de9cd9a | 417 | dump_prediction (dump_file, predictor, probability, bb, |
d195b46f | 418 | !first_match || best_predictor == predictor); |
6a4d6760 | 419 | *pnote = XEXP (*pnote, 1); |
d195b46f JH |
420 | } |
421 | else | |
6a4d6760 | 422 | pnote = &XEXP (*pnote, 1); |
d195b46f | 423 | } |
bfdade77 | 424 | |
4db384c9 JH |
425 | if (!prob_note) |
426 | { | |
427 | REG_NOTES (insn) | |
428 | = gen_rtx_EXPR_LIST (REG_BR_PROB, | |
134d3a2e | 429 | GEN_INT (combined_probability), REG_NOTES (insn)); |
bfdade77 | 430 | |
134d3a2e JH |
431 | /* Save the prediction into CFG in case we are seeing non-degenerated |
432 | conditional jump. */ | |
433 | if (bb->succ->succ_next) | |
434 | { | |
435 | BRANCH_EDGE (bb)->probability = combined_probability; | |
bfdade77 RK |
436 | FALLTHRU_EDGE (bb)->probability |
437 | = REG_BR_PROB_BASE - combined_probability; | |
134d3a2e | 438 | } |
4db384c9 | 439 | } |
e53de54d JH |
440 | else if (bb->succ->succ_next) |
441 | { | |
442 | int prob = INTVAL (XEXP (prob_note, 0)); | |
443 | ||
444 | BRANCH_EDGE (bb)->probability = prob; | |
445 | FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob; | |
446 | } | |
447 | else | |
448 | bb->succ->probability = REG_BR_PROB_BASE; | |
ee92cb46 JH |
449 | } |
450 | ||
6de9cd9a DN |
451 | /* Combine predictions into single probability and store them into CFG. |
452 | Remove now useless prediction entries. */ | |
f1ebdfc5 | 453 | |
6de9cd9a DN |
454 | static void |
455 | combine_predictions_for_bb (FILE *file, basic_block bb) | |
f1ebdfc5 | 456 | { |
6de9cd9a DN |
457 | int best_probability = PROB_EVEN; |
458 | int best_predictor = END_PREDICTORS; | |
459 | int combined_probability = REG_BR_PROB_BASE / 2; | |
460 | int d; | |
461 | bool first_match = false; | |
462 | bool found = false; | |
463 | struct edge_prediction *pred; | |
464 | int nedges = 0; | |
465 | edge e, first = NULL, second = NULL; | |
f1ebdfc5 | 466 | |
6de9cd9a DN |
467 | for (e = bb->succ; e; e = e->succ_next) |
468 | if (!(e->flags & (EDGE_EH | EDGE_FAKE))) | |
469 | { | |
470 | nedges ++; | |
471 | if (first && !second) | |
472 | second = e; | |
473 | if (!first) | |
474 | first = e; | |
475 | } | |
476 | ||
477 | /* When there is no successor or only one choice, prediction is easy. | |
478 | ||
479 | We are lazy for now and predict only basic blocks with two outgoing | |
480 | edges. It is possible to predict generic case too, but we have to | |
481 | ignore first match heuristics and do more involved combining. Implement | |
482 | this later. */ | |
483 | if (nedges != 2) | |
484 | { | |
87022a6b JH |
485 | if (!bb->count) |
486 | set_even_probabilities (bb); | |
6de9cd9a DN |
487 | bb_ann (bb)->predictions = NULL; |
488 | if (file) | |
489 | fprintf (file, "%i edges in bb %i predicted to even probabilities\n", | |
490 | nedges, bb->index); | |
491 | return; | |
492 | } | |
493 | ||
494 | if (file) | |
495 | fprintf (file, "Predictions for bb %i\n", bb->index); | |
496 | ||
497 | /* We implement "first match" heuristics and use probability guessed | |
498 | by predictor with smallest index. */ | |
499 | for (pred = bb_ann (bb)->predictions; pred; pred = pred->next) | |
500 | { | |
501 | int predictor = pred->predictor; | |
502 | int probability = pred->probability; | |
503 | ||
504 | if (pred->edge != first) | |
505 | probability = REG_BR_PROB_BASE - probability; | |
506 | ||
507 | found = true; | |
508 | if (best_predictor > predictor) | |
509 | best_probability = probability, best_predictor = predictor; | |
510 | ||
511 | d = (combined_probability * probability | |
512 | + (REG_BR_PROB_BASE - combined_probability) | |
513 | * (REG_BR_PROB_BASE - probability)); | |
514 | ||
515 | /* Use FP math to avoid overflows of 32bit integers. */ | |
516 | if (d == 0) | |
517 | /* If one probability is 0% and one 100%, avoid division by zero. */ | |
518 | combined_probability = REG_BR_PROB_BASE / 2; | |
519 | else | |
520 | combined_probability = (((double) combined_probability) * probability | |
521 | * REG_BR_PROB_BASE / d + 0.5); | |
522 | } | |
523 | ||
524 | /* Decide which heuristic to use. In case we didn't match anything, | |
525 | use no_prediction heuristic, in case we did match, use either | |
526 | first match or Dempster-Shaffer theory depending on the flags. */ | |
527 | ||
528 | if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH) | |
529 | first_match = true; | |
530 | ||
531 | if (!found) | |
532 | dump_prediction (file, PRED_NO_PREDICTION, combined_probability, bb, true); | |
533 | else | |
534 | { | |
535 | dump_prediction (file, PRED_DS_THEORY, combined_probability, bb, | |
536 | !first_match); | |
537 | dump_prediction (file, PRED_FIRST_MATCH, best_probability, bb, | |
538 | first_match); | |
539 | } | |
540 | ||
541 | if (first_match) | |
542 | combined_probability = best_probability; | |
543 | dump_prediction (file, PRED_COMBINED, combined_probability, bb, true); | |
544 | ||
545 | for (pred = bb_ann (bb)->predictions; pred; pred = pred->next) | |
546 | { | |
547 | int predictor = pred->predictor; | |
548 | int probability = pred->probability; | |
549 | ||
550 | if (pred->edge != bb->succ) | |
551 | probability = REG_BR_PROB_BASE - probability; | |
552 | dump_prediction (file, predictor, probability, bb, | |
553 | !first_match || best_predictor == predictor); | |
554 | } | |
555 | bb_ann (bb)->predictions = NULL; | |
556 | ||
87022a6b JH |
557 | if (!bb->count) |
558 | { | |
559 | first->probability = combined_probability; | |
560 | second->probability = REG_BR_PROB_BASE - combined_probability; | |
561 | } | |
6de9cd9a DN |
562 | } |
563 | ||
564 | /* Predict edge probabilities by exploiting loop structure. | |
b6acab32 JH |
565 | When RTLSIMPLELOOPS is set, attempt to count number of iterations by analyzing |
566 | RTL otherwise use tree based approach. */ | |
6de9cd9a | 567 | static void |
b6acab32 | 568 | predict_loops (struct loops *loops_info, bool rtlsimpleloops) |
6de9cd9a DN |
569 | { |
570 | unsigned i; | |
0b92ff33 | 571 | |
b6acab32 JH |
572 | if (!rtlsimpleloops) |
573 | scev_initialize (loops_info); | |
574 | ||
65169dcf JE |
575 | /* Try to predict out blocks in a loop that are not part of a |
576 | natural loop. */ | |
2ecfd709 | 577 | for (i = 1; i < loops_info->num; i++) |
f1ebdfc5 | 578 | { |
2ecfd709 | 579 | basic_block bb, *bbs; |
3d436d2a | 580 | unsigned j; |
0dd0e980 | 581 | int exits; |
2ecfd709 | 582 | struct loop *loop = loops_info->parray[i]; |
50654f6c | 583 | struct niter_desc desc; |
3d436d2a | 584 | unsigned HOST_WIDE_INT niter; |
f1ebdfc5 | 585 | |
d47cc544 | 586 | flow_loop_scan (loop, LOOP_EXIT_EDGES); |
0dd0e980 JH |
587 | exits = loop->num_exits; |
588 | ||
b6acab32 | 589 | if (rtlsimpleloops) |
3d436d2a | 590 | { |
6de9cd9a DN |
591 | iv_analysis_loop_init (loop); |
592 | find_simple_exit (loop, &desc); | |
593 | ||
594 | if (desc.simple_p && desc.const_iter) | |
595 | { | |
596 | int prob; | |
597 | niter = desc.niter + 1; | |
598 | if (niter == 0) /* We might overflow here. */ | |
599 | niter = desc.niter; | |
600 | ||
601 | prob = (REG_BR_PROB_BASE | |
602 | - (REG_BR_PROB_BASE + niter /2) / niter); | |
603 | /* Branch prediction algorithm gives 0 frequency for everything | |
604 | after the end of loop for loop having 0 probability to finish. */ | |
605 | if (prob == REG_BR_PROB_BASE) | |
606 | prob = REG_BR_PROB_BASE - 1; | |
607 | predict_edge (desc.in_edge, PRED_LOOP_ITERATIONS, | |
608 | prob); | |
609 | } | |
3d436d2a | 610 | } |
b6acab32 JH |
611 | else |
612 | { | |
613 | edge *exits; | |
614 | unsigned j, n_exits; | |
615 | struct tree_niter_desc niter_desc; | |
616 | ||
617 | exits = get_loop_exit_edges (loop, &n_exits); | |
618 | for (j = 0; j < n_exits; j++) | |
619 | { | |
620 | tree niter = NULL; | |
621 | ||
622 | if (number_of_iterations_exit (loop, exits[j], &niter_desc)) | |
623 | niter = niter_desc.niter; | |
624 | if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST) | |
625 | niter = loop_niter_by_eval (loop, exits[j]); | |
626 | ||
627 | if (TREE_CODE (niter) == INTEGER_CST) | |
628 | { | |
629 | int probability; | |
630 | if (host_integerp (niter, 1) | |
631 | && tree_int_cst_lt (niter, | |
632 | build_int_cstu (NULL_TREE, | |
633 | REG_BR_PROB_BASE - 1))) | |
634 | { | |
635 | HOST_WIDE_INT nitercst = tree_low_cst (niter, 1) + 1; | |
636 | probability = (REG_BR_PROB_BASE + nitercst / 2) / nitercst; | |
637 | } | |
638 | else | |
639 | probability = 1; | |
640 | ||
641 | predict_edge (exits[j], PRED_LOOP_ITERATIONS, probability); | |
642 | } | |
643 | } | |
644 | ||
645 | free (exits); | |
646 | } | |
3d436d2a | 647 | |
2ecfd709 | 648 | bbs = get_loop_body (loop); |
6de9cd9a | 649 | |
2ecfd709 ZD |
650 | for (j = 0; j < loop->num_nodes; j++) |
651 | { | |
652 | int header_found = 0; | |
653 | edge e; | |
654 | ||
655 | bb = bbs[j]; | |
bfdade77 | 656 | |
969d70ca JH |
657 | /* Bypass loop heuristics on continue statement. These |
658 | statements construct loops via "non-loop" constructs | |
659 | in the source language and are better to be handled | |
660 | separately. */ | |
b6acab32 | 661 | if ((rtlsimpleloops && !can_predict_insn_p (BB_END (bb))) |
2ffa9932 | 662 | || predicted_by_p (bb, PRED_CONTINUE)) |
969d70ca JH |
663 | continue; |
664 | ||
2ecfd709 ZD |
665 | /* Loop branch heuristics - predict an edge back to a |
666 | loop's head as taken. */ | |
667 | for (e = bb->succ; e; e = e->succ_next) | |
668 | if (e->dest == loop->header | |
669 | && e->src == loop->latch) | |
670 | { | |
671 | header_found = 1; | |
672 | predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN); | |
673 | } | |
bfdade77 | 674 | |
2ecfd709 | 675 | /* Loop exit heuristics - predict an edge exiting the loop if the |
d55d8fc7 | 676 | conditional has no loop header successors as not taken. */ |
2ecfd709 ZD |
677 | if (!header_found) |
678 | for (e = bb->succ; e; e = e->succ_next) | |
679 | if (e->dest->index < 0 | |
680 | || !flow_bb_inside_loop_p (loop, e->dest)) | |
681 | predict_edge | |
682 | (e, PRED_LOOP_EXIT, | |
683 | (REG_BR_PROB_BASE | |
684 | - predictor_info [(int) PRED_LOOP_EXIT].hitrate) | |
685 | / exits); | |
686 | } | |
36579663 | 687 | |
e0a21ab9 | 688 | /* Free basic blocks from get_loop_body. */ |
36579663 | 689 | free (bbs); |
f1ebdfc5 | 690 | } |
b6acab32 JH |
691 | |
692 | if (!rtlsimpleloops) | |
693 | scev_reset (); | |
6de9cd9a DN |
694 | } |
695 | ||
87022a6b JH |
696 | /* Attempt to predict probabilities of BB outgoing edges using local |
697 | properties. */ | |
698 | static void | |
699 | bb_estimate_probability_locally (basic_block bb) | |
700 | { | |
701 | rtx last_insn = BB_END (bb); | |
702 | rtx cond; | |
703 | ||
704 | if (! can_predict_insn_p (last_insn)) | |
705 | return; | |
706 | cond = get_condition (last_insn, NULL, false, false); | |
707 | if (! cond) | |
708 | return; | |
709 | ||
710 | /* Try "pointer heuristic." | |
711 | A comparison ptr == 0 is predicted as false. | |
712 | Similarly, a comparison ptr1 == ptr2 is predicted as false. */ | |
713 | if (COMPARISON_P (cond) | |
714 | && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0))) | |
715 | || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1))))) | |
716 | { | |
717 | if (GET_CODE (cond) == EQ) | |
718 | predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN); | |
719 | else if (GET_CODE (cond) == NE) | |
720 | predict_insn_def (last_insn, PRED_POINTER, TAKEN); | |
721 | } | |
722 | else | |
723 | ||
724 | /* Try "opcode heuristic." | |
725 | EQ tests are usually false and NE tests are usually true. Also, | |
726 | most quantities are positive, so we can make the appropriate guesses | |
727 | about signed comparisons against zero. */ | |
728 | switch (GET_CODE (cond)) | |
729 | { | |
730 | case CONST_INT: | |
731 | /* Unconditional branch. */ | |
732 | predict_insn_def (last_insn, PRED_UNCONDITIONAL, | |
733 | cond == const0_rtx ? NOT_TAKEN : TAKEN); | |
734 | break; | |
735 | ||
736 | case EQ: | |
737 | case UNEQ: | |
738 | /* Floating point comparisons appears to behave in a very | |
739 | unpredictable way because of special role of = tests in | |
740 | FP code. */ | |
741 | if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0)))) | |
742 | ; | |
743 | /* Comparisons with 0 are often used for booleans and there is | |
744 | nothing useful to predict about them. */ | |
745 | else if (XEXP (cond, 1) == const0_rtx | |
746 | || XEXP (cond, 0) == const0_rtx) | |
747 | ; | |
748 | else | |
749 | predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN); | |
750 | break; | |
751 | ||
752 | case NE: | |
753 | case LTGT: | |
754 | /* Floating point comparisons appears to behave in a very | |
755 | unpredictable way because of special role of = tests in | |
756 | FP code. */ | |
757 | if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0)))) | |
758 | ; | |
759 | /* Comparisons with 0 are often used for booleans and there is | |
760 | nothing useful to predict about them. */ | |
761 | else if (XEXP (cond, 1) == const0_rtx | |
762 | || XEXP (cond, 0) == const0_rtx) | |
763 | ; | |
764 | else | |
765 | predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN); | |
766 | break; | |
767 | ||
768 | case ORDERED: | |
769 | predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN); | |
770 | break; | |
771 | ||
772 | case UNORDERED: | |
773 | predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN); | |
774 | break; | |
775 | ||
776 | case LE: | |
777 | case LT: | |
778 | if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx | |
779 | || XEXP (cond, 1) == constm1_rtx) | |
780 | predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN); | |
781 | break; | |
782 | ||
783 | case GE: | |
784 | case GT: | |
785 | if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx | |
786 | || XEXP (cond, 1) == constm1_rtx) | |
787 | predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN); | |
788 | break; | |
789 | ||
790 | default: | |
791 | break; | |
792 | } | |
793 | } | |
794 | ||
6de9cd9a DN |
795 | /* Statically estimate the probability that a branch will be taken and produce |
796 | estimated profile. When profile feedback is present never executed portions | |
797 | of function gets estimated. */ | |
798 | ||
799 | void | |
800 | estimate_probability (struct loops *loops_info) | |
801 | { | |
802 | basic_block bb; | |
803 | ||
804 | connect_infinite_loops_to_exit (); | |
805 | calculate_dominance_info (CDI_DOMINATORS); | |
806 | calculate_dominance_info (CDI_POST_DOMINATORS); | |
807 | ||
808 | predict_loops (loops_info, true); | |
f1ebdfc5 | 809 | |
50654f6c ZD |
810 | iv_analysis_done (); |
811 | ||
134d3a2e | 812 | /* Attempt to predict conditional jumps using a number of heuristics. */ |
e0082a72 | 813 | FOR_EACH_BB (bb) |
f1ebdfc5 | 814 | { |
a813c111 | 815 | rtx last_insn = BB_END (bb); |
152897b1 | 816 | edge e; |
f1ebdfc5 | 817 | |
2ffa9932 | 818 | if (! can_predict_insn_p (last_insn)) |
f1ebdfc5 | 819 | continue; |
9bcbfc52 | 820 | |
0b92ff33 JH |
821 | for (e = bb->succ; e; e = e->succ_next) |
822 | { | |
969d70ca JH |
823 | /* Predict early returns to be probable, as we've already taken |
824 | care for error returns and other are often used for fast paths | |
825 | trought function. */ | |
826 | if ((e->dest == EXIT_BLOCK_PTR | |
827 | || (e->dest->succ && !e->dest->succ->succ_next | |
828 | && e->dest->succ->dest == EXIT_BLOCK_PTR)) | |
829 | && !predicted_by_p (bb, PRED_NULL_RETURN) | |
830 | && !predicted_by_p (bb, PRED_CONST_RETURN) | |
831 | && !predicted_by_p (bb, PRED_NEGATIVE_RETURN) | |
832 | && !last_basic_block_p (e->dest)) | |
833 | predict_edge_def (e, PRED_EARLY_RETURN, TAKEN); | |
0b92ff33 | 834 | |
454ff5cb | 835 | /* Look for block we are guarding (i.e. we dominate it, |
0b92ff33 | 836 | but it doesn't postdominate us). */ |
bfdade77 | 837 | if (e->dest != EXIT_BLOCK_PTR && e->dest != bb |
d47cc544 SB |
838 | && dominated_by_p (CDI_DOMINATORS, e->dest, e->src) |
839 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest)) | |
0b92ff33 JH |
840 | { |
841 | rtx insn; | |
bfdade77 | 842 | |
0b92ff33 JH |
843 | /* The call heuristic claims that a guarded function call |
844 | is improbable. This is because such calls are often used | |
845 | to signal exceptional situations such as printing error | |
846 | messages. */ | |
a813c111 | 847 | for (insn = BB_HEAD (e->dest); insn != NEXT_INSN (BB_END (e->dest)); |
0b92ff33 | 848 | insn = NEXT_INSN (insn)) |
4b4bf941 | 849 | if (CALL_P (insn) |
0b92ff33 JH |
850 | /* Constant and pure calls are hardly used to signalize |
851 | something exceptional. */ | |
24a28584 | 852 | && ! CONST_OR_PURE_CALL_P (insn)) |
0b92ff33 JH |
853 | { |
854 | predict_edge_def (e, PRED_CALL, NOT_TAKEN); | |
855 | break; | |
856 | } | |
857 | } | |
858 | } | |
87022a6b | 859 | bb_estimate_probability_locally (bb); |
f1ebdfc5 | 860 | } |
4db384c9 JH |
861 | |
862 | /* Attach the combined probability to each conditional jump. */ | |
e0082a72 | 863 | FOR_EACH_BB (bb) |
58016611 | 864 | combine_predictions_for_insn (BB_END (bb), bb); |
6de9cd9a | 865 | |
58016611 | 866 | remove_fake_edges (); |
6de9cd9a | 867 | estimate_bb_frequencies (loops_info); |
d47cc544 | 868 | free_dominance_info (CDI_POST_DOMINATORS); |
878f99d2 JH |
869 | if (profile_status == PROFILE_ABSENT) |
870 | profile_status = PROFILE_GUESSED; | |
6de9cd9a | 871 | } |
87022a6b | 872 | |
229031d0 | 873 | /* Set edge->probability for each successor edge of BB. */ |
87022a6b JH |
874 | void |
875 | guess_outgoing_edge_probabilities (basic_block bb) | |
876 | { | |
877 | bb_estimate_probability_locally (bb); | |
878 | combine_predictions_for_insn (BB_END (bb), bb); | |
879 | } | |
6de9cd9a | 880 | \f |
42f97fd2 JH |
881 | /* Return constant EXPR will likely have at execution time, NULL if unknown. |
882 | The function is used by builtin_expect branch predictor so the evidence | |
883 | must come from this construct and additional possible constant folding. | |
884 | ||
885 | We may want to implement more involved value guess (such as value range | |
886 | propagation based prediction), but such tricks shall go to new | |
887 | implementation. */ | |
888 | ||
889 | static tree | |
890 | expr_expected_value (tree expr, bitmap visited) | |
891 | { | |
892 | if (TREE_CONSTANT (expr)) | |
893 | return expr; | |
894 | else if (TREE_CODE (expr) == SSA_NAME) | |
895 | { | |
896 | tree def = SSA_NAME_DEF_STMT (expr); | |
897 | ||
898 | /* If we were already here, break the infinite cycle. */ | |
899 | if (bitmap_bit_p (visited, SSA_NAME_VERSION (expr))) | |
900 | return NULL; | |
901 | bitmap_set_bit (visited, SSA_NAME_VERSION (expr)); | |
902 | ||
903 | if (TREE_CODE (def) == PHI_NODE) | |
904 | { | |
905 | /* All the arguments of the PHI node must have the same constant | |
906 | length. */ | |
907 | int i; | |
908 | tree val = NULL, new_val; | |
6de9cd9a | 909 | |
42f97fd2 JH |
910 | for (i = 0; i < PHI_NUM_ARGS (def); i++) |
911 | { | |
912 | tree arg = PHI_ARG_DEF (def, i); | |
913 | ||
914 | /* If this PHI has itself as an argument, we cannot | |
915 | determine the string length of this argument. However, | |
b01d837f | 916 | if we can find a expected constant value for the other |
42f97fd2 JH |
917 | PHI args then we can still be sure that this is |
918 | likely a constant. So be optimistic and just | |
919 | continue with the next argument. */ | |
920 | if (arg == PHI_RESULT (def)) | |
921 | continue; | |
922 | ||
923 | new_val = expr_expected_value (arg, visited); | |
924 | if (!new_val) | |
925 | return NULL; | |
926 | if (!val) | |
927 | val = new_val; | |
928 | else if (!operand_equal_p (val, new_val, false)) | |
929 | return NULL; | |
930 | } | |
931 | return val; | |
932 | } | |
933 | if (TREE_CODE (def) != MODIFY_EXPR || TREE_OPERAND (def, 0) != expr) | |
934 | return NULL; | |
935 | return expr_expected_value (TREE_OPERAND (def, 1), visited); | |
936 | } | |
937 | else if (TREE_CODE (expr) == CALL_EXPR) | |
938 | { | |
939 | tree decl = get_callee_fndecl (expr); | |
940 | if (!decl) | |
941 | return NULL; | |
942 | if (DECL_BUILT_IN (decl) && DECL_FUNCTION_CODE (decl) == BUILT_IN_EXPECT) | |
943 | { | |
944 | tree arglist = TREE_OPERAND (expr, 1); | |
945 | tree val; | |
946 | ||
947 | if (arglist == NULL_TREE | |
948 | || TREE_CHAIN (arglist) == NULL_TREE) | |
949 | return NULL; | |
950 | val = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (expr, 1))); | |
951 | if (TREE_CONSTANT (val)) | |
952 | return val; | |
953 | return TREE_VALUE (TREE_CHAIN (TREE_OPERAND (expr, 1))); | |
954 | } | |
955 | } | |
096759eb | 956 | if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr)) |
42f97fd2 JH |
957 | { |
958 | tree op0, op1, res; | |
959 | op0 = expr_expected_value (TREE_OPERAND (expr, 0), visited); | |
960 | if (!op0) | |
961 | return NULL; | |
962 | op1 = expr_expected_value (TREE_OPERAND (expr, 1), visited); | |
963 | if (!op1) | |
964 | return NULL; | |
965 | res = fold (build (TREE_CODE (expr), TREE_TYPE (expr), op0, op1)); | |
966 | if (TREE_CONSTANT (res)) | |
967 | return res; | |
968 | return NULL; | |
969 | } | |
096759eb | 970 | if (UNARY_CLASS_P (expr)) |
42f97fd2 JH |
971 | { |
972 | tree op0, res; | |
973 | op0 = expr_expected_value (TREE_OPERAND (expr, 0), visited); | |
974 | if (!op0) | |
975 | return NULL; | |
976 | res = fold (build1 (TREE_CODE (expr), TREE_TYPE (expr), op0)); | |
977 | if (TREE_CONSTANT (res)) | |
978 | return res; | |
979 | return NULL; | |
980 | } | |
981 | return NULL; | |
982 | } | |
983 | \f | |
984 | /* Get rid of all builtin_expect calls we no longer need. */ | |
985 | static void | |
986 | strip_builtin_expect (void) | |
987 | { | |
988 | basic_block bb; | |
989 | FOR_EACH_BB (bb) | |
990 | { | |
991 | block_stmt_iterator bi; | |
992 | for (bi = bsi_start (bb); !bsi_end_p (bi); bsi_next (&bi)) | |
993 | { | |
994 | tree stmt = bsi_stmt (bi); | |
995 | tree fndecl; | |
996 | tree arglist; | |
997 | ||
998 | if (TREE_CODE (stmt) == MODIFY_EXPR | |
999 | && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR | |
1000 | && (fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 1))) | |
1001 | && DECL_BUILT_IN (fndecl) | |
1002 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT | |
1003 | && (arglist = TREE_OPERAND (TREE_OPERAND (stmt, 1), 1)) | |
1004 | && TREE_CHAIN (arglist)) | |
1005 | { | |
1006 | TREE_OPERAND (stmt, 1) = TREE_VALUE (arglist); | |
1007 | modify_stmt (stmt); | |
1008 | } | |
1009 | } | |
1010 | } | |
1011 | } | |
1012 | \f | |
6de9cd9a DN |
1013 | /* Predict using opcode of the last statement in basic block. */ |
1014 | static void | |
1015 | tree_predict_by_opcode (basic_block bb) | |
1016 | { | |
1017 | tree stmt = last_stmt (bb); | |
1018 | edge then_edge; | |
1019 | tree cond; | |
1020 | tree op0; | |
1021 | tree type; | |
42f97fd2 JH |
1022 | tree val; |
1023 | bitmap visited; | |
6de9cd9a DN |
1024 | |
1025 | if (!stmt || TREE_CODE (stmt) != COND_EXPR) | |
1026 | return; | |
1027 | for (then_edge = bb->succ; then_edge; then_edge = then_edge->succ_next) | |
1028 | if (then_edge->flags & EDGE_TRUE_VALUE) | |
1029 | break; | |
1030 | cond = TREE_OPERAND (stmt, 0); | |
6615c446 | 1031 | if (!COMPARISON_CLASS_P (cond)) |
6de9cd9a DN |
1032 | return; |
1033 | op0 = TREE_OPERAND (cond, 0); | |
1034 | type = TREE_TYPE (op0); | |
42f97fd2 JH |
1035 | visited = BITMAP_XMALLOC (); |
1036 | val = expr_expected_value (cond, visited); | |
1037 | BITMAP_XFREE (visited); | |
1038 | if (val) | |
1039 | { | |
1040 | if (integer_zerop (val)) | |
1041 | predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, NOT_TAKEN); | |
1042 | else | |
1043 | predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, TAKEN); | |
1044 | return; | |
1045 | } | |
6de9cd9a DN |
1046 | /* Try "pointer heuristic." |
1047 | A comparison ptr == 0 is predicted as false. | |
1048 | Similarly, a comparison ptr1 == ptr2 is predicted as false. */ | |
1049 | if (POINTER_TYPE_P (type)) | |
1050 | { | |
1051 | if (TREE_CODE (cond) == EQ_EXPR) | |
1052 | predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN); | |
1053 | else if (TREE_CODE (cond) == NE_EXPR) | |
1054 | predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN); | |
1055 | } | |
1056 | else | |
1057 | ||
1058 | /* Try "opcode heuristic." | |
1059 | EQ tests are usually false and NE tests are usually true. Also, | |
1060 | most quantities are positive, so we can make the appropriate guesses | |
1061 | about signed comparisons against zero. */ | |
1062 | switch (TREE_CODE (cond)) | |
1063 | { | |
1064 | case EQ_EXPR: | |
1065 | case UNEQ_EXPR: | |
1066 | /* Floating point comparisons appears to behave in a very | |
1067 | unpredictable way because of special role of = tests in | |
1068 | FP code. */ | |
1069 | if (FLOAT_TYPE_P (type)) | |
1070 | ; | |
1071 | /* Comparisons with 0 are often used for booleans and there is | |
1072 | nothing useful to predict about them. */ | |
1073 | else if (integer_zerop (op0) | |
1074 | || integer_zerop (TREE_OPERAND (cond, 1))) | |
1075 | ; | |
1076 | else | |
1077 | predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN); | |
1078 | break; | |
1079 | ||
1080 | case NE_EXPR: | |
d1a7edaf | 1081 | case LTGT_EXPR: |
6de9cd9a DN |
1082 | /* Floating point comparisons appears to behave in a very |
1083 | unpredictable way because of special role of = tests in | |
1084 | FP code. */ | |
1085 | if (FLOAT_TYPE_P (type)) | |
1086 | ; | |
1087 | /* Comparisons with 0 are often used for booleans and there is | |
1088 | nothing useful to predict about them. */ | |
1089 | else if (integer_zerop (op0) | |
1090 | || integer_zerop (TREE_OPERAND (cond, 1))) | |
1091 | ; | |
1092 | else | |
1093 | predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN); | |
1094 | break; | |
1095 | ||
1096 | case ORDERED_EXPR: | |
1097 | predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN); | |
1098 | break; | |
1099 | ||
1100 | case UNORDERED_EXPR: | |
1101 | predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN); | |
1102 | break; | |
1103 | ||
1104 | case LE_EXPR: | |
1105 | case LT_EXPR: | |
1106 | if (integer_zerop (TREE_OPERAND (cond, 1)) | |
1107 | || integer_onep (TREE_OPERAND (cond, 1)) | |
1108 | || integer_all_onesp (TREE_OPERAND (cond, 1)) | |
1109 | || real_zerop (TREE_OPERAND (cond, 1)) | |
1110 | || real_onep (TREE_OPERAND (cond, 1)) | |
1111 | || real_minus_onep (TREE_OPERAND (cond, 1))) | |
1112 | predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN); | |
1113 | break; | |
1114 | ||
1115 | case GE_EXPR: | |
1116 | case GT_EXPR: | |
1117 | if (integer_zerop (TREE_OPERAND (cond, 1)) | |
1118 | || integer_onep (TREE_OPERAND (cond, 1)) | |
1119 | || integer_all_onesp (TREE_OPERAND (cond, 1)) | |
1120 | || real_zerop (TREE_OPERAND (cond, 1)) | |
1121 | || real_onep (TREE_OPERAND (cond, 1)) | |
1122 | || real_minus_onep (TREE_OPERAND (cond, 1))) | |
1123 | predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN); | |
1124 | break; | |
1125 | ||
1126 | default: | |
1127 | break; | |
1128 | } | |
1129 | } | |
1130 | ||
bb033fd8 JH |
1131 | /* Try to guess whether the value of return means error code. */ |
1132 | static enum br_predictor | |
1133 | return_prediction (tree val, enum prediction *prediction) | |
1134 | { | |
1135 | /* VOID. */ | |
1136 | if (!val) | |
1137 | return PRED_NO_PREDICTION; | |
1138 | /* Different heuristics for pointers and scalars. */ | |
1139 | if (POINTER_TYPE_P (TREE_TYPE (val))) | |
1140 | { | |
1141 | /* NULL is usually not returned. */ | |
1142 | if (integer_zerop (val)) | |
1143 | { | |
1144 | *prediction = NOT_TAKEN; | |
1145 | return PRED_NULL_RETURN; | |
1146 | } | |
1147 | } | |
1148 | else if (INTEGRAL_TYPE_P (TREE_TYPE (val))) | |
1149 | { | |
1150 | /* Negative return values are often used to indicate | |
1151 | errors. */ | |
1152 | if (TREE_CODE (val) == INTEGER_CST | |
1153 | && tree_int_cst_sgn (val) < 0) | |
1154 | { | |
1155 | *prediction = NOT_TAKEN; | |
1156 | return PRED_NEGATIVE_RETURN; | |
1157 | } | |
1158 | /* Constant return values seems to be commonly taken. | |
1159 | Zero/one often represent booleans so exclude them from the | |
1160 | heuristics. */ | |
1161 | if (TREE_CONSTANT (val) | |
1162 | && (!integer_zerop (val) && !integer_onep (val))) | |
1163 | { | |
1164 | *prediction = TAKEN; | |
1165 | return PRED_NEGATIVE_RETURN; | |
1166 | } | |
1167 | } | |
1168 | return PRED_NO_PREDICTION; | |
1169 | } | |
1170 | ||
1171 | /* Find the basic block with return expression and look up for possible | |
1172 | return value trying to apply RETURN_PREDICTION heuristics. */ | |
1173 | static void | |
1174 | apply_return_prediction (int *heads) | |
1175 | { | |
1176 | tree return_stmt; | |
1177 | tree return_val; | |
1178 | edge e; | |
1179 | tree phi; | |
1180 | int phi_num_args, i; | |
1181 | enum br_predictor pred; | |
1182 | enum prediction direction; | |
1183 | ||
1184 | for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next) | |
1185 | { | |
1186 | return_stmt = last_stmt (e->src); | |
1187 | if (TREE_CODE (return_stmt) == RETURN_EXPR) | |
1188 | break; | |
1189 | } | |
1190 | if (!e) | |
1191 | return; | |
1192 | return_val = TREE_OPERAND (return_stmt, 0); | |
1193 | if (!return_val) | |
1194 | return; | |
1195 | if (TREE_CODE (return_val) == MODIFY_EXPR) | |
1196 | return_val = TREE_OPERAND (return_val, 1); | |
1197 | if (TREE_CODE (return_val) != SSA_NAME | |
1198 | || !SSA_NAME_DEF_STMT (return_val) | |
1199 | || TREE_CODE (SSA_NAME_DEF_STMT (return_val)) != PHI_NODE) | |
1200 | return; | |
1201 | phi = SSA_NAME_DEF_STMT (return_val); | |
1202 | while (phi) | |
1203 | { | |
1204 | tree next = PHI_CHAIN (phi); | |
1205 | if (PHI_RESULT (phi) == return_val) | |
1206 | break; | |
1207 | phi = next; | |
1208 | } | |
1209 | if (!phi) | |
1210 | return; | |
1211 | phi_num_args = PHI_NUM_ARGS (phi); | |
1212 | pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction); | |
1213 | ||
1214 | /* Avoid the degenerate case where all return values form the function | |
1215 | belongs to same category (ie they are all positive constants) | |
1216 | so we can hardly say something about them. */ | |
1217 | for (i = 1; i < phi_num_args; i++) | |
1218 | if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction)) | |
1219 | break; | |
1220 | if (i != phi_num_args) | |
1221 | for (i = 0; i < phi_num_args; i++) | |
1222 | { | |
1223 | pred = return_prediction (PHI_ARG_DEF (phi, i), &direction); | |
1224 | if (pred != PRED_NO_PREDICTION) | |
1225 | predict_paths_leading_to (PHI_ARG_EDGE (phi, i)->src, heads, pred, | |
1226 | direction); | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | /* Look for basic block that contains unlikely to happen events | |
1231 | (such as noreturn calls) and mark all paths leading to execution | |
1232 | of this basic blocks as unlikely. */ | |
1233 | ||
1234 | static void | |
1235 | tree_bb_level_predictions (void) | |
1236 | { | |
1237 | basic_block bb; | |
1238 | int *heads; | |
1239 | ||
1240 | heads = xmalloc (sizeof (int) * last_basic_block); | |
1241 | memset (heads, -1, sizeof (int) * last_basic_block); | |
1242 | heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block; | |
1243 | ||
1244 | apply_return_prediction (heads); | |
1245 | ||
1246 | FOR_EACH_BB (bb) | |
1247 | { | |
1248 | block_stmt_iterator bsi = bsi_last (bb); | |
1249 | ||
1250 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
1251 | { | |
1252 | tree stmt = bsi_stmt (bsi); | |
1253 | switch (TREE_CODE (stmt)) | |
1254 | { | |
1255 | case MODIFY_EXPR: | |
1256 | if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR) | |
1257 | { | |
1258 | stmt = TREE_OPERAND (stmt, 1); | |
1259 | goto call_expr; | |
1260 | } | |
1261 | break; | |
1262 | case CALL_EXPR: | |
1263 | call_expr:; | |
1264 | if (call_expr_flags (stmt) & ECF_NORETURN) | |
1265 | predict_paths_leading_to (bb, heads, PRED_NORETURN, | |
1266 | NOT_TAKEN); | |
1267 | break; | |
1268 | default: | |
1269 | break; | |
1270 | } | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | free (heads); | |
1275 | } | |
1276 | ||
6de9cd9a DN |
1277 | /* Predict branch probabilities and estimate profile of the tree CFG. */ |
1278 | static void | |
1279 | tree_estimate_probability (void) | |
1280 | { | |
1281 | basic_block bb; | |
1282 | struct loops loops_info; | |
1283 | ||
1284 | flow_loops_find (&loops_info, LOOP_TREE); | |
1285 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1286 | flow_loops_dump (&loops_info, dump_file, NULL, 0); | |
1287 | ||
bb033fd8 | 1288 | add_noreturn_fake_exit_edges (); |
6de9cd9a DN |
1289 | connect_infinite_loops_to_exit (); |
1290 | calculate_dominance_info (CDI_DOMINATORS); | |
1291 | calculate_dominance_info (CDI_POST_DOMINATORS); | |
1292 | ||
bb033fd8 JH |
1293 | tree_bb_level_predictions (); |
1294 | ||
6de9cd9a DN |
1295 | predict_loops (&loops_info, false); |
1296 | ||
1297 | FOR_EACH_BB (bb) | |
1298 | { | |
1299 | edge e; | |
1300 | ||
1301 | for (e = bb->succ; e; e = e->succ_next) | |
1302 | { | |
1303 | /* Predict early returns to be probable, as we've already taken | |
bb033fd8 JH |
1304 | care for error returns and other cases are often used for |
1305 | fast paths trought function. */ | |
1306 | if (e->dest == EXIT_BLOCK_PTR | |
1307 | && TREE_CODE (last_stmt (bb)) == RETURN_EXPR | |
1308 | && bb->pred && bb->pred->pred_next) | |
1309 | { | |
1310 | edge e1; | |
1311 | ||
1312 | for (e1 = bb->pred; e1; e1 = e1->pred_next) | |
1313 | if (!predicted_by_p (e1->src, PRED_NULL_RETURN) | |
1314 | && !predicted_by_p (e1->src, PRED_CONST_RETURN) | |
1315 | && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN) | |
1316 | && !last_basic_block_p (e1->src)) | |
1317 | predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
1318 | } | |
6de9cd9a | 1319 | |
bb033fd8 | 1320 | /* Look for block we are guarding (ie we dominate it, |
6de9cd9a DN |
1321 | but it doesn't postdominate us). */ |
1322 | if (e->dest != EXIT_BLOCK_PTR && e->dest != bb | |
1323 | && dominated_by_p (CDI_DOMINATORS, e->dest, e->src) | |
1324 | && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest)) | |
1325 | { | |
1326 | block_stmt_iterator bi; | |
1327 | ||
1328 | /* The call heuristic claims that a guarded function call | |
1329 | is improbable. This is because such calls are often used | |
1330 | to signal exceptional situations such as printing error | |
1331 | messages. */ | |
1332 | for (bi = bsi_start (e->dest); !bsi_end_p (bi); | |
1333 | bsi_next (&bi)) | |
1334 | { | |
1335 | tree stmt = bsi_stmt (bi); | |
1336 | if ((TREE_CODE (stmt) == CALL_EXPR | |
1337 | || (TREE_CODE (stmt) == MODIFY_EXPR | |
1338 | && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)) | |
1339 | /* Constant and pure calls are hardly used to signalize | |
1340 | something exceptional. */ | |
1341 | && TREE_SIDE_EFFECTS (stmt)) | |
1342 | { | |
1343 | predict_edge_def (e, PRED_CALL, NOT_TAKEN); | |
1344 | break; | |
1345 | } | |
1346 | } | |
1347 | } | |
1348 | } | |
1349 | tree_predict_by_opcode (bb); | |
1350 | } | |
1351 | FOR_EACH_BB (bb) | |
1352 | combine_predictions_for_bb (dump_file, bb); | |
861f9cd0 | 1353 | |
42f97fd2 JH |
1354 | if (0) /* FIXME: Enable once we are pass down the profile to RTL level. */ |
1355 | strip_builtin_expect (); | |
6de9cd9a DN |
1356 | estimate_bb_frequencies (&loops_info); |
1357 | free_dominance_info (CDI_POST_DOMINATORS); | |
6809cbf9 | 1358 | remove_fake_exit_edges (); |
6de9cd9a DN |
1359 | flow_loops_free (&loops_info); |
1360 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1361 | dump_tree_cfg (dump_file, dump_flags); | |
878f99d2 JH |
1362 | if (profile_status == PROFILE_ABSENT) |
1363 | profile_status = PROFILE_GUESSED; | |
f1ebdfc5 | 1364 | } |
994a57cd | 1365 | \f |
bfdade77 RK |
1366 | /* __builtin_expect dropped tokens into the insn stream describing expected |
1367 | values of registers. Generate branch probabilities based off these | |
1368 | values. */ | |
f1ebdfc5 | 1369 | |
994a57cd | 1370 | void |
79a490a9 | 1371 | expected_value_to_br_prob (void) |
994a57cd | 1372 | { |
36244024 | 1373 | rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX; |
994a57cd RH |
1374 | |
1375 | for (insn = get_insns (); insn ; insn = NEXT_INSN (insn)) | |
1376 | { | |
10f13594 RH |
1377 | switch (GET_CODE (insn)) |
1378 | { | |
1379 | case NOTE: | |
1380 | /* Look for expected value notes. */ | |
1381 | if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE) | |
1382 | { | |
1383 | ev = NOTE_EXPECTED_VALUE (insn); | |
1384 | ev_reg = XEXP (ev, 0); | |
49778644 | 1385 | delete_insn (insn); |
10f13594 RH |
1386 | } |
1387 | continue; | |
1388 | ||
1389 | case CODE_LABEL: | |
1390 | /* Never propagate across labels. */ | |
1391 | ev = NULL_RTX; | |
1392 | continue; | |
994a57cd | 1393 | |
10f13594 | 1394 | case JUMP_INSN: |
a1f300c0 | 1395 | /* Look for simple conditional branches. If we haven't got an |
10f13594 | 1396 | expected value yet, no point going further. */ |
4b4bf941 | 1397 | if (!JUMP_P (insn) || ev == NULL_RTX |
bfdade77 | 1398 | || ! any_condjump_p (insn)) |
10f13594 RH |
1399 | continue; |
1400 | break; | |
bfdade77 RK |
1401 | |
1402 | default: | |
1403 | /* Look for insns that clobber the EV register. */ | |
1404 | if (ev && reg_set_p (ev_reg, insn)) | |
1405 | ev = NULL_RTX; | |
1406 | continue; | |
10f13594 RH |
1407 | } |
1408 | ||
1409 | /* Collect the branch condition, hopefully relative to EV_REG. */ | |
d9490f2f RH |
1410 | /* ??? At present we'll miss things like |
1411 | (expected_value (eq r70 0)) | |
1412 | (set r71 -1) | |
1413 | (set r80 (lt r70 r71)) | |
1414 | (set pc (if_then_else (ne r80 0) ...)) | |
57cb6d52 | 1415 | as canonicalize_condition will render this to us as |
d9490f2f RH |
1416 | (lt r70, r71) |
1417 | Could use cselib to try and reduce this further. */ | |
24ee7cae | 1418 | cond = XEXP (SET_SRC (pc_set (insn)), 0); |
45d09c02 RS |
1419 | cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg, |
1420 | false, false); | |
bfdade77 | 1421 | if (! cond || XEXP (cond, 0) != ev_reg |
d9490f2f | 1422 | || GET_CODE (XEXP (cond, 1)) != CONST_INT) |
994a57cd RH |
1423 | continue; |
1424 | ||
57cb6d52 | 1425 | /* Substitute and simplify. Given that the expression we're |
994a57cd RH |
1426 | building involves two constants, we should wind up with either |
1427 | true or false. */ | |
1428 | cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode, | |
1429 | XEXP (ev, 1), XEXP (cond, 1)); | |
1430 | cond = simplify_rtx (cond); | |
1431 | ||
1432 | /* Turn the condition into a scaled branch probability. */ | |
8127d0e0 NS |
1433 | if (cond != const_true_rtx && cond != const0_rtx) |
1434 | abort (); | |
4db384c9 | 1435 | predict_insn_def (insn, PRED_BUILTIN_EXPECT, |
1b28186a | 1436 | cond == const_true_rtx ? TAKEN : NOT_TAKEN); |
994a57cd RH |
1437 | } |
1438 | } | |
861f9cd0 | 1439 | \f |
79a490a9 AJ |
1440 | /* Check whether this is the last basic block of function. Commonly |
1441 | there is one extra common cleanup block. */ | |
969d70ca | 1442 | static bool |
79a490a9 | 1443 | last_basic_block_p (basic_block bb) |
969d70ca | 1444 | { |
f6366fc7 ZD |
1445 | if (bb == EXIT_BLOCK_PTR) |
1446 | return false; | |
1447 | ||
1448 | return (bb->next_bb == EXIT_BLOCK_PTR | |
1449 | || (bb->next_bb->next_bb == EXIT_BLOCK_PTR | |
969d70ca | 1450 | && bb->succ && !bb->succ->succ_next |
f6366fc7 | 1451 | && bb->succ->dest->next_bb == EXIT_BLOCK_PTR)); |
969d70ca | 1452 | } |
bb033fd8 JH |
1453 | |
1454 | /* Sets branch probabilities according to PREDiction and | |
1455 | FLAGS. HEADS[bb->index] should be index of basic block in that we | |
1456 | need to alter branch predictions (i.e. the first of our dominators | |
1457 | such that we do not post-dominate it) (but we fill this information | |
1458 | on demand, so -1 may be there in case this was not needed yet). */ | |
1459 | ||
1460 | static void | |
1461 | predict_paths_leading_to (basic_block bb, int *heads, enum br_predictor pred, | |
1462 | enum prediction taken) | |
1463 | { | |
1464 | edge e; | |
1465 | int y; | |
1466 | ||
1467 | if (heads[bb->index] < 0) | |
1468 | { | |
1469 | /* This is first time we need this field in heads array; so | |
1470 | find first dominator that we do not post-dominate (we are | |
1471 | using already known members of heads array). */ | |
1472 | basic_block ai = bb; | |
1473 | basic_block next_ai = get_immediate_dominator (CDI_DOMINATORS, bb); | |
1474 | int head; | |
1475 | ||
1476 | while (heads[next_ai->index] < 0) | |
1477 | { | |
1478 | if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb)) | |
1479 | break; | |
1480 | heads[next_ai->index] = ai->index; | |
1481 | ai = next_ai; | |
1482 | next_ai = get_immediate_dominator (CDI_DOMINATORS, next_ai); | |
1483 | } | |
1484 | if (!dominated_by_p (CDI_POST_DOMINATORS, next_ai, bb)) | |
1485 | head = next_ai->index; | |
1486 | else | |
1487 | head = heads[next_ai->index]; | |
1488 | while (next_ai != bb) | |
1489 | { | |
1490 | next_ai = ai; | |
1491 | if (heads[ai->index] == ENTRY_BLOCK) | |
1492 | ai = ENTRY_BLOCK_PTR; | |
1493 | else | |
1494 | ai = BASIC_BLOCK (heads[ai->index]); | |
1495 | heads[next_ai->index] = head; | |
1496 | } | |
1497 | } | |
1498 | y = heads[bb->index]; | |
1499 | ||
1500 | /* Now find the edge that leads to our branch and aply the prediction. */ | |
1501 | ||
1502 | if (y == last_basic_block) | |
1503 | return; | |
1504 | for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next) | |
1505 | if (e->dest->index >= 0 | |
1506 | && dominated_by_p (CDI_POST_DOMINATORS, e->dest, bb)) | |
1507 | predict_edge_def (e, pred, taken); | |
1508 | } | |
969d70ca | 1509 | \f |
57cb6d52 | 1510 | /* This is used to carry information about basic blocks. It is |
861f9cd0 JH |
1511 | attached to the AUX field of the standard CFG block. */ |
1512 | ||
1513 | typedef struct block_info_def | |
1514 | { | |
1515 | /* Estimated frequency of execution of basic_block. */ | |
ac5e69da | 1516 | sreal frequency; |
861f9cd0 JH |
1517 | |
1518 | /* To keep queue of basic blocks to process. */ | |
1519 | basic_block next; | |
1520 | ||
ba228239 | 1521 | /* True if block needs to be visited in propagate_freq. */ |
2c45a16a | 1522 | unsigned int tovisit:1; |
247a370b | 1523 | |
eaec9b3d | 1524 | /* Number of predecessors we need to visit first. */ |
754d9299 | 1525 | int npredecessors; |
861f9cd0 JH |
1526 | } *block_info; |
1527 | ||
1528 | /* Similar information for edges. */ | |
1529 | typedef struct edge_info_def | |
1530 | { | |
1531 | /* In case edge is an loopback edge, the probability edge will be reached | |
1532 | in case header is. Estimated number of iterations of the loop can be | |
8aa18a7d | 1533 | then computed as 1 / (1 - back_edge_prob). */ |
ac5e69da | 1534 | sreal back_edge_prob; |
861f9cd0 | 1535 | /* True if the edge is an loopback edge in the natural loop. */ |
2c45a16a | 1536 | unsigned int back_edge:1; |
861f9cd0 JH |
1537 | } *edge_info; |
1538 | ||
1539 | #define BLOCK_INFO(B) ((block_info) (B)->aux) | |
1540 | #define EDGE_INFO(E) ((edge_info) (E)->aux) | |
1541 | ||
1542 | /* Helper function for estimate_bb_frequencies. | |
2ecfd709 | 1543 | Propagate the frequencies for LOOP. */ |
bfdade77 | 1544 | |
861f9cd0 | 1545 | static void |
79a490a9 | 1546 | propagate_freq (struct loop *loop) |
861f9cd0 | 1547 | { |
2ecfd709 | 1548 | basic_block head = loop->header; |
e0082a72 ZD |
1549 | basic_block bb; |
1550 | basic_block last; | |
861f9cd0 JH |
1551 | edge e; |
1552 | basic_block nextbb; | |
247a370b | 1553 | |
eaec9b3d | 1554 | /* For each basic block we need to visit count number of his predecessors |
247a370b | 1555 | we need to visit first. */ |
214ee4a2 | 1556 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
247a370b | 1557 | { |
247a370b JH |
1558 | if (BLOCK_INFO (bb)->tovisit) |
1559 | { | |
1560 | int count = 0; | |
bfdade77 | 1561 | |
247a370b JH |
1562 | for (e = bb->pred; e; e = e->pred_next) |
1563 | if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK)) | |
1564 | count++; | |
1565 | else if (BLOCK_INFO (e->src)->tovisit | |
c263766c RH |
1566 | && dump_file && !EDGE_INFO (e)->back_edge) |
1567 | fprintf (dump_file, | |
247a370b | 1568 | "Irreducible region hit, ignoring edge to %i->%i\n", |
0b17ab2f | 1569 | e->src->index, bb->index); |
754d9299 | 1570 | BLOCK_INFO (bb)->npredecessors = count; |
247a370b JH |
1571 | } |
1572 | } | |
861f9cd0 | 1573 | |
8aa18a7d | 1574 | memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one)); |
e0082a72 ZD |
1575 | last = head; |
1576 | for (bb = head; bb; bb = nextbb) | |
861f9cd0 | 1577 | { |
ac5e69da | 1578 | sreal cyclic_probability, frequency; |
8aa18a7d JH |
1579 | |
1580 | memcpy (&cyclic_probability, &real_zero, sizeof (real_zero)); | |
1581 | memcpy (&frequency, &real_zero, sizeof (real_zero)); | |
861f9cd0 JH |
1582 | |
1583 | nextbb = BLOCK_INFO (bb)->next; | |
1584 | BLOCK_INFO (bb)->next = NULL; | |
1585 | ||
1586 | /* Compute frequency of basic block. */ | |
1587 | if (bb != head) | |
1588 | { | |
247a370b | 1589 | #ifdef ENABLE_CHECKING |
861f9cd0 | 1590 | for (e = bb->pred; e; e = e->pred_next) |
8127d0e0 NS |
1591 | if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK)) |
1592 | abort (); | |
247a370b | 1593 | #endif |
861f9cd0 JH |
1594 | |
1595 | for (e = bb->pred; e; e = e->pred_next) | |
1596 | if (EDGE_INFO (e)->back_edge) | |
8aa18a7d | 1597 | { |
ac5e69da JZ |
1598 | sreal_add (&cyclic_probability, &cyclic_probability, |
1599 | &EDGE_INFO (e)->back_edge_prob); | |
8aa18a7d | 1600 | } |
247a370b | 1601 | else if (!(e->flags & EDGE_DFS_BACK)) |
8aa18a7d | 1602 | { |
ac5e69da | 1603 | sreal tmp; |
8aa18a7d JH |
1604 | |
1605 | /* frequency += (e->probability | |
1606 | * BLOCK_INFO (e->src)->frequency / | |
1607 | REG_BR_PROB_BASE); */ | |
1608 | ||
ac5e69da JZ |
1609 | sreal_init (&tmp, e->probability, 0); |
1610 | sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency); | |
1611 | sreal_mul (&tmp, &tmp, &real_inv_br_prob_base); | |
1612 | sreal_add (&frequency, &frequency, &tmp); | |
8aa18a7d JH |
1613 | } |
1614 | ||
ac5e69da JZ |
1615 | if (sreal_compare (&cyclic_probability, &real_zero) == 0) |
1616 | { | |
1617 | memcpy (&BLOCK_INFO (bb)->frequency, &frequency, | |
1618 | sizeof (frequency)); | |
1619 | } | |
fbe3b30b SB |
1620 | else |
1621 | { | |
ac5e69da JZ |
1622 | if (sreal_compare (&cyclic_probability, &real_almost_one) > 0) |
1623 | { | |
1624 | memcpy (&cyclic_probability, &real_almost_one, | |
1625 | sizeof (real_almost_one)); | |
1626 | } | |
861f9cd0 | 1627 | |
79a490a9 | 1628 | /* BLOCK_INFO (bb)->frequency = frequency |
ac5e69da | 1629 | / (1 - cyclic_probability) */ |
861f9cd0 | 1630 | |
ac5e69da JZ |
1631 | sreal_sub (&cyclic_probability, &real_one, &cyclic_probability); |
1632 | sreal_div (&BLOCK_INFO (bb)->frequency, | |
1633 | &frequency, &cyclic_probability); | |
fbe3b30b | 1634 | } |
861f9cd0 JH |
1635 | } |
1636 | ||
247a370b | 1637 | BLOCK_INFO (bb)->tovisit = 0; |
861f9cd0 JH |
1638 | |
1639 | /* Compute back edge frequencies. */ | |
1640 | for (e = bb->succ; e; e = e->succ_next) | |
1641 | if (e->dest == head) | |
8aa18a7d | 1642 | { |
ac5e69da | 1643 | sreal tmp; |
8aa18a7d JH |
1644 | |
1645 | /* EDGE_INFO (e)->back_edge_prob | |
1646 | = ((e->probability * BLOCK_INFO (bb)->frequency) | |
1647 | / REG_BR_PROB_BASE); */ | |
8aa18a7d | 1648 | |
ac5e69da JZ |
1649 | sreal_init (&tmp, e->probability, 0); |
1650 | sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency); | |
1651 | sreal_mul (&EDGE_INFO (e)->back_edge_prob, | |
1652 | &tmp, &real_inv_br_prob_base); | |
8aa18a7d | 1653 | } |
861f9cd0 | 1654 | |
57cb6d52 | 1655 | /* Propagate to successor blocks. */ |
861f9cd0 | 1656 | for (e = bb->succ; e; e = e->succ_next) |
247a370b | 1657 | if (!(e->flags & EDGE_DFS_BACK) |
754d9299 | 1658 | && BLOCK_INFO (e->dest)->npredecessors) |
861f9cd0 | 1659 | { |
754d9299 JM |
1660 | BLOCK_INFO (e->dest)->npredecessors--; |
1661 | if (!BLOCK_INFO (e->dest)->npredecessors) | |
247a370b JH |
1662 | { |
1663 | if (!nextbb) | |
1664 | nextbb = e->dest; | |
1665 | else | |
1666 | BLOCK_INFO (last)->next = e->dest; | |
bfdade77 | 1667 | |
247a370b JH |
1668 | last = e->dest; |
1669 | } | |
1670 | } | |
861f9cd0 JH |
1671 | } |
1672 | } | |
1673 | ||
57cb6d52 | 1674 | /* Estimate probabilities of loopback edges in loops at same nest level. */ |
bfdade77 | 1675 | |
861f9cd0 | 1676 | static void |
79a490a9 | 1677 | estimate_loops_at_level (struct loop *first_loop) |
861f9cd0 | 1678 | { |
2ecfd709 | 1679 | struct loop *loop; |
861f9cd0 JH |
1680 | |
1681 | for (loop = first_loop; loop; loop = loop->next) | |
1682 | { | |
861f9cd0 | 1683 | edge e; |
2ecfd709 | 1684 | basic_block *bbs; |
3d436d2a | 1685 | unsigned i; |
861f9cd0 JH |
1686 | |
1687 | estimate_loops_at_level (loop->inner); | |
79a490a9 | 1688 | |
2ecfd709 | 1689 | if (loop->latch->succ) /* Do not do this for dummy function loop. */ |
861f9cd0 | 1690 | { |
2ecfd709 ZD |
1691 | /* Find current loop back edge and mark it. */ |
1692 | e = loop_latch_edge (loop); | |
1693 | EDGE_INFO (e)->back_edge = 1; | |
1694 | } | |
1695 | ||
1696 | bbs = get_loop_body (loop); | |
1697 | for (i = 0; i < loop->num_nodes; i++) | |
1698 | BLOCK_INFO (bbs[i])->tovisit = 1; | |
1699 | free (bbs); | |
1700 | propagate_freq (loop); | |
861f9cd0 JH |
1701 | } |
1702 | } | |
1703 | ||
02307675 R |
1704 | /* Convert counts measured by profile driven feedback to frequencies. |
1705 | Return nonzero iff there was any nonzero execution count. */ | |
bfdade77 | 1706 | |
bbd236a1 | 1707 | int |
79a490a9 | 1708 | counts_to_freqs (void) |
861f9cd0 | 1709 | { |
02307675 | 1710 | gcov_type count_max, true_count_max = 0; |
e0082a72 | 1711 | basic_block bb; |
0b17ab2f | 1712 | |
e0082a72 | 1713 | FOR_EACH_BB (bb) |
02307675 | 1714 | true_count_max = MAX (bb->count, true_count_max); |
861f9cd0 | 1715 | |
02307675 | 1716 | count_max = MAX (true_count_max, 1); |
e0082a72 ZD |
1717 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
1718 | bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max; | |
02307675 | 1719 | return true_count_max; |
861f9cd0 JH |
1720 | } |
1721 | ||
bfdade77 RK |
1722 | /* Return true if function is likely to be expensive, so there is no point to |
1723 | optimize performance of prologue, epilogue or do inlining at the expense | |
d55d8fc7 | 1724 | of code size growth. THRESHOLD is the limit of number of instructions |
bfdade77 RK |
1725 | function can execute at average to be still considered not expensive. */ |
1726 | ||
6ab16dd9 | 1727 | bool |
79a490a9 | 1728 | expensive_function_p (int threshold) |
6ab16dd9 JH |
1729 | { |
1730 | unsigned int sum = 0; | |
e0082a72 | 1731 | basic_block bb; |
5197bd50 | 1732 | unsigned int limit; |
6ab16dd9 JH |
1733 | |
1734 | /* We can not compute accurately for large thresholds due to scaled | |
1735 | frequencies. */ | |
8127d0e0 NS |
1736 | if (threshold > BB_FREQ_MAX) |
1737 | abort (); | |
6ab16dd9 | 1738 | |
eaec9b3d | 1739 | /* Frequencies are out of range. This either means that function contains |
6ab16dd9 JH |
1740 | internal loop executing more than BB_FREQ_MAX times or profile feedback |
1741 | is available and function has not been executed at all. */ | |
1742 | if (ENTRY_BLOCK_PTR->frequency == 0) | |
1743 | return true; | |
6a4d6760 | 1744 | |
6ab16dd9 JH |
1745 | /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */ |
1746 | limit = ENTRY_BLOCK_PTR->frequency * threshold; | |
e0082a72 | 1747 | FOR_EACH_BB (bb) |
6ab16dd9 | 1748 | { |
6ab16dd9 JH |
1749 | rtx insn; |
1750 | ||
a813c111 | 1751 | for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); |
6ab16dd9 | 1752 | insn = NEXT_INSN (insn)) |
bfdade77 RK |
1753 | if (active_insn_p (insn)) |
1754 | { | |
1755 | sum += bb->frequency; | |
1756 | if (sum > limit) | |
1757 | return true; | |
6ab16dd9 JH |
1758 | } |
1759 | } | |
bfdade77 | 1760 | |
6ab16dd9 JH |
1761 | return false; |
1762 | } | |
1763 | ||
861f9cd0 | 1764 | /* Estimate basic blocks frequency by given branch probabilities. */ |
bfdade77 | 1765 | |
861f9cd0 | 1766 | static void |
79a490a9 | 1767 | estimate_bb_frequencies (struct loops *loops) |
861f9cd0 | 1768 | { |
e0082a72 | 1769 | basic_block bb; |
ac5e69da | 1770 | sreal freq_max; |
8aa18a7d | 1771 | |
02307675 | 1772 | if (!flag_branch_probabilities || !counts_to_freqs ()) |
194734e9 | 1773 | { |
c4f6b78e RE |
1774 | static int real_values_initialized = 0; |
1775 | ||
1776 | if (!real_values_initialized) | |
1777 | { | |
85bb9c2a | 1778 | real_values_initialized = 1; |
c4f6b78e RE |
1779 | sreal_init (&real_zero, 0, 0); |
1780 | sreal_init (&real_one, 1, 0); | |
1781 | sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0); | |
1782 | sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0); | |
1783 | sreal_init (&real_one_half, 1, -1); | |
1784 | sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base); | |
1785 | sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base); | |
1786 | } | |
861f9cd0 | 1787 | |
194734e9 | 1788 | mark_dfs_back_edges (); |
194734e9 JH |
1789 | |
1790 | ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE; | |
1791 | ||
1792 | /* Set up block info for each basic block. */ | |
1793 | alloc_aux_for_blocks (sizeof (struct block_info_def)); | |
1794 | alloc_aux_for_edges (sizeof (struct edge_info_def)); | |
e0082a72 | 1795 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
861f9cd0 | 1796 | { |
861f9cd0 | 1797 | edge e; |
194734e9 JH |
1798 | |
1799 | BLOCK_INFO (bb)->tovisit = 0; | |
1800 | for (e = bb->succ; e; e = e->succ_next) | |
861f9cd0 | 1801 | { |
ac5e69da JZ |
1802 | sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0); |
1803 | sreal_mul (&EDGE_INFO (e)->back_edge_prob, | |
1804 | &EDGE_INFO (e)->back_edge_prob, | |
1805 | &real_inv_br_prob_base); | |
861f9cd0 | 1806 | } |
861f9cd0 | 1807 | } |
bfdade77 | 1808 | |
194734e9 JH |
1809 | /* First compute probabilities locally for each loop from innermost |
1810 | to outermost to examine probabilities for back edges. */ | |
1811 | estimate_loops_at_level (loops->tree_root); | |
861f9cd0 | 1812 | |
194734e9 | 1813 | memcpy (&freq_max, &real_zero, sizeof (real_zero)); |
e0082a72 | 1814 | FOR_EACH_BB (bb) |
ac5e69da JZ |
1815 | if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0) |
1816 | memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max)); | |
fbe3b30b | 1817 | |
ac5e69da | 1818 | sreal_div (&freq_max, &real_bb_freq_max, &freq_max); |
e0082a72 | 1819 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
8aa18a7d | 1820 | { |
ac5e69da | 1821 | sreal tmp; |
bfdade77 | 1822 | |
ac5e69da JZ |
1823 | sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max); |
1824 | sreal_add (&tmp, &tmp, &real_one_half); | |
1825 | bb->frequency = sreal_to_int (&tmp); | |
194734e9 | 1826 | } |
bfdade77 | 1827 | |
194734e9 JH |
1828 | free_aux_for_blocks (); |
1829 | free_aux_for_edges (); | |
1830 | } | |
1831 | compute_function_frequency (); | |
1832 | if (flag_reorder_functions) | |
1833 | choose_function_section (); | |
1834 | } | |
861f9cd0 | 1835 | |
194734e9 JH |
1836 | /* Decide whether function is hot, cold or unlikely executed. */ |
1837 | static void | |
79a490a9 | 1838 | compute_function_frequency (void) |
194734e9 | 1839 | { |
e0082a72 ZD |
1840 | basic_block bb; |
1841 | ||
cdb23767 | 1842 | if (!profile_info || !flag_branch_probabilities) |
194734e9 JH |
1843 | return; |
1844 | cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED; | |
e0082a72 | 1845 | FOR_EACH_BB (bb) |
861f9cd0 | 1846 | { |
194734e9 JH |
1847 | if (maybe_hot_bb_p (bb)) |
1848 | { | |
1849 | cfun->function_frequency = FUNCTION_FREQUENCY_HOT; | |
1850 | return; | |
1851 | } | |
1852 | if (!probably_never_executed_bb_p (bb)) | |
1853 | cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL; | |
861f9cd0 | 1854 | } |
194734e9 | 1855 | } |
861f9cd0 | 1856 | |
194734e9 JH |
1857 | /* Choose appropriate section for the function. */ |
1858 | static void | |
79a490a9 | 1859 | choose_function_section (void) |
194734e9 JH |
1860 | { |
1861 | if (DECL_SECTION_NAME (current_function_decl) | |
c07f146f JH |
1862 | || !targetm.have_named_sections |
1863 | /* Theoretically we can split the gnu.linkonce text section too, | |
79a490a9 | 1864 | but this requires more work as the frequency needs to match |
c07f146f JH |
1865 | for all generated objects so we need to merge the frequency |
1866 | of all instances. For now just never set frequency for these. */ | |
c728da61 | 1867 | || DECL_ONE_ONLY (current_function_decl)) |
194734e9 | 1868 | return; |
9fb32434 CT |
1869 | |
1870 | /* If we are doing the partitioning optimization, let the optimization | |
1871 | choose the correct section into which to put things. */ | |
1872 | ||
1873 | if (flag_reorder_blocks_and_partition) | |
1874 | return; | |
1875 | ||
194734e9 JH |
1876 | if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT) |
1877 | DECL_SECTION_NAME (current_function_decl) = | |
1878 | build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME); | |
1879 | if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED) | |
1880 | DECL_SECTION_NAME (current_function_decl) = | |
1881 | build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME), | |
1882 | UNLIKELY_EXECUTED_TEXT_SECTION_NAME); | |
861f9cd0 | 1883 | } |
6de9cd9a DN |
1884 | |
1885 | ||
1886 | struct tree_opt_pass pass_profile = | |
1887 | { | |
1888 | "profile", /* name */ | |
1889 | NULL, /* gate */ | |
1890 | tree_estimate_probability, /* execute */ | |
1891 | NULL, /* sub */ | |
1892 | NULL, /* next */ | |
1893 | 0, /* static_pass_number */ | |
1894 | TV_BRANCH_PROB, /* tv_id */ | |
1895 | PROP_cfg, /* properties_required */ | |
1896 | 0, /* properties_provided */ | |
1897 | 0, /* properties_destroyed */ | |
1898 | 0, /* todo_flags_start */ | |
9f8628ba PB |
1899 | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */ |
1900 | 0 /* letter */ | |
6de9cd9a | 1901 | }; |