source: trunk/src/gcc/gcc/predict.c@ 1392

Last change on this file since 1392 was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 36.8 KB
Line 
1/* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
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 "tree.h"
34#include "rtl.h"
35#include "tm_p.h"
36#include "hard-reg-set.h"
37#include "basic-block.h"
38#include "insn-config.h"
39#include "regs.h"
40#include "flags.h"
41#include "output.h"
42#include "function.h"
43#include "except.h"
44#include "toplev.h"
45#include "recog.h"
46#include "expr.h"
47#include "predict.h"
48#include "profile.h"
49#include "real.h"
50#include "params.h"
51#include "target.h"
52#include "loop.h"
53
54/* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
55 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
56static REAL_VALUE_TYPE real_zero, real_one, real_almost_one, real_br_prob_base,
57 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
58
59/* Random guesstimation given names. */
60#define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1)
61#define PROB_EVEN (REG_BR_PROB_BASE / 2)
62#define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
63#define PROB_ALWAYS (REG_BR_PROB_BASE)
64
65static bool predicted_by_p PARAMS ((basic_block,
66 enum br_predictor));
67static void combine_predictions_for_insn PARAMS ((rtx, basic_block));
68static void dump_prediction PARAMS ((enum br_predictor, int,
69 basic_block, int));
70static void estimate_loops_at_level PARAMS ((struct loop *loop));
71static void propagate_freq PARAMS ((struct loop *));
72static void estimate_bb_frequencies PARAMS ((struct loops *));
73static void counts_to_freqs PARAMS ((void));
74static void process_note_predictions PARAMS ((basic_block, int *,
75 dominance_info,
76 dominance_info));
77static void process_note_prediction PARAMS ((basic_block, int *,
78 dominance_info,
79 dominance_info, int, int));
80static bool last_basic_block_p PARAMS ((basic_block));
81static void compute_function_frequency PARAMS ((void));
82static void choose_function_section PARAMS ((void));
83static bool can_predict_insn_p PARAMS ((rtx));
84
85/* Information we hold about each branch predictor.
86 Filled using information from predict.def. */
87
88struct predictor_info
89{
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;
94};
95
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
102#define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
103
104#define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
105static const struct predictor_info predictor_info[]= {
106#include "predict.def"
107
108 /* Upper bound on predictors. */
109 {NULL, 0, 0}
110};
111#undef DEF_PREDICTOR
112
113/* Return true in case BB can be CPU intensive and should be optimized
114 for maximal perofmrance. */
115
116bool
117maybe_hot_bb_p (bb)
118 basic_block bb;
119{
120 if (profile_info.count_profiles_merged
121 && flag_branch_probabilities
122 && (bb->count
123 < profile_info.max_counter_in_program
124 / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
125 return false;
126 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
127 return false;
128 return true;
129}
130
131/* Return true in case BB is cold and should be optimized for size. */
132
133bool
134probably_cold_bb_p (bb)
135 basic_block bb;
136{
137 if (profile_info.count_profiles_merged
138 && flag_branch_probabilities
139 && (bb->count
140 < profile_info.max_counter_in_program
141 / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
142 return true;
143 if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
144 return true;
145 return false;
146}
147
148/* Return true in case BB is probably never executed. */
149bool
150probably_never_executed_bb_p (bb)
151 basic_block bb;
152{
153 if (profile_info.count_profiles_merged
154 && flag_branch_probabilities)
155 return ((bb->count + profile_info.count_profiles_merged / 2)
156 / profile_info.count_profiles_merged) == 0;
157 return false;
158}
159
160/* Return true if the one of outgoing edges is already predicted by
161 PREDICTOR. */
162
163static bool
164predicted_by_p (bb, predictor)
165 basic_block bb;
166 enum br_predictor predictor;
167{
168 rtx note;
169 if (!INSN_P (bb->end))
170 return false;
171 for (note = REG_NOTES (bb->end); note; note = XEXP (note, 1))
172 if (REG_NOTE_KIND (note) == REG_BR_PRED
173 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
174 return true;
175 return false;
176}
177
178void
179predict_insn (insn, predictor, probability)
180 rtx insn;
181 int probability;
182 enum br_predictor predictor;
183{
184 if (!any_condjump_p (insn))
185 abort ();
186 if (!flag_guess_branch_prob)
187 return;
188
189 REG_NOTES (insn)
190 = gen_rtx_EXPR_LIST (REG_BR_PRED,
191 gen_rtx_CONCAT (VOIDmode,
192 GEN_INT ((int) predictor),
193 GEN_INT ((int) probability)),
194 REG_NOTES (insn));
195}
196
197/* Predict insn by given predictor. */
198
199void
200predict_insn_def (insn, predictor, taken)
201 rtx insn;
202 enum br_predictor predictor;
203 enum prediction taken;
204{
205 int probability = predictor_info[(int) predictor].hitrate;
206
207 if (taken != TAKEN)
208 probability = REG_BR_PROB_BASE - probability;
209
210 predict_insn (insn, predictor, probability);
211}
212
213/* Predict edge E with given probability if possible. */
214
215void
216predict_edge (e, predictor, probability)
217 edge e;
218 int probability;
219 enum br_predictor predictor;
220{
221 rtx last_insn;
222 last_insn = e->src->end;
223
224 /* We can store the branch prediction information only about
225 conditional jumps. */
226 if (!any_condjump_p (last_insn))
227 return;
228
229 /* We always store probability of branching. */
230 if (e->flags & EDGE_FALLTHRU)
231 probability = REG_BR_PROB_BASE - probability;
232
233 predict_insn (last_insn, predictor, probability);
234}
235
236/* Return true when we can store prediction on insn INSN.
237 At the moment we represent predictions only on conditional
238 jumps, not at computed jump or other complicated cases. */
239static bool
240can_predict_insn_p (insn)
241 rtx insn;
242{
243 return (GET_CODE (insn) == JUMP_INSN
244 && any_condjump_p (insn)
245 && BLOCK_FOR_INSN (insn)->succ->succ_next);
246}
247
248/* Predict edge E by given predictor if possible. */
249
250void
251predict_edge_def (e, predictor, taken)
252 edge e;
253 enum br_predictor predictor;
254 enum prediction taken;
255{
256 int probability = predictor_info[(int) predictor].hitrate;
257
258 if (taken != TAKEN)
259 probability = REG_BR_PROB_BASE - probability;
260
261 predict_edge (e, predictor, probability);
262}
263
264/* Invert all branch predictions or probability notes in the INSN. This needs
265 to be done each time we invert the condition used by the jump. */
266
267void
268invert_br_probabilities (insn)
269 rtx insn;
270{
271 rtx note;
272
273 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
274 if (REG_NOTE_KIND (note) == REG_BR_PROB)
275 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
276 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
277 XEXP (XEXP (note, 0), 1)
278 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
279}
280
281/* Dump information about the branch prediction to the output file. */
282
283static void
284dump_prediction (predictor, probability, bb, used)
285 enum br_predictor predictor;
286 int probability;
287 basic_block bb;
288 int used;
289{
290 edge e = bb->succ;
291
292 if (!rtl_dump_file)
293 return;
294
295 while (e && (e->flags & EDGE_FALLTHRU))
296 e = e->succ_next;
297
298 fprintf (rtl_dump_file, " %s heuristics%s: %.1f%%",
299 predictor_info[predictor].name,
300 used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
301
302 if (bb->count)
303 {
304 fprintf (rtl_dump_file, " exec ");
305 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
306 if (e)
307 {
308 fprintf (rtl_dump_file, " hit ");
309 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, e->count);
310 fprintf (rtl_dump_file, " (%.1f%%)", e->count * 100.0 / bb->count);
311 }
312 }
313
314 fprintf (rtl_dump_file, "\n");
315}
316
317/* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
318 note if not already present. Remove now useless REG_BR_PRED notes. */
319
320static void
321combine_predictions_for_insn (insn, bb)
322 rtx insn;
323 basic_block bb;
324{
325 rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
326 rtx *pnote = &REG_NOTES (insn);
327 rtx note;
328 int best_probability = PROB_EVEN;
329 int best_predictor = END_PREDICTORS;
330 int combined_probability = REG_BR_PROB_BASE / 2;
331 int d;
332 bool first_match = false;
333 bool found = false;
334
335 if (rtl_dump_file)
336 fprintf (rtl_dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
337 bb->index);
338
339 /* We implement "first match" heuristics and use probability guessed
340 by predictor with smallest index. In the future we will use better
341 probability combination techniques. */
342 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
343 if (REG_NOTE_KIND (note) == REG_BR_PRED)
344 {
345 int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
346 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
347
348 found = true;
349 if (best_predictor > predictor)
350 best_probability = probability, best_predictor = predictor;
351
352 d = (combined_probability * probability
353 + (REG_BR_PROB_BASE - combined_probability)
354 * (REG_BR_PROB_BASE - probability));
355
356 /* Use FP math to avoid overflows of 32bit integers. */
357 if (d == 0)
358 /* If one probability is 0% and one 100%, avoid division by zero. */
359 combined_probability = REG_BR_PROB_BASE / 2;
360 else
361 combined_probability = (((double) combined_probability) * probability
362 * REG_BR_PROB_BASE / d + 0.5);
363 }
364
365 /* Decide which heuristic to use. In case we didn't match anything,
366 use no_prediction heuristic, in case we did match, use either
367 first match or Dempster-Shaffer theory depending on the flags. */
368
369 if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
370 first_match = true;
371
372 if (!found)
373 dump_prediction (PRED_NO_PREDICTION, combined_probability, bb, true);
374 else
375 {
376 dump_prediction (PRED_DS_THEORY, combined_probability, bb, !first_match);
377 dump_prediction (PRED_FIRST_MATCH, best_probability, bb, first_match);
378 }
379
380 if (first_match)
381 combined_probability = best_probability;
382 dump_prediction (PRED_COMBINED, combined_probability, bb, true);
383
384 while (*pnote)
385 {
386 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
387 {
388 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
389 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
390
391 dump_prediction (predictor, probability, bb,
392 !first_match || best_predictor == predictor);
393 *pnote = XEXP (*pnote, 1);
394 }
395 else
396 pnote = &XEXP (*pnote, 1);
397 }
398
399 if (!prob_note)
400 {
401 REG_NOTES (insn)
402 = gen_rtx_EXPR_LIST (REG_BR_PROB,
403 GEN_INT (combined_probability), REG_NOTES (insn));
404
405 /* Save the prediction into CFG in case we are seeing non-degenerated
406 conditional jump. */
407 if (bb->succ->succ_next)
408 {
409 BRANCH_EDGE (bb)->probability = combined_probability;
410 FALLTHRU_EDGE (bb)->probability
411 = REG_BR_PROB_BASE - combined_probability;
412 }
413 }
414}
415
416/* Statically estimate the probability that a branch will be taken.
417 ??? In the next revision there will be a number of other predictors added
418 from the above references. Further, each heuristic will be factored out
419 into its own function for clarity (and to facilitate the combination of
420 predictions). */
421
422void
423estimate_probability (loops_info)
424 struct loops *loops_info;
425{
426 dominance_info dominators, post_dominators;
427 basic_block bb;
428 int i;
429
430 connect_infinite_loops_to_exit ();
431 dominators = calculate_dominance_info (CDI_DOMINATORS);
432 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
433
434 /* Try to predict out blocks in a loop that are not part of a
435 natural loop. */
436 for (i = 1; i < loops_info->num; i++)
437 {
438 basic_block bb, *bbs;
439 int j;
440 int exits;
441 struct loop *loop = loops_info->parray[i];
442
443 flow_loop_scan (loops_info, loop, LOOP_EXIT_EDGES);
444 exits = loop->num_exits;
445
446 bbs = get_loop_body (loop);
447 for (j = 0; j < loop->num_nodes; j++)
448 {
449 int header_found = 0;
450 edge e;
451
452 bb = bbs[j];
453
454 /* Bypass loop heuristics on continue statement. These
455 statements construct loops via "non-loop" constructs
456 in the source language and are better to be handled
457 separately. */
458 if (!can_predict_insn_p (bb->end)
459 || predicted_by_p (bb, PRED_CONTINUE))
460 continue;
461
462 /* Loop branch heuristics - predict an edge back to a
463 loop's head as taken. */
464 for (e = bb->succ; e; e = e->succ_next)
465 if (e->dest == loop->header
466 && e->src == loop->latch)
467 {
468 header_found = 1;
469 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
470 }
471
472 /* Loop exit heuristics - predict an edge exiting the loop if the
473 conditinal has no loop header successors as not taken. */
474 if (!header_found)
475 for (e = bb->succ; e; e = e->succ_next)
476 if (e->dest->index < 0
477 || !flow_bb_inside_loop_p (loop, e->dest))
478 predict_edge
479 (e, PRED_LOOP_EXIT,
480 (REG_BR_PROB_BASE
481 - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
482 / exits);
483 }
484 }
485
486 /* Attempt to predict conditional jumps using a number of heuristics. */
487 FOR_EACH_BB (bb)
488 {
489 rtx last_insn = bb->end;
490 rtx cond, earliest;
491 edge e;
492
493 if (! can_predict_insn_p (last_insn))
494 continue;
495
496 for (e = bb->succ; e; e = e->succ_next)
497 {
498 /* Predict early returns to be probable, as we've already taken
499 care for error returns and other are often used for fast paths
500 trought function. */
501 if ((e->dest == EXIT_BLOCK_PTR
502 || (e->dest->succ && !e->dest->succ->succ_next
503 && e->dest->succ->dest == EXIT_BLOCK_PTR))
504 && !predicted_by_p (bb, PRED_NULL_RETURN)
505 && !predicted_by_p (bb, PRED_CONST_RETURN)
506 && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
507 && !last_basic_block_p (e->dest))
508 predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
509
510 /* Look for block we are guarding (ie we dominate it,
511 but it doesn't postdominate us). */
512 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
513 && dominated_by_p (dominators, e->dest, e->src)
514 && !dominated_by_p (post_dominators, e->src, e->dest))
515 {
516 rtx insn;
517
518 /* The call heuristic claims that a guarded function call
519 is improbable. This is because such calls are often used
520 to signal exceptional situations such as printing error
521 messages. */
522 for (insn = e->dest->head; insn != NEXT_INSN (e->dest->end);
523 insn = NEXT_INSN (insn))
524 if (GET_CODE (insn) == CALL_INSN
525 /* Constant and pure calls are hardly used to signalize
526 something exceptional. */
527 && ! CONST_OR_PURE_CALL_P (insn))
528 {
529 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
530 break;
531 }
532 }
533 }
534
535 cond = get_condition (last_insn, &earliest);
536 if (! cond)
537 continue;
538
539 /* Try "pointer heuristic."
540 A comparison ptr == 0 is predicted as false.
541 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
542 if (GET_RTX_CLASS (GET_CODE (cond)) == '<'
543 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
544 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
545 {
546 if (GET_CODE (cond) == EQ)
547 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
548 else if (GET_CODE (cond) == NE)
549 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
550 }
551 else
552
553 /* Try "opcode heuristic."
554 EQ tests are usually false and NE tests are usually true. Also,
555 most quantities are positive, so we can make the appropriate guesses
556 about signed comparisons against zero. */
557 switch (GET_CODE (cond))
558 {
559 case CONST_INT:
560 /* Unconditional branch. */
561 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
562 cond == const0_rtx ? NOT_TAKEN : TAKEN);
563 break;
564
565 case EQ:
566 case UNEQ:
567 /* Floating point comparisons appears to behave in a very
568 inpredictable way because of special role of = tests in
569 FP code. */
570 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
571 ;
572 /* Comparisons with 0 are often used for booleans and there is
573 nothing usefull to predict about them. */
574 else if (XEXP (cond, 1) == const0_rtx
575 || XEXP (cond, 0) == const0_rtx)
576 ;
577 else
578 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
579 break;
580
581 case NE:
582 case LTGT:
583 /* Floating point comparisons appears to behave in a very
584 inpredictable way because of special role of = tests in
585 FP code. */
586 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
587 ;
588 /* Comparisons with 0 are often used for booleans and there is
589 nothing usefull to predict about them. */
590 else if (XEXP (cond, 1) == const0_rtx
591 || XEXP (cond, 0) == const0_rtx)
592 ;
593 else
594 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
595 break;
596
597 case ORDERED:
598 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
599 break;
600
601 case UNORDERED:
602 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
603 break;
604
605 case LE:
606 case LT:
607 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
608 || XEXP (cond, 1) == constm1_rtx)
609 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
610 break;
611
612 case GE:
613 case GT:
614 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
615 || XEXP (cond, 1) == constm1_rtx)
616 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
617 break;
618
619 default:
620 break;
621 }
622 }
623
624 /* Attach the combined probability to each conditional jump. */
625 FOR_EACH_BB (bb)
626 if (GET_CODE (bb->end) == JUMP_INSN
627 && any_condjump_p (bb->end)
628 && bb->succ->succ_next != NULL)
629 combine_predictions_for_insn (bb->end, bb);
630
631 free_dominance_info (post_dominators);
632 free_dominance_info (dominators);
633
634 remove_fake_edges ();
635 estimate_bb_frequencies (loops_info);
636}
637
638
639/* __builtin_expect dropped tokens into the insn stream describing expected
640 values of registers. Generate branch probabilities based off these
641 values. */
642
643void
644expected_value_to_br_prob ()
645{
646 rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
647
648 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
649 {
650 switch (GET_CODE (insn))
651 {
652 case NOTE:
653 /* Look for expected value notes. */
654 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
655 {
656 ev = NOTE_EXPECTED_VALUE (insn);
657 ev_reg = XEXP (ev, 0);
658 delete_insn (insn);
659 }
660 continue;
661
662 case CODE_LABEL:
663 /* Never propagate across labels. */
664 ev = NULL_RTX;
665 continue;
666
667 case JUMP_INSN:
668 /* Look for simple conditional branches. If we haven't got an
669 expected value yet, no point going further. */
670 if (GET_CODE (insn) != JUMP_INSN || ev == NULL_RTX
671 || ! any_condjump_p (insn))
672 continue;
673 break;
674
675 default:
676 /* Look for insns that clobber the EV register. */
677 if (ev && reg_set_p (ev_reg, insn))
678 ev = NULL_RTX;
679 continue;
680 }
681
682 /* Collect the branch condition, hopefully relative to EV_REG. */
683 /* ??? At present we'll miss things like
684 (expected_value (eq r70 0))
685 (set r71 -1)
686 (set r80 (lt r70 r71))
687 (set pc (if_then_else (ne r80 0) ...))
688 as canonicalize_condition will render this to us as
689 (lt r70, r71)
690 Could use cselib to try and reduce this further. */
691 cond = XEXP (SET_SRC (pc_set (insn)), 0);
692 cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg);
693 if (! cond || XEXP (cond, 0) != ev_reg
694 || GET_CODE (XEXP (cond, 1)) != CONST_INT)
695 continue;
696
697 /* Substitute and simplify. Given that the expression we're
698 building involves two constants, we should wind up with either
699 true or false. */
700 cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
701 XEXP (ev, 1), XEXP (cond, 1));
702 cond = simplify_rtx (cond);
703
704 /* Turn the condition into a scaled branch probability. */
705 if (cond != const_true_rtx && cond != const0_rtx)
706 abort ();
707 predict_insn_def (insn, PRED_BUILTIN_EXPECT,
708 cond == const_true_rtx ? TAKEN : NOT_TAKEN);
709 }
710}
711
712
713/* Check whether this is the last basic block of function. Commonly tehre
714 is one extra common cleanup block. */
715static bool
716last_basic_block_p (bb)
717 basic_block bb;
718{
719 if (bb == EXIT_BLOCK_PTR)
720 return false;
721
722 return (bb->next_bb == EXIT_BLOCK_PTR
723 || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
724 && bb->succ && !bb->succ->succ_next
725 && bb->succ->dest->next_bb == EXIT_BLOCK_PTR));
726}
727
728/* Sets branch probabilities according to PREDiction and FLAGS. HEADS[bb->index]
729 should be index of basic block in that we need to alter branch predictions
730 (i.e. the first of our dominators such that we do not post-dominate it)
731 (but we fill this information on demand, so -1 may be there in case this
732 was not needed yet). */
733
734static void
735process_note_prediction (bb, heads, dominators, post_dominators, pred, flags)
736 basic_block bb;
737 int *heads;
738 dominance_info dominators;
739 dominance_info post_dominators;
740 int pred;
741 int flags;
742{
743 edge e;
744 int y;
745 bool taken;
746
747 taken = flags & IS_TAKEN;
748
749 if (heads[bb->index] < 0)
750 {
751 /* This is first time we need this field in heads array; so
752 find first dominator that we do not post-dominate (we are
753 using already known members of heads array). */
754 basic_block ai = bb;
755 basic_block next_ai = get_immediate_dominator (dominators, bb);
756 int head;
757
758 while (heads[next_ai->index] < 0)
759 {
760 if (!dominated_by_p (post_dominators, next_ai, bb))
761 break;
762 heads[next_ai->index] = ai->index;
763 ai = next_ai;
764 next_ai = get_immediate_dominator (dominators, next_ai);
765 }
766 if (!dominated_by_p (post_dominators, next_ai, bb))
767 head = next_ai->index;
768 else
769 head = heads[next_ai->index];
770 while (next_ai != bb)
771 {
772 next_ai = ai;
773 if (heads[ai->index] == ENTRY_BLOCK)
774 ai = ENTRY_BLOCK_PTR;
775 else
776 ai = BASIC_BLOCK (heads[ai->index]);
777 heads[next_ai->index] = head;
778 }
779 }
780 y = heads[bb->index];
781
782 /* Now find the edge that leads to our branch and aply the prediction. */
783
784 if (y == last_basic_block || !can_predict_insn_p (BASIC_BLOCK (y)->end))
785 return;
786 for (e = BASIC_BLOCK (y)->succ; e; e = e->succ_next)
787 if (e->dest->index >= 0
788 && dominated_by_p (post_dominators, e->dest, bb))
789 predict_edge_def (e, pred, taken);
790}
791
792/* Gathers NOTE_INSN_PREDICTIONs in given basic block and turns them
793 into branch probabilities. For description of heads array, see
794 process_note_prediction. */
795
796static void
797process_note_predictions (bb, heads, dominators, post_dominators)
798 basic_block bb;
799 int *heads;
800 dominance_info dominators;
801 dominance_info post_dominators;
802{
803 rtx insn;
804 edge e;
805
806 /* Additionaly, we check here for blocks with no successors. */
807 int contained_noreturn_call = 0;
808 int was_bb_head = 0;
809 int noreturn_block = 1;
810
811 for (insn = bb->end; insn;
812 was_bb_head |= (insn == bb->head), insn = PREV_INSN (insn))
813 {
814 if (GET_CODE (insn) != NOTE)
815 {
816 if (was_bb_head)
817 break;
818 else
819 {
820 /* Noreturn calls cause program to exit, therefore they are
821 always predicted as not taken. */
822 if (GET_CODE (insn) == CALL_INSN
823 && find_reg_note (insn, REG_NORETURN, NULL))
824 contained_noreturn_call = 1;
825 continue;
826 }
827 }
828 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION)
829 {
830 int alg = (int) NOTE_PREDICTION_ALG (insn);
831 /* Process single prediction note. */
832 process_note_prediction (bb,
833 heads,
834 dominators,
835 post_dominators,
836 alg, (int) NOTE_PREDICTION_FLAGS (insn));
837 delete_insn (insn);
838 }
839 }
840 for (e = bb->succ; e; e = e->succ_next)
841 if (!(e->flags & EDGE_FAKE))
842 noreturn_block = 0;
843 if (contained_noreturn_call)
844 {
845 /* This block ended from other reasons than because of return.
846 If it is because of noreturn call, this should certainly not
847 be taken. Otherwise it is probably some error recovery. */
848 process_note_prediction (bb,
849 heads,
850 dominators,
851 post_dominators, PRED_NORETURN, NOT_TAKEN);
852 }
853}
854
855/* Gathers NOTE_INSN_PREDICTIONs and turns them into
856 branch probabilities. */
857
858void
859note_prediction_to_br_prob ()
860{
861 basic_block bb;
862 dominance_info post_dominators, dominators;
863 int *heads;
864
865 /* To enable handling of noreturn blocks. */
866 add_noreturn_fake_exit_edges ();
867 connect_infinite_loops_to_exit ();
868
869 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
870 dominators = calculate_dominance_info (CDI_DOMINATORS);
871
872 heads = xmalloc (sizeof (int) * last_basic_block);
873 memset (heads, -1, sizeof (int) * last_basic_block);
874 heads[ENTRY_BLOCK_PTR->next_bb->index] = last_basic_block;
875
876 /* Process all prediction notes. */
877
878 FOR_EACH_BB (bb)
879 process_note_predictions (bb, heads, dominators, post_dominators);
880
881 free_dominance_info (post_dominators);
882 free_dominance_info (dominators);
883 free (heads);
884
885 remove_fake_edges ();
886}
887
888
889/* This is used to carry information about basic blocks. It is
890 attached to the AUX field of the standard CFG block. */
891
892typedef struct block_info_def
893{
894 /* Estimated frequency of execution of basic_block. */
895 REAL_VALUE_TYPE frequency;
896
897 /* To keep queue of basic blocks to process. */
898 basic_block next;
899
900 /* True if block needs to be visited in prop_freqency. */
901 int tovisit:1;
902
903 /* Number of predecessors we need to visit first. */
904 int npredecessors;
905} *block_info;
906
907/* Similar information for edges. */
908typedef struct edge_info_def
909{
910 /* In case edge is an loopback edge, the probability edge will be reached
911 in case header is. Estimated number of iterations of the loop can be
912 then computed as 1 / (1 - back_edge_prob). */
913 REAL_VALUE_TYPE back_edge_prob;
914 /* True if the edge is an loopback edge in the natural loop. */
915 int back_edge:1;
916} *edge_info;
917
918#define BLOCK_INFO(B) ((block_info) (B)->aux)
919#define EDGE_INFO(E) ((edge_info) (E)->aux)
920
921/* Helper function for estimate_bb_frequencies.
922 Propagate the frequencies for LOOP. */
923
924static void
925propagate_freq (loop)
926 struct loop *loop;
927{
928 basic_block head = loop->header;
929 basic_block bb;
930 basic_block last;
931 edge e;
932 basic_block nextbb;
933
934 /* For each basic block we need to visit count number of his predecessors
935 we need to visit first. */
936 FOR_EACH_BB (bb)
937 {
938 if (BLOCK_INFO (bb)->tovisit)
939 {
940 int count = 0;
941
942 for (e = bb->pred; e; e = e->pred_next)
943 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
944 count++;
945 else if (BLOCK_INFO (e->src)->tovisit
946 && rtl_dump_file && !EDGE_INFO (e)->back_edge)
947 fprintf (rtl_dump_file,
948 "Irreducible region hit, ignoring edge to %i->%i\n",
949 e->src->index, bb->index);
950 BLOCK_INFO (bb)->npredecessors = count;
951 }
952 }
953
954 memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
955 last = head;
956 for (bb = head; bb; bb = nextbb)
957 {
958 REAL_VALUE_TYPE cyclic_probability, frequency;
959
960 memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
961 memcpy (&frequency, &real_zero, sizeof (real_zero));
962
963 nextbb = BLOCK_INFO (bb)->next;
964 BLOCK_INFO (bb)->next = NULL;
965
966 /* Compute frequency of basic block. */
967 if (bb != head)
968 {
969#ifdef ENABLE_CHECKING
970 for (e = bb->pred; e; e = e->pred_next)
971 if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
972 abort ();
973#endif
974
975 for (e = bb->pred; e; e = e->pred_next)
976 if (EDGE_INFO (e)->back_edge)
977 {
978 REAL_ARITHMETIC (cyclic_probability, PLUS_EXPR,
979 cyclic_probability,
980 EDGE_INFO (e)->back_edge_prob);
981 }
982 else if (!(e->flags & EDGE_DFS_BACK))
983 {
984 REAL_VALUE_TYPE tmp;
985
986 /* frequency += (e->probability
987 * BLOCK_INFO (e->src)->frequency /
988 REG_BR_PROB_BASE); */
989
990 REAL_VALUE_FROM_INT (tmp, e->probability, 0,
991 TYPE_MODE (double_type_node));
992 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp,
993 BLOCK_INFO (e->src)->frequency);
994 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp, real_inv_br_prob_base);
995 REAL_ARITHMETIC (frequency, PLUS_EXPR, frequency, tmp);
996 }
997
998 if (REAL_VALUES_IDENTICAL (cyclic_probability, real_zero))
999 memcpy (&BLOCK_INFO (bb)->frequency, &frequency, sizeof (frequency));
1000 else
1001 {
1002 if (REAL_VALUES_LESS (real_almost_one, cyclic_probability))
1003 memcpy (&cyclic_probability, &real_almost_one, sizeof (real_zero));
1004
1005 /* BLOCK_INFO (bb)->frequency = frequency / (1 - cyclic_probability)
1006 */
1007
1008 REAL_ARITHMETIC (cyclic_probability, MINUS_EXPR, real_one,
1009 cyclic_probability);
1010 REAL_ARITHMETIC (BLOCK_INFO (bb)->frequency,
1011 RDIV_EXPR, frequency, cyclic_probability);
1012 }
1013 }
1014
1015 BLOCK_INFO (bb)->tovisit = 0;
1016
1017 /* Compute back edge frequencies. */
1018 for (e = bb->succ; e; e = e->succ_next)
1019 if (e->dest == head)
1020 {
1021 REAL_VALUE_TYPE tmp;
1022
1023 /* EDGE_INFO (e)->back_edge_prob
1024 = ((e->probability * BLOCK_INFO (bb)->frequency)
1025 / REG_BR_PROB_BASE); */
1026 REAL_VALUE_FROM_INT (tmp, e->probability, 0,
1027 TYPE_MODE (double_type_node));
1028 REAL_ARITHMETIC (tmp, MULT_EXPR, tmp,
1029 BLOCK_INFO (bb)->frequency);
1030 REAL_ARITHMETIC (EDGE_INFO (e)->back_edge_prob,
1031 MULT_EXPR, tmp, real_inv_br_prob_base);
1032
1033 }
1034
1035 /* Propagate to successor blocks. */
1036 for (e = bb->succ; e; e = e->succ_next)
1037 if (!(e->flags & EDGE_DFS_BACK)
1038 && BLOCK_INFO (e->dest)->npredecessors)
1039 {
1040 BLOCK_INFO (e->dest)->npredecessors--;
1041 if (!BLOCK_INFO (e->dest)->npredecessors)
1042 {
1043 if (!nextbb)
1044 nextbb = e->dest;
1045 else
1046 BLOCK_INFO (last)->next = e->dest;
1047
1048 last = e->dest;
1049 }
1050 }
1051 }
1052}
1053
1054/* Estimate probabilities of loopback edges in loops at same nest level. */
1055
1056static void
1057estimate_loops_at_level (first_loop)
1058 struct loop *first_loop;
1059{
1060 struct loop *loop;
1061
1062 for (loop = first_loop; loop; loop = loop->next)
1063 {
1064 edge e;
1065 basic_block *bbs;
1066 int i;
1067
1068 estimate_loops_at_level (loop->inner);
1069
1070 if (loop->latch->succ) /* Do not do this for dummy function loop. */
1071 {
1072 /* Find current loop back edge and mark it. */
1073 e = loop_latch_edge (loop);
1074 EDGE_INFO (e)->back_edge = 1;
1075 }
1076
1077 bbs = get_loop_body (loop);
1078 for (i = 0; i < loop->num_nodes; i++)
1079 BLOCK_INFO (bbs[i])->tovisit = 1;
1080 free (bbs);
1081 propagate_freq (loop);
1082 }
1083}
1084
1085/* Convert counts measured by profile driven feedback to frequencies. */
1086
1087static void
1088counts_to_freqs ()
1089{
1090 HOST_WIDEST_INT count_max = 1;
1091 basic_block bb;
1092
1093 FOR_EACH_BB (bb)
1094 count_max = MAX (bb->count, count_max);
1095
1096 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1097 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
1098}
1099
1100/* Return true if function is likely to be expensive, so there is no point to
1101 optimize performance of prologue, epilogue or do inlining at the expense
1102 of code size growth. THRESHOLD is the limit of number of isntructions
1103 function can execute at average to be still considered not expensive. */
1104
1105bool
1106expensive_function_p (threshold)
1107 int threshold;
1108{
1109 unsigned int sum = 0;
1110 basic_block bb;
1111 unsigned int limit;
1112
1113 /* We can not compute accurately for large thresholds due to scaled
1114 frequencies. */
1115 if (threshold > BB_FREQ_MAX)
1116 abort ();
1117
1118 /* Frequencies are out of range. This either means that function contains
1119 internal loop executing more than BB_FREQ_MAX times or profile feedback
1120 is available and function has not been executed at all. */
1121 if (ENTRY_BLOCK_PTR->frequency == 0)
1122 return true;
1123
1124 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
1125 limit = ENTRY_BLOCK_PTR->frequency * threshold;
1126 FOR_EACH_BB (bb)
1127 {
1128 rtx insn;
1129
1130 for (insn = bb->head; insn != NEXT_INSN (bb->end);
1131 insn = NEXT_INSN (insn))
1132 if (active_insn_p (insn))
1133 {
1134 sum += bb->frequency;
1135 if (sum > limit)
1136 return true;
1137 }
1138 }
1139
1140 return false;
1141}
1142
1143/* Estimate basic blocks frequency by given branch probabilities. */
1144
1145static void
1146estimate_bb_frequencies (loops)
1147 struct loops *loops;
1148{
1149 basic_block bb;
1150 REAL_VALUE_TYPE freq_max;
1151 enum machine_mode double_mode = TYPE_MODE (double_type_node);
1152
1153 if (flag_branch_probabilities)
1154 counts_to_freqs ();
1155 else
1156 {
1157 REAL_VALUE_FROM_INT (real_zero, 0, 0, double_mode);
1158 REAL_VALUE_FROM_INT (real_one, 1, 0, double_mode);
1159 REAL_VALUE_FROM_INT (real_br_prob_base, REG_BR_PROB_BASE, 0, double_mode);
1160 REAL_VALUE_FROM_INT (real_bb_freq_max, BB_FREQ_MAX, 0, double_mode);
1161 REAL_VALUE_FROM_INT (real_one_half, 2, 0, double_mode);
1162 REAL_ARITHMETIC (real_one_half, RDIV_EXPR, real_one, real_one_half);
1163 REAL_ARITHMETIC (real_inv_br_prob_base, RDIV_EXPR, real_one, real_br_prob_base);
1164 REAL_ARITHMETIC (real_almost_one, MINUS_EXPR, real_one, real_inv_br_prob_base);
1165
1166 mark_dfs_back_edges ();
1167 /* Fill in the probability values in flowgraph based on the REG_BR_PROB
1168 notes. */
1169 FOR_EACH_BB (bb)
1170 {
1171 rtx last_insn = bb->end;
1172
1173 if (!can_predict_insn_p (last_insn))
1174 {
1175 /* We can predict only conditional jumps at the moment.
1176 Expect each edge to be equally probable.
1177 ?? In the future we want to make abnormal edges improbable. */
1178 int nedges = 0;
1179 edge e;
1180
1181 for (e = bb->succ; e; e = e->succ_next)
1182 {
1183 nedges++;
1184 if (e->probability != 0)
1185 break;
1186 }
1187 if (!e)
1188 for (e = bb->succ; e; e = e->succ_next)
1189 e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
1190 }
1191 }
1192
1193 ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
1194
1195 /* Set up block info for each basic block. */
1196 alloc_aux_for_blocks (sizeof (struct block_info_def));
1197 alloc_aux_for_edges (sizeof (struct edge_info_def));
1198 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1199 {
1200 edge e;
1201
1202 BLOCK_INFO (bb)->tovisit = 0;
1203 for (e = bb->succ; e; e = e->succ_next)
1204 {
1205 REAL_VALUE_FROM_INT (EDGE_INFO (e)->back_edge_prob,
1206 e->probability, 0, double_mode);
1207 REAL_ARITHMETIC (EDGE_INFO (e)->back_edge_prob,
1208 MULT_EXPR, EDGE_INFO (e)->back_edge_prob,
1209 real_inv_br_prob_base);
1210 }
1211 }
1212
1213 /* First compute probabilities locally for each loop from innermost
1214 to outermost to examine probabilities for back edges. */
1215 estimate_loops_at_level (loops->tree_root);
1216
1217 memcpy (&freq_max, &real_zero, sizeof (real_zero));
1218 FOR_EACH_BB (bb)
1219 if (REAL_VALUES_LESS
1220 (freq_max, BLOCK_INFO (bb)->frequency))
1221 memcpy (&freq_max, &BLOCK_INFO (bb)->frequency,
1222 sizeof (freq_max));
1223
1224 REAL_ARITHMETIC (freq_max, RDIV_EXPR, real_bb_freq_max, freq_max);
1225
1226 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1227 {
1228 REAL_VALUE_TYPE tmp;
1229
1230 REAL_ARITHMETIC (tmp, MULT_EXPR, BLOCK_INFO (bb)->frequency,
1231 freq_max);
1232 REAL_ARITHMETIC (tmp, PLUS_EXPR, tmp, real_one_half);
1233 bb->frequency = REAL_VALUE_UNSIGNED_FIX (tmp);
1234 }
1235
1236 free_aux_for_blocks ();
1237 free_aux_for_edges ();
1238 }
1239 compute_function_frequency ();
1240 if (flag_reorder_functions)
1241 choose_function_section ();
1242}
1243
1244/* Decide whether function is hot, cold or unlikely executed. */
1245static void
1246compute_function_frequency ()
1247{
1248 basic_block bb;
1249
1250 if (!profile_info.count_profiles_merged
1251 || !flag_branch_probabilities)
1252 return;
1253 cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
1254 FOR_EACH_BB (bb)
1255 {
1256 if (maybe_hot_bb_p (bb))
1257 {
1258 cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
1259 return;
1260 }
1261 if (!probably_never_executed_bb_p (bb))
1262 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
1263 }
1264}
1265
1266/* Choose appropriate section for the function. */
1267static void
1268choose_function_section ()
1269{
1270 if (DECL_SECTION_NAME (current_function_decl)
1271 || !targetm.have_named_sections
1272 /* Theoretically we can split the gnu.linkonce text section too,
1273 but this requires more work as the frequency needs to match
1274 for all generated objects so we need to merge the frequency
1275 of all instances. For now just never set frequency for these. */
1276 || DECL_ONE_ONLY (current_function_decl))
1277 return;
1278 if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
1279 DECL_SECTION_NAME (current_function_decl) =
1280 build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
1281 if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1282 DECL_SECTION_NAME (current_function_decl) =
1283 build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
1284 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
1285}
Note: See TracBrowser for help on using the repository browser.