+/*------------------------------------------------------------------*/
+/* A algorithm that precomputes whether Conds lead to an exception.
+ * Computes a field for all Projs from Conds that says the following:
+ * - The Proj projs from a normal dual Cond with probability 50:50
+ * - This Proj of the Cond leads to an exception, i.e., a raise node.
+ * It is taken with exception probability.
+ * - The Proj of the Cond avoids an exception. It is taken with
+ * 1 - exception probability. */
+/*------------------------------------------------------------------*/
+
+#include "irouts.h"
+
+typedef enum {
+ Cond_prob_none,
+ Cond_prob_normal,
+ Cond_prob_avoid_exception,
+ Cond_prob_exception_taken,
+ Cond_prob_was_exception_taken,
+} Cond_prob;
+
+static int just_passed_a_Raise = 0;
+static ir_node *Cond_list = NULL;
+
+/* We do not use an extra set, as Projs are not yet in the existing one. */
+void set_ProjX_probability(ir_node *n, Cond_prob prob) {
+ reg_exec_freq ef;
+ ef.reg = n;
+ ef.prob = prob;
+ set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
+}
+
+Cond_prob get_ProjX_probability(ir_node *n) {
+ reg_exec_freq ef, *found;
+ ef.reg = n;
+
+ found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
+
+ if (found)
+ return (Cond_prob)found->prob;
+ else
+ return Cond_prob_none;
+}
+
+/* A walker that only visits the nodes we want to see. */
+
+static void
+my_irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env) {
+ int i;
+ set_irn_visited(node, current_ir_graph->visited);
+
+ pre(node, env);
+
+ if (node->op != op_Block) {
+ ir_node *pred;
+ if (node->op == op_Proj)
+ pred = get_irn_n(node, 0);
+ else
+ pred = get_irn_n(node, -1);
+ if (pred->visited < current_ir_graph->visited)
+ my_irg_walk_2_both(pred, pre, post, env);
+ }
+
+ else { /* a Block */
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_n(node, i);
+ if (pred->visited < current_ir_graph->visited)
+ my_irg_walk_2_both(pred, pre, post, env);
+ }
+ }
+
+ if (node->op == op_End) {
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_n(node, i);
+ if ((pred->op == op_Block) && (pred->visited < current_ir_graph->visited))
+ my_irg_walk_2_both(pred, pre, post, env);
+ }
+ }
+
+ post(node, env);
+}
+static void my_irg_walk_current_graph(irg_walk_func *pre, irg_walk_func *post, void *env) {
+ inc_irg_visited(current_ir_graph);
+ my_irg_walk_2_both(get_irg_end(current_ir_graph), pre, post, env);
+}
+
+
+static void walk_pre(ir_node *n, void *env) {
+
+ if (get_irn_op(n) == op_Raise)
+ just_passed_a_Raise = 1;
+
+ if ( (get_irn_op(n) == op_Proj)
+ && (get_irn_op(get_Proj_pred(n)) == op_Cond)
+ && (just_passed_a_Raise)) {
+ ir_node *c = get_Proj_pred(n);
+
+ /* If we already visited the other Proj, and it also leads to a Raise,
+ we are in the middle of something. Continue searching. */
+ assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
+ ir_node *other_proj = get_irn_out(c, 0);
+ if (other_proj == n) other_proj = get_irn_out(c, 1);
+ if (get_ProjX_probability(other_proj) == Cond_prob_exception_taken) {
+ set_ProjX_probability(other_proj, Cond_prob_was_exception_taken);
+ /* Keep searching for the Proj, so keep just_passed_a_Raise. */
+ } else {
+ set_ProjX_probability(n, Cond_prob_exception_taken);
+ just_passed_a_Raise = 0;
+ }
+ }
+
+ if (get_irn_op(n) == op_Cond) {
+ set_irn_link(n, Cond_list);
+ Cond_list = n;
+ }
+}
+
+static void walk_post(ir_node *n, void *env) {
+
+ if (get_irn_op(n) == op_Raise)
+ just_passed_a_Raise = 0;
+
+ if ( (get_irn_op(n) == op_Proj)
+ && (get_irn_op(get_Proj_pred(n)) == op_Cond)
+ && ((get_ProjX_probability(n) == Cond_prob_exception_taken) ||
+ (get_ProjX_probability(n) == Cond_prob_was_exception_taken) )) {
+ just_passed_a_Raise = 1;
+ }
+}
+
+/** Precompute which Conds test for an exception.
+ *
+ * Operates on current_ir_graph. */
+void precompute_cond_evaluation(void) {
+ ir_node *c;
+
+ compute_outs(current_ir_graph);
+
+ just_passed_a_Raise = 0;
+ Cond_list = NULL;
+ my_irg_walk_current_graph(walk_pre, walk_post, NULL);
+
+ for (c = Cond_list; c; c = get_irn_link(c)) {
+ assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
+ ir_node *p0 = get_irn_out(c, 0);
+ ir_node *p1 = get_irn_out(c, 1);
+
+ /* both are exceptions */
+ if ((get_ProjX_probability(p0) == Cond_prob_exception_taken) &&
+ (get_ProjX_probability(p1) == Cond_prob_exception_taken) ) {
+ assert(0 && "I tried to avoid these!");
+ /* It's a */
+ set_ProjX_probability(p0, Cond_prob_normal);
+ set_ProjX_probability(p1, Cond_prob_normal);
+ }
+
+ /* p0 is exception */
+ else if (get_ProjX_probability(p0) == Cond_prob_exception_taken) {
+ set_ProjX_probability(p1, Cond_prob_avoid_exception);
+ }
+
+ /* p1 is exception */
+ else if (get_ProjX_probability(p1) == Cond_prob_exception_taken) {
+ set_ProjX_probability(p0, Cond_prob_avoid_exception);
+ }
+
+ /* none is exception */
+ else {
+ set_ProjX_probability(p0, Cond_prob_normal);
+ set_ProjX_probability(p1, Cond_prob_normal);
+ }
+ }
+}
+
+int is_fragile_Proj(ir_node *n) {
+ return is_Proj(n) && (get_ProjX_probability(n) == Cond_prob_exception_taken);
+}
+