+ predict_insn (last_insn, predictor, probability);
+}
+
+/* Predict edge E by given predictor if possible. */
+void
+predict_edge_def (e, predictor, taken)
+ edge e;
+ enum br_predictor predictor;
+ enum prediction taken;
+{
+ int probability = predictor_info[(int) predictor].hitrate;
+
+ if (taken != TAKEN)
+ probability = REG_BR_PROB_BASE - probability;
+ predict_edge (e, predictor, probability);
+}
+
+/* Invert all branch predictions or probability notes in the INSN. This needs
+ to be done each time we invert the condition used by the jump. */
+void
+invert_br_probabilities (insn)
+ rtx insn;
+{
+ rtx note = REG_NOTES (insn);
+
+ while (note)
+ {
+ if (REG_NOTE_KIND (note) == REG_BR_PROB)
+ XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
+ else if (REG_NOTE_KIND (note) == REG_BR_PRED)
+ XEXP (XEXP (note, 0), 1)
+ = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
+ note = XEXP (note, 1);
+ }
+}
+
+/* Dump information about the branch prediction to the output file. */
+static void
+dump_prediction (predictor, probability, bb)
+ enum br_predictor predictor;
+ int probability;
+ basic_block bb;
+{
+ edge e = bb->succ;
+
+ if (!rtl_dump_file)
+ return;
+
+ while (e->flags & EDGE_FALLTHRU)
+ e = e->succ_next;
+
+ fprintf (rtl_dump_file, " %s heuristics: %.1f%%",
+ predictor_info[predictor].name,
+ probability * 100.0 / REG_BR_PROB_BASE);
+
+ if (bb->count)
+ fprintf (rtl_dump_file, " exec %i hit %i (%.1f%%)",
+ bb->count, e->count, e->count * 100.0 / bb->count);
+ fprintf (rtl_dump_file, "\n");
+}
+
+/* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
+ note if not already present. Remove now useless REG_BR_PRED notes. */
+static void
+combine_predictions_for_insn (insn, bb)
+ rtx insn;
+ basic_block bb;
+{
+ rtx prob_note = find_reg_note (insn, REG_BR_PROB, 0);
+ rtx *pnote = ®_NOTES (insn);
+ int best_probability = PROB_EVEN;
+ int best_predictor = END_PREDICTORS;
+
+ if (rtl_dump_file)
+ fprintf (rtl_dump_file, "Predictions for insn %i\n", INSN_UID (insn));
+
+ /* We implement "first match" heuristics and use probability guessed
+ by predictor with smallest index. In future we will use better
+ probability combination techniques. */
+ while (*pnote)
+ {
+ rtx *next_pnote = &XEXP (*pnote, 1);
+ if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
+ {
+ int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
+ int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
+
+ dump_prediction (predictor, probability, bb);
+ if (best_predictor > predictor)
+ best_probability = probability, best_predictor = predictor;
+ *pnote = XEXP (*pnote, 1);
+ }
+ pnote = next_pnote;
+ }
+ dump_prediction (PRED_FIRST_MATCH, best_probability, bb);
+ if (!prob_note)
+ {
+ REG_NOTES (insn)
+ = gen_rtx_EXPR_LIST (REG_BR_PROB,
+ GEN_INT (best_probability), REG_NOTES (insn));
+ }