+ gimple dcall_stmt, load_stmt, cond_stmt;
+ tree tmp0, tmp1, tmpv, tmp;
+ basic_block cond_bb, dcall_bb, icall_bb, join_bb;
+ tree optype = build_pointer_type (void_type_node);
+ edge e_cd, e_ci, e_di, e_dj, e_ij;
+ gimple_stmt_iterator gsi;
+ int lp_nr;
+
+ cond_bb = gimple_bb (icall_stmt);
+ gsi = gsi_for_stmt (icall_stmt);
+
+ tmpv = create_tmp_reg (optype, "PROF");
+ tmp0 = make_ssa_name (tmpv, NULL);
+ tmp1 = make_ssa_name (tmpv, NULL);
+ tmp = unshare_expr (gimple_call_fn (icall_stmt));
+ load_stmt = gimple_build_assign (tmp0, tmp);
+ SSA_NAME_DEF_STMT (tmp0) = load_stmt;
+ gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
+
+ tmp = fold_convert (optype, build_addr (direct_call->decl,
+ current_function_decl));
+ load_stmt = gimple_build_assign (tmp1, tmp);
+ SSA_NAME_DEF_STMT (tmp1) = load_stmt;
+ gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
+
+ cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
+ gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
+
+ gimple_set_vdef (icall_stmt, NULL_TREE);
+ gimple_set_vuse (icall_stmt, NULL_TREE);
+ update_stmt (icall_stmt);
+ dcall_stmt = gimple_copy (icall_stmt);
+ gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
+ gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
+
+ /* Fix CFG. */
+ /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
+ e_cd = split_block (cond_bb, cond_stmt);
+ dcall_bb = e_cd->dest;
+ dcall_bb->count = count;
+
+ e_di = split_block (dcall_bb, dcall_stmt);
+ icall_bb = e_di->dest;
+ icall_bb->count = all - count;
+
+ /* Do not disturb existing EH edges from the indirect call. */
+ if (!stmt_ends_bb_p (icall_stmt))
+ e_ij = split_block (icall_bb, icall_stmt);
+ else
+ {
+ e_ij = find_fallthru_edge (icall_bb->succs);
+ e_ij->probability = REG_BR_PROB_BASE;
+ e_ij->count = all - count;
+ e_ij = single_pred_edge (split_edge (e_ij));
+ }
+ join_bb = e_ij->dest;
+ join_bb->count = all;
+
+ e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
+ e_cd->probability = prob;
+ e_cd->count = count;
+
+ e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
+ e_ci->probability = REG_BR_PROB_BASE - prob;
+ e_ci->count = all - count;
+
+ remove_edge (e_di);
+
+ e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
+ e_dj->probability = REG_BR_PROB_BASE;
+ e_dj->count = count;
+
+ e_ij->probability = REG_BR_PROB_BASE;
+ e_ij->count = all - count;
+
+ /* Insert PHI node for the call result if necessary. */
+ if (gimple_call_lhs (icall_stmt)
+ && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME)
+ {
+ tree result = gimple_call_lhs (icall_stmt);
+ gimple phi = create_phi_node (result, join_bb);
+ SSA_NAME_DEF_STMT (result) = phi;
+ gimple_call_set_lhs (icall_stmt,
+ make_ssa_name (SSA_NAME_VAR (result), icall_stmt));
+ add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
+ gimple_call_set_lhs (dcall_stmt,
+ make_ssa_name (SSA_NAME_VAR (result), dcall_stmt));
+ add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
+ }
+
+ /* Build an EH edge for the direct call if necessary. */
+ lp_nr = lookup_stmt_eh_lp (icall_stmt);
+ if (lp_nr != 0
+ && stmt_could_throw_p (dcall_stmt))
+ {
+ edge e_eh, e;
+ edge_iterator ei;
+ gimple_stmt_iterator psi;
+
+ add_stmt_to_eh_lp (dcall_stmt, lp_nr);
+ FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
+ if (e_eh->flags & EDGE_EH)
+ break;
+ e = make_edge (dcall_bb, e_eh->dest, EDGE_EH);
+ for (psi = gsi_start_phis (e_eh->dest);
+ !gsi_end_p (psi); gsi_next (&psi))
+ {
+ gimple phi = gsi_stmt (psi);
+ SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
+ PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
+ }
+ }
+
+ return dcall_stmt;
+}
+
+/*
+ For every checked indirect/virtual call determine if most common pid of
+ function/class method has probability more than 50%. If yes modify code of
+ this call to:
+ */
+
+static bool
+gimple_ic_transform (gimple stmt)
+{
+ histogram_value histogram;
+ gcov_type val, count, all, bb_all;
+ gcov_type prob;
+ tree callee;
+ gimple modify;
+ struct cgraph_node *direct_call;
+
+ if (gimple_code (stmt) != GIMPLE_CALL)
+ return false;
+
+ callee = gimple_call_fn (stmt);
+
+ if (TREE_CODE (callee) == FUNCTION_DECL)
+ return false;
+
+ histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
+ if (!histogram)
+ return false;
+
+ val = histogram->hvalue.counters [0];
+ count = histogram->hvalue.counters [1];
+ all = histogram->hvalue.counters [2];
+ gimple_remove_histogram_value (cfun, stmt, histogram);
+
+ if (4 * count <= 3 * all)
+ return false;
+
+ bb_all = gimple_bb (stmt)->count;
+ /* The order of CHECK_COUNTER calls is important -
+ since check_counter can correct the third parameter
+ and we want to make count <= all <= bb_all. */
+ if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
+ || check_counter (stmt, "ic", &count, &all, all))
+ return false;
+
+ if (all > 0)
+ prob = (count * REG_BR_PROB_BASE + all / 2) / all;
+ else
+ prob = 0;
+ direct_call = find_func_by_pid ((int)val);
+
+ if (direct_call == NULL)
+ return false;
+
+ modify = gimple_ic (stmt, direct_call, prob, count, all);
+
+ if (dump_file)
+ {
+ fprintf (dump_file, "Indirect call -> direct call ");
+ print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
+ fprintf (dump_file, "=> ");
+ print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
+ fprintf (dump_file, " transformation on insn ");
+ print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
+ fprintf (dump_file, " to ");
+ print_gimple_stmt (dump_file, modify, 0, TDF_SLIM);
+ fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
+ " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
+ }
+
+ return true;