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