Mercurial > hg > CbC > CbC_gcc
comparison gcc/predict.c @ 55:77e2b8dfacca gcc-4.4.5
update it from 4.4.3 to 4.5.0
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 12 Feb 2010 23:39:51 +0900 |
parents | a06113de4d67 |
children | b7f97abdc517 |
comparison
equal
deleted
inserted
replaced
52:c156f1bd5cd9 | 55:77e2b8dfacca |
---|---|
1 /* Branch prediction routines for the GNU compiler. | 1 /* Branch prediction routines for the GNU compiler. |
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008 | 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 |
3 Free Software Foundation, Inc. | 3 Free Software Foundation, Inc. |
4 | 4 |
5 This file is part of GCC. | 5 This file is part of GCC. |
6 | 6 |
7 GCC is free software; you can redistribute it and/or modify it under | 7 GCC is free software; you can redistribute it and/or modify it under |
64 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE, | 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. */ | 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, | 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; | 67 real_inv_br_prob_base, real_one_half, real_bb_freq_max; |
68 | 68 |
69 /* Random guesstimation given names. | 69 /* Random guesstimation given names. |
70 PROV_VERY_UNLIKELY should be small enough so basic block predicted | 70 PROV_VERY_UNLIKELY should be small enough so basic block predicted |
71 by it gets bellow HOT_BB_FREQUENCY_FRANCTION. */ | 71 by it gets bellow HOT_BB_FREQUENCY_FRANCTION. */ |
72 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1) | 72 #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1) |
73 #define PROB_EVEN (REG_BR_PROB_BASE / 2) | 73 #define PROB_EVEN (REG_BR_PROB_BASE / 2) |
74 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY) | 74 #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY) |
75 #define PROB_ALWAYS (REG_BR_PROB_BASE) | 75 #define PROB_ALWAYS (REG_BR_PROB_BASE) |
76 | 76 |
77 static void combine_predictions_for_insn (rtx, basic_block); | 77 static void combine_predictions_for_insn (rtx, basic_block); |
78 static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int); | 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); | 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); | 80 static void choose_function_section (void); |
82 static bool can_predict_insn_p (const_rtx); | 81 static bool can_predict_insn_p (const_rtx); |
83 | 82 |
84 /* Information we hold about each branch predictor. | 83 /* Information we hold about each branch predictor. |
85 Filled using information from predict.def. */ | 84 Filled using information from predict.def. */ |
166 || lookup_attribute ("cold", DECL_ATTRIBUTES (edge->caller->decl))) | 165 || lookup_attribute ("cold", DECL_ATTRIBUTES (edge->caller->decl))) |
167 return false; | 166 return false; |
168 if (lookup_attribute ("hot", DECL_ATTRIBUTES (edge->caller->decl))) | 167 if (lookup_attribute ("hot", DECL_ATTRIBUTES (edge->caller->decl))) |
169 return true; | 168 return true; |
170 if (flag_guess_branch_prob | 169 if (flag_guess_branch_prob |
171 && edge->frequency < (CGRAPH_FREQ_MAX | 170 && edge->frequency <= (CGRAPH_FREQ_BASE |
172 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))) | 171 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))) |
173 return false; | 172 return false; |
174 return true; | 173 return true; |
175 } | 174 } |
176 | 175 |
177 /* Return true in case BB can be CPU intensive and should be optimized | 176 /* Return true in case BB can be CPU intensive and should be optimized |
385 struct edge_prediction *i; | 384 struct edge_prediction *i; |
386 void **preds = pointer_map_contains (bb_predictions, bb); | 385 void **preds = pointer_map_contains (bb_predictions, bb); |
387 | 386 |
388 if (!preds) | 387 if (!preds) |
389 return false; | 388 return false; |
390 | 389 |
391 for (i = (struct edge_prediction *) *preds; i; i = i->ep_next) | 390 for (i = (struct edge_prediction *) *preds; i; i = i->ep_next) |
392 if (i->ep_predictor == predictor) | 391 if (i->ep_predictor == predictor) |
393 return true; | 392 return true; |
394 return false; | 393 return false; |
395 } | 394 } |
396 | 395 |
397 /* Return true when the probability of edge is reliable. | 396 /* Return true when the probability of edge is reliable. |
398 | 397 |
399 The profile guessing code is good at predicting branch outcome (ie. | 398 The profile guessing code is good at predicting branch outcome (ie. |
400 taken/not taken), that is predicted right slightly over 75% of time. | 399 taken/not taken), that is predicted right slightly over 75% of time. |
401 It is however notoriously poor on predicting the probability itself. | 400 It is however notoriously poor on predicting the probability itself. |
402 In general the profile appear a lot flatter (with probabilities closer | 401 In general the profile appear a lot flatter (with probabilities closer |
403 to 50%) than the reality so it is bad idea to use it to drive optimization | 402 to 50%) than the reality so it is bad idea to use it to drive optimization |
503 to edge E. */ | 502 to edge E. */ |
504 void | 503 void |
505 remove_predictions_associated_with_edge (edge e) | 504 remove_predictions_associated_with_edge (edge e) |
506 { | 505 { |
507 void **preds; | 506 void **preds; |
508 | 507 |
509 if (!bb_predictions) | 508 if (!bb_predictions) |
510 return; | 509 return; |
511 | 510 |
512 preds = pointer_map_contains (bb_predictions, e->src); | 511 preds = pointer_map_contains (bb_predictions, e->src); |
513 | 512 |
652 { | 651 { |
653 rtx prob_note; | 652 rtx prob_note; |
654 rtx *pnote; | 653 rtx *pnote; |
655 rtx note; | 654 rtx note; |
656 int best_probability = PROB_EVEN; | 655 int best_probability = PROB_EVEN; |
657 int best_predictor = END_PREDICTORS; | 656 enum br_predictor best_predictor = END_PREDICTORS; |
658 int combined_probability = REG_BR_PROB_BASE / 2; | 657 int combined_probability = REG_BR_PROB_BASE / 2; |
659 int d; | 658 int d; |
660 bool first_match = false; | 659 bool first_match = false; |
661 bool found = false; | 660 bool found = false; |
662 | 661 |
675 /* We implement "first match" heuristics and use probability guessed | 674 /* We implement "first match" heuristics and use probability guessed |
676 by predictor with smallest index. */ | 675 by predictor with smallest index. */ |
677 for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | 676 for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) |
678 if (REG_NOTE_KIND (note) == REG_BR_PRED) | 677 if (REG_NOTE_KIND (note) == REG_BR_PRED) |
679 { | 678 { |
680 int predictor = INTVAL (XEXP (XEXP (note, 0), 0)); | 679 enum br_predictor predictor = ((enum br_predictor) |
680 INTVAL (XEXP (XEXP (note, 0), 0))); | |
681 int probability = INTVAL (XEXP (XEXP (note, 0), 1)); | 681 int probability = INTVAL (XEXP (XEXP (note, 0), 1)); |
682 | 682 |
683 found = true; | 683 found = true; |
684 if (best_predictor > predictor) | 684 if (best_predictor > predictor) |
685 best_probability = probability, best_predictor = predictor; | 685 best_probability = probability, best_predictor = predictor; |
721 | 721 |
722 while (*pnote) | 722 while (*pnote) |
723 { | 723 { |
724 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED) | 724 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED) |
725 { | 725 { |
726 int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0)); | 726 enum br_predictor predictor = ((enum br_predictor) |
727 INTVAL (XEXP (XEXP (*pnote, 0), 0))); | |
727 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1)); | 728 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1)); |
728 | 729 |
729 dump_prediction (dump_file, predictor, probability, bb, | 730 dump_prediction (dump_file, predictor, probability, bb, |
730 !first_match || best_predictor == predictor); | 731 !first_match || best_predictor == predictor); |
731 *pnote = XEXP (*pnote, 1); | 732 *pnote = XEXP (*pnote, 1); |
763 | 764 |
764 static void | 765 static void |
765 combine_predictions_for_bb (basic_block bb) | 766 combine_predictions_for_bb (basic_block bb) |
766 { | 767 { |
767 int best_probability = PROB_EVEN; | 768 int best_probability = PROB_EVEN; |
768 int best_predictor = END_PREDICTORS; | 769 enum br_predictor best_predictor = END_PREDICTORS; |
769 int combined_probability = REG_BR_PROB_BASE / 2; | 770 int combined_probability = REG_BR_PROB_BASE / 2; |
770 int d; | 771 int d; |
771 bool first_match = false; | 772 bool first_match = false; |
772 bool found = false; | 773 bool found = false; |
773 struct edge_prediction *pred; | 774 struct edge_prediction *pred; |
784 second = e; | 785 second = e; |
785 if (!first) | 786 if (!first) |
786 first = e; | 787 first = e; |
787 } | 788 } |
788 | 789 |
789 /* When there is no successor or only one choice, prediction is easy. | 790 /* When there is no successor or only one choice, prediction is easy. |
790 | 791 |
791 We are lazy for now and predict only basic blocks with two outgoing | 792 We are lazy for now and predict only basic blocks with two outgoing |
792 edges. It is possible to predict generic case too, but we have to | 793 edges. It is possible to predict generic case too, but we have to |
793 ignore first match heuristics and do more involved combining. Implement | 794 ignore first match heuristics and do more involved combining. Implement |
794 this later. */ | 795 this later. */ |
811 { | 812 { |
812 /* We implement "first match" heuristics and use probability guessed | 813 /* We implement "first match" heuristics and use probability guessed |
813 by predictor with smallest index. */ | 814 by predictor with smallest index. */ |
814 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) | 815 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) |
815 { | 816 { |
816 int predictor = pred->ep_predictor; | 817 enum br_predictor predictor = pred->ep_predictor; |
817 int probability = pred->ep_probability; | 818 int probability = pred->ep_probability; |
818 | 819 |
819 if (pred->ep_edge != first) | 820 if (pred->ep_edge != first) |
820 probability = REG_BR_PROB_BASE - probability; | 821 probability = REG_BR_PROB_BASE - probability; |
821 | 822 |
833 int probability2 = pred->ep_probability; | 834 int probability2 = pred->ep_probability; |
834 | 835 |
835 if (pred2->ep_edge != first) | 836 if (pred2->ep_edge != first) |
836 probability2 = REG_BR_PROB_BASE - probability2; | 837 probability2 = REG_BR_PROB_BASE - probability2; |
837 | 838 |
838 if ((probability < REG_BR_PROB_BASE / 2) != | 839 if ((probability < REG_BR_PROB_BASE / 2) != |
839 (probability2 < REG_BR_PROB_BASE / 2)) | 840 (probability2 < REG_BR_PROB_BASE / 2)) |
840 break; | 841 break; |
841 | 842 |
842 /* If the same predictor later gave better result, go for it! */ | 843 /* If the same predictor later gave better result, go for it! */ |
843 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability)) | 844 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability)) |
886 | 887 |
887 if (preds) | 888 if (preds) |
888 { | 889 { |
889 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) | 890 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next) |
890 { | 891 { |
891 int predictor = pred->ep_predictor; | 892 enum br_predictor predictor = pred->ep_predictor; |
892 int probability = pred->ep_probability; | 893 int probability = pred->ep_probability; |
893 | 894 |
894 if (pred->ep_edge != EDGE_SUCC (bb, 0)) | 895 if (pred->ep_edge != EDGE_SUCC (bb, 0)) |
895 probability = REG_BR_PROB_BASE - probability; | 896 probability = REG_BR_PROB_BASE - probability; |
896 dump_prediction (dump_file, predictor, probability, bb, | 897 dump_prediction (dump_file, predictor, probability, bb, |
911 static void | 912 static void |
912 predict_loops (void) | 913 predict_loops (void) |
913 { | 914 { |
914 loop_iterator li; | 915 loop_iterator li; |
915 struct loop *loop; | 916 struct loop *loop; |
916 | |
917 scev_initialize (); | |
918 | 917 |
919 /* Try to predict out blocks in a loop that are not part of a | 918 /* Try to predict out blocks in a loop that are not part of a |
920 natural loop. */ | 919 natural loop. */ |
921 FOR_EACH_LOOP (li, loop, 0) | 920 FOR_EACH_LOOP (li, loop, 0) |
922 { | 921 { |
1020 | 1019 |
1021 We limit the minimal probability by 2% to avoid | 1020 We limit the minimal probability by 2% to avoid |
1022 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction | 1021 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction |
1023 as this was causing regression in perl benchmark containing such | 1022 as this was causing regression in perl benchmark containing such |
1024 a wide loop. */ | 1023 a wide loop. */ |
1025 | 1024 |
1026 int probability = ((REG_BR_PROB_BASE | 1025 int probability = ((REG_BR_PROB_BASE |
1027 - predictor_info [(int) PRED_LOOP_EXIT].hitrate) | 1026 - predictor_info [(int) PRED_LOOP_EXIT].hitrate) |
1028 / n_exits); | 1027 / n_exits); |
1029 if (probability < HITRATE (2)) | 1028 if (probability < HITRATE (2)) |
1030 probability = HITRATE (2); | 1029 probability = HITRATE (2); |
1032 if (e->dest->index < NUM_FIXED_BLOCKS | 1031 if (e->dest->index < NUM_FIXED_BLOCKS |
1033 || !flow_bb_inside_loop_p (loop, e->dest)) | 1032 || !flow_bb_inside_loop_p (loop, e->dest)) |
1034 predict_edge (e, PRED_LOOP_EXIT, probability); | 1033 predict_edge (e, PRED_LOOP_EXIT, probability); |
1035 } | 1034 } |
1036 } | 1035 } |
1037 | 1036 |
1038 /* Free basic blocks from get_loop_body. */ | 1037 /* Free basic blocks from get_loop_body. */ |
1039 free (bbs); | 1038 free (bbs); |
1040 } | 1039 } |
1041 | |
1042 scev_finalize (); | |
1043 } | 1040 } |
1044 | 1041 |
1045 /* Attempt to predict probabilities of BB outgoing edges using local | 1042 /* Attempt to predict probabilities of BB outgoing edges using local |
1046 properties. */ | 1043 properties. */ |
1047 static void | 1044 static void |
1263 return NULL; | 1260 return NULL; |
1264 } | 1261 } |
1265 return NULL; | 1262 return NULL; |
1266 } | 1263 } |
1267 | 1264 |
1268 /* Return constant EXPR will likely have at execution time, NULL if unknown. | 1265 /* Return constant EXPR will likely have at execution time, NULL if unknown. |
1269 The function is used by builtin_expect branch predictor so the evidence | 1266 The function is used by builtin_expect branch predictor so the evidence |
1270 must come from this construct and additional possible constant folding. | 1267 must come from this construct and additional possible constant folding. |
1271 | 1268 |
1272 We may want to implement more involved value guess (such as value range | 1269 We may want to implement more involved value guess (such as value range |
1273 propagation based prediction), but such tricks shall go to new | 1270 propagation based prediction), but such tricks shall go to new |
1274 implementation. */ | 1271 implementation. */ |
1275 | 1272 |
1276 static tree | 1273 static tree |
1604 gcc_assert (!*value); | 1601 gcc_assert (!*value); |
1605 return false; | 1602 return false; |
1606 } | 1603 } |
1607 #endif | 1604 #endif |
1608 | 1605 |
1609 /* Predict branch probabilities and estimate profile of the tree CFG. */ | 1606 /* Predict branch probabilities and estimate profile for basic block BB. */ |
1610 static unsigned int | 1607 |
1608 static void | |
1609 tree_estimate_probability_bb (basic_block bb) | |
1610 { | |
1611 edge e; | |
1612 edge_iterator ei; | |
1613 gimple last; | |
1614 | |
1615 FOR_EACH_EDGE (e, ei, bb->succs) | |
1616 { | |
1617 /* Predict early returns to be probable, as we've already taken | |
1618 care for error returns and other cases are often used for | |
1619 fast paths through function. | |
1620 | |
1621 Since we've already removed the return statements, we are | |
1622 looking for CFG like: | |
1623 | |
1624 if (conditional) | |
1625 { | |
1626 .. | |
1627 goto return_block | |
1628 } | |
1629 some other blocks | |
1630 return_block: | |
1631 return_stmt. */ | |
1632 if (e->dest != bb->next_bb | |
1633 && e->dest != EXIT_BLOCK_PTR | |
1634 && single_succ_p (e->dest) | |
1635 && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR | |
1636 && (last = last_stmt (e->dest)) != NULL | |
1637 && gimple_code (last) == GIMPLE_RETURN) | |
1638 { | |
1639 edge e1; | |
1640 edge_iterator ei1; | |
1641 | |
1642 if (single_succ_p (bb)) | |
1643 { | |
1644 FOR_EACH_EDGE (e1, ei1, bb->preds) | |
1645 if (!predicted_by_p (e1->src, PRED_NULL_RETURN) | |
1646 && !predicted_by_p (e1->src, PRED_CONST_RETURN) | |
1647 && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN)) | |
1648 predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
1649 } | |
1650 else | |
1651 if (!predicted_by_p (e->src, PRED_NULL_RETURN) | |
1652 && !predicted_by_p (e->src, PRED_CONST_RETURN) | |
1653 && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN)) | |
1654 predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
1655 } | |
1656 | |
1657 /* Look for block we are guarding (ie we dominate it, | |
1658 but it doesn't postdominate us). */ | |
1659 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb | |
1660 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src) | |
1661 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest)) | |
1662 { | |
1663 gimple_stmt_iterator bi; | |
1664 | |
1665 /* The call heuristic claims that a guarded function call | |
1666 is improbable. This is because such calls are often used | |
1667 to signal exceptional situations such as printing error | |
1668 messages. */ | |
1669 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi); | |
1670 gsi_next (&bi)) | |
1671 { | |
1672 gimple stmt = gsi_stmt (bi); | |
1673 if (is_gimple_call (stmt) | |
1674 /* Constant and pure calls are hardly used to signalize | |
1675 something exceptional. */ | |
1676 && gimple_has_side_effects (stmt)) | |
1677 { | |
1678 predict_edge_def (e, PRED_CALL, NOT_TAKEN); | |
1679 break; | |
1680 } | |
1681 } | |
1682 } | |
1683 } | |
1684 tree_predict_by_opcode (bb); | |
1685 } | |
1686 | |
1687 /* Predict branch probabilities and estimate profile of the tree CFG. | |
1688 This function can be called from the loop optimizers to recompute | |
1689 the profile information. */ | |
1690 | |
1691 void | |
1611 tree_estimate_probability (void) | 1692 tree_estimate_probability (void) |
1612 { | 1693 { |
1613 basic_block bb; | 1694 basic_block bb; |
1614 | |
1615 loop_optimizer_init (0); | |
1616 if (dump_file && (dump_flags & TDF_DETAILS)) | |
1617 flow_loops_dump (dump_file, NULL, 0); | |
1618 | 1695 |
1619 add_noreturn_fake_exit_edges (); | 1696 add_noreturn_fake_exit_edges (); |
1620 connect_infinite_loops_to_exit (); | 1697 connect_infinite_loops_to_exit (); |
1621 /* We use loop_niter_by_eval, which requires that the loops have | 1698 /* We use loop_niter_by_eval, which requires that the loops have |
1622 preheaders. */ | 1699 preheaders. */ |
1623 create_preheaders (CP_SIMPLE_PREHEADERS); | 1700 create_preheaders (CP_SIMPLE_PREHEADERS); |
1624 calculate_dominance_info (CDI_POST_DOMINATORS); | 1701 calculate_dominance_info (CDI_POST_DOMINATORS); |
1625 | 1702 |
1626 bb_predictions = pointer_map_create (); | 1703 bb_predictions = pointer_map_create (); |
1627 tree_bb_level_predictions (); | 1704 tree_bb_level_predictions (); |
1628 | |
1629 mark_irreducible_loops (); | |
1630 record_loop_exits (); | 1705 record_loop_exits (); |
1706 | |
1631 if (number_of_loops () > 1) | 1707 if (number_of_loops () > 1) |
1632 predict_loops (); | 1708 predict_loops (); |
1633 | 1709 |
1634 FOR_EACH_BB (bb) | 1710 FOR_EACH_BB (bb) |
1635 { | 1711 tree_estimate_probability_bb (bb); |
1636 edge e; | 1712 |
1637 edge_iterator ei; | |
1638 gimple last; | |
1639 | |
1640 FOR_EACH_EDGE (e, ei, bb->succs) | |
1641 { | |
1642 /* Predict early returns to be probable, as we've already taken | |
1643 care for error returns and other cases are often used for | |
1644 fast paths through function. | |
1645 | |
1646 Since we've already removed the return statements, we are | |
1647 looking for CFG like: | |
1648 | |
1649 if (conditional) | |
1650 { | |
1651 .. | |
1652 goto return_block | |
1653 } | |
1654 some other blocks | |
1655 return_block: | |
1656 return_stmt. */ | |
1657 if (e->dest != bb->next_bb | |
1658 && e->dest != EXIT_BLOCK_PTR | |
1659 && single_succ_p (e->dest) | |
1660 && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR | |
1661 && (last = last_stmt (e->dest)) != NULL | |
1662 && gimple_code (last) == GIMPLE_RETURN) | |
1663 { | |
1664 edge e1; | |
1665 edge_iterator ei1; | |
1666 | |
1667 if (single_succ_p (bb)) | |
1668 { | |
1669 FOR_EACH_EDGE (e1, ei1, bb->preds) | |
1670 if (!predicted_by_p (e1->src, PRED_NULL_RETURN) | |
1671 && !predicted_by_p (e1->src, PRED_CONST_RETURN) | |
1672 && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN)) | |
1673 predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
1674 } | |
1675 else | |
1676 if (!predicted_by_p (e->src, PRED_NULL_RETURN) | |
1677 && !predicted_by_p (e->src, PRED_CONST_RETURN) | |
1678 && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN)) | |
1679 predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN); | |
1680 } | |
1681 | |
1682 /* Look for block we are guarding (ie we dominate it, | |
1683 but it doesn't postdominate us). */ | |
1684 if (e->dest != EXIT_BLOCK_PTR && e->dest != bb | |
1685 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src) | |
1686 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest)) | |
1687 { | |
1688 gimple_stmt_iterator bi; | |
1689 | |
1690 /* The call heuristic claims that a guarded function call | |
1691 is improbable. This is because such calls are often used | |
1692 to signal exceptional situations such as printing error | |
1693 messages. */ | |
1694 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi); | |
1695 gsi_next (&bi)) | |
1696 { | |
1697 gimple stmt = gsi_stmt (bi); | |
1698 if (is_gimple_call (stmt) | |
1699 /* Constant and pure calls are hardly used to signalize | |
1700 something exceptional. */ | |
1701 && gimple_has_side_effects (stmt)) | |
1702 { | |
1703 predict_edge_def (e, PRED_CALL, NOT_TAKEN); | |
1704 break; | |
1705 } | |
1706 } | |
1707 } | |
1708 } | |
1709 tree_predict_by_opcode (bb); | |
1710 } | |
1711 FOR_EACH_BB (bb) | 1713 FOR_EACH_BB (bb) |
1712 combine_predictions_for_bb (bb); | 1714 combine_predictions_for_bb (bb); |
1713 | 1715 |
1714 #ifdef ENABLE_CHECKING | 1716 #ifdef ENABLE_CHECKING |
1715 pointer_map_traverse (bb_predictions, assert_is_empty, NULL); | 1717 pointer_map_traverse (bb_predictions, assert_is_empty, NULL); |
1718 bb_predictions = NULL; | 1720 bb_predictions = NULL; |
1719 | 1721 |
1720 estimate_bb_frequencies (); | 1722 estimate_bb_frequencies (); |
1721 free_dominance_info (CDI_POST_DOMINATORS); | 1723 free_dominance_info (CDI_POST_DOMINATORS); |
1722 remove_fake_exit_edges (); | 1724 remove_fake_exit_edges (); |
1725 } | |
1726 | |
1727 /* Predict branch probabilities and estimate profile of the tree CFG. | |
1728 This is the driver function for PASS_PROFILE. */ | |
1729 | |
1730 static unsigned int | |
1731 tree_estimate_probability_driver (void) | |
1732 { | |
1733 unsigned nb_loops; | |
1734 | |
1735 loop_optimizer_init (0); | |
1736 if (dump_file && (dump_flags & TDF_DETAILS)) | |
1737 flow_loops_dump (dump_file, NULL, 0); | |
1738 | |
1739 mark_irreducible_loops (); | |
1740 | |
1741 nb_loops = number_of_loops (); | |
1742 if (nb_loops > 1) | |
1743 scev_initialize (); | |
1744 | |
1745 tree_estimate_probability (); | |
1746 | |
1747 if (nb_loops > 1) | |
1748 scev_finalize (); | |
1749 | |
1723 loop_optimizer_finalize (); | 1750 loop_optimizer_finalize (); |
1724 if (dump_file && (dump_flags & TDF_DETAILS)) | 1751 if (dump_file && (dump_flags & TDF_DETAILS)) |
1725 gimple_dump_cfg (dump_file, dump_flags); | 1752 gimple_dump_cfg (dump_file, dump_flags); |
1726 if (profile_status == PROFILE_ABSENT) | 1753 if (profile_status == PROFILE_ABSENT) |
1727 profile_status = PROFILE_GUESSED; | 1754 profile_status = PROFILE_GUESSED; |
1902 | 1929 |
1903 e = find_edge (bb, head); | 1930 e = find_edge (bb, head); |
1904 if (e) | 1931 if (e) |
1905 { | 1932 { |
1906 sreal tmp; | 1933 sreal tmp; |
1907 | 1934 |
1908 /* EDGE_INFO (e)->back_edge_prob | 1935 /* EDGE_INFO (e)->back_edge_prob |
1909 = ((e->probability * BLOCK_INFO (bb)->frequency) | 1936 = ((e->probability * BLOCK_INFO (bb)->frequency) |
1910 / REG_BR_PROB_BASE); */ | 1937 / REG_BR_PROB_BASE); */ |
1911 | 1938 |
1912 sreal_init (&tmp, e->probability, 0); | 1939 sreal_init (&tmp, e->probability, 0); |
1913 sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency); | 1940 sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency); |
1914 sreal_mul (&EDGE_INFO (e)->back_edge_prob, | 1941 sreal_mul (&EDGE_INFO (e)->back_edge_prob, |
1915 &tmp, &real_inv_br_prob_base); | 1942 &tmp, &real_inv_br_prob_base); |
1916 } | 1943 } |
1925 { | 1952 { |
1926 if (!nextbb) | 1953 if (!nextbb) |
1927 nextbb = e->dest; | 1954 nextbb = e->dest; |
1928 else | 1955 else |
1929 BLOCK_INFO (last)->next = e->dest; | 1956 BLOCK_INFO (last)->next = e->dest; |
1930 | 1957 |
1931 last = e->dest; | 1958 last = e->dest; |
1932 } | 1959 } |
1933 } | 1960 } |
1934 } | 1961 } |
1935 } | 1962 } |
1991 counts_to_freqs (void) | 2018 counts_to_freqs (void) |
1992 { | 2019 { |
1993 gcov_type count_max, true_count_max = 0; | 2020 gcov_type count_max, true_count_max = 0; |
1994 basic_block bb; | 2021 basic_block bb; |
1995 | 2022 |
1996 FOR_EACH_BB (bb) | 2023 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
1997 true_count_max = MAX (bb->count, true_count_max); | 2024 true_count_max = MAX (bb->count, true_count_max); |
1998 | 2025 |
1999 count_max = MAX (true_count_max, 1); | 2026 count_max = MAX (true_count_max, 1); |
2000 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) | 2027 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb) |
2001 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max; | 2028 bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max; |
2115 if (flag_reorder_functions) | 2142 if (flag_reorder_functions) |
2116 choose_function_section (); | 2143 choose_function_section (); |
2117 } | 2144 } |
2118 | 2145 |
2119 /* Decide whether function is hot, cold or unlikely executed. */ | 2146 /* Decide whether function is hot, cold or unlikely executed. */ |
2120 static void | 2147 void |
2121 compute_function_frequency (void) | 2148 compute_function_frequency (void) |
2122 { | 2149 { |
2123 basic_block bb; | 2150 basic_block bb; |
2124 | 2151 |
2125 if (!profile_info || !flag_branch_probabilities) | 2152 if (!profile_info || !flag_branch_probabilities) |
2183 tree | 2210 tree |
2184 build_predict_expr (enum br_predictor predictor, enum prediction taken) | 2211 build_predict_expr (enum br_predictor predictor, enum prediction taken) |
2185 { | 2212 { |
2186 tree t = build1 (PREDICT_EXPR, void_type_node, | 2213 tree t = build1 (PREDICT_EXPR, void_type_node, |
2187 build_int_cst (NULL, predictor)); | 2214 build_int_cst (NULL, predictor)); |
2188 PREDICT_EXPR_OUTCOME (t) = taken; | 2215 SET_PREDICT_EXPR_OUTCOME (t, taken); |
2189 return t; | 2216 return t; |
2190 } | 2217 } |
2191 | 2218 |
2192 const char * | 2219 const char * |
2193 predictor_name (enum br_predictor predictor) | 2220 predictor_name (enum br_predictor predictor) |
2194 { | 2221 { |
2195 return predictor_info[predictor].name; | 2222 return predictor_info[predictor].name; |
2196 } | 2223 } |
2197 | 2224 |
2198 struct gimple_opt_pass pass_profile = | 2225 struct gimple_opt_pass pass_profile = |
2199 { | 2226 { |
2200 { | 2227 { |
2201 GIMPLE_PASS, | 2228 GIMPLE_PASS, |
2202 "profile", /* name */ | 2229 "profile", /* name */ |
2203 gate_estimate_probability, /* gate */ | 2230 gate_estimate_probability, /* gate */ |
2204 tree_estimate_probability, /* execute */ | 2231 tree_estimate_probability_driver, /* execute */ |
2205 NULL, /* sub */ | 2232 NULL, /* sub */ |
2206 NULL, /* next */ | 2233 NULL, /* next */ |
2207 0, /* static_pass_number */ | 2234 0, /* static_pass_number */ |
2208 TV_BRANCH_PROB, /* tv_id */ | 2235 TV_BRANCH_PROB, /* tv_id */ |
2209 PROP_cfg, /* properties_required */ | 2236 PROP_cfg, /* properties_required */ |
2212 0, /* todo_flags_start */ | 2239 0, /* todo_flags_start */ |
2213 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */ | 2240 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */ |
2214 } | 2241 } |
2215 }; | 2242 }; |
2216 | 2243 |
2217 struct gimple_opt_pass pass_strip_predict_hints = | 2244 struct gimple_opt_pass pass_strip_predict_hints = |
2218 { | 2245 { |
2219 { | 2246 { |
2220 GIMPLE_PASS, | 2247 GIMPLE_PASS, |
2221 NULL, /* name */ | 2248 "*strip_predict_hints", /* name */ |
2222 NULL, /* gate */ | 2249 NULL, /* gate */ |
2223 strip_predict_hints, /* execute */ | 2250 strip_predict_hints, /* execute */ |
2224 NULL, /* sub */ | 2251 NULL, /* sub */ |
2225 NULL, /* next */ | 2252 NULL, /* next */ |
2226 0, /* static_pass_number */ | 2253 0, /* static_pass_number */ |