renamed remove_outs,
[libfirm] / ir / ana / execution_frequency.c
index e740c0b..ef54581 100644 (file)
 #include "set.h"
 #include "pdeq.h"
 
-#include "irprog.h"
+#include "irprog_t.h"
+#include "irgraph_t.h"
 #include "irnode_t.h"
 #include "irloop.h"
 #include "irgwalk.h"
 
 #include "interval_analysis.h"
 
+void set_irp_exec_freq_state(exec_freq_state s);
+
 /*------------------------------------------------------------------*/
 /* A hashmap mapping the frequency to block and loop nodes.  Block
  * and loop nodes are regions.                                      */
@@ -58,7 +61,7 @@ static INLINE void set_region_exec_freq(void *reg, double freq) {
   set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
 }
 
-INLINE double get_region_exec_freq(void *reg) {
+double get_region_exec_freq(void *reg) {
   reg_exec_freq ef, *found;
   ef.reg  = reg;
   assert(exec_freq_set);
@@ -149,7 +152,7 @@ my_irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void
     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);
+        my_irg_walk_2_both(pred, pre, post, env);
     }
   }
 
@@ -157,7 +160,7 @@ my_irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void
     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);
+        my_irg_walk_2_both(pred, pre, post, env);
     }
   }
 
@@ -177,12 +180,13 @@ static void walk_pre(ir_node *n, void *env) {
   if (   (get_irn_op(n) == op_Proj)
       && (get_irn_op(get_Proj_pred(n)) == op_Cond)
       && (just_passed_a_Raise)) {
+    ir_node *other_proj;
     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);
+    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);
@@ -218,20 +222,22 @@ static void walk_post(ir_node *n, void *env) {
 void precompute_cond_evaluation(void) {
   ir_node *c;
 
-  compute_outs(current_ir_graph);
+  compute_irg_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)) {
+    ir_node *p0, *p1;
+
     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);
+    p0 = get_irn_out(c, 0);
+    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)   ) {
+        (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);
@@ -261,7 +267,7 @@ int is_fragile_Proj(ir_node *n) {
 }
 
 /*------------------------------------------------------------------*/
-/* The algorithm to compute the execution freqencies.
+/* The algorithm to compute the execution frequencies.
  *
  * Walk the control flow loop tree which we consider the interval
  * tree.  Compute the execution for the lowest loop, add inner loops
@@ -346,7 +352,7 @@ static void compute_frequency(int default_loop_weight) {
     ir_loop *l = (ir_loop *)pdeq_getl(block_worklist);
     int i, n_elems = get_loop_n_elements(l);
 
-    /* The header is initialized with the freqency of the full loop times the iteration weight. */
+    /* The header is initialized with the frequency of the full loop times the iteration weight. */
     check_proper_head(l, get_loop_element(l, 0).son);
 
     for (i = 0; i < n_elems; ++i) {
@@ -374,6 +380,10 @@ void compute_execution_frequency(ir_graph *irg, int default_loop_weight, double
   construct_intervals(current_ir_graph);
   compute_frequency(default_loop_weight);
 
+  set_irg_exec_freq_state(irg, exec_freq_consistent);
+  if (get_irp_exec_freq_state() == exec_freq_none)
+    set_irp_exec_freq_state(exec_freq_inconsistent);
+
   /*
     dump_loop_tree     (current_ir_graph, "-execfreq");
     dump_ir_block_graph(current_ir_graph, "-execfreq");
@@ -390,10 +400,53 @@ void compute_execution_frequencies(int default_loop_weight, double exception_pro
   for (i = 0; i < n_irgs; ++i) {
     compute_execution_frequency(get_irp_irg(i), default_loop_weight, exception_probability);
   }
+  set_irp_exec_freq_state(exec_freq_consistent);
 }
 
 /** free occupied memory, reset */
 void free_execution_frequency(void) {
+  int i, n_irgs = get_irp_n_irgs();
   free_intervals();
   del_set(exec_freq_set);
+
+  for (i = 0; i < n_irgs; ++i)
+    set_irg_exec_freq_state(get_irp_irg(i), exec_freq_none);
+  set_irp_exec_freq_state(exec_freq_none);
+}
+
+exec_freq_state get_irg_exec_freq_state(ir_graph *irg) {
+  return irg->execfreq_state;
+}
+void            set_irg_exec_freq_state(ir_graph *irg, exec_freq_state s) {
+  if ((get_irp_exec_freq_state() == exec_freq_consistent && s != exec_freq_consistent) ||
+      (get_irp_exec_freq_state() == exec_freq_none       && s != exec_freq_none))
+    irp->execfreq_state = exec_freq_inconsistent;
+  irg->execfreq_state = s;
+}
+
+/* Sets irg and irp exec freq state to inconsistent if it is set to consistent. */
+void            set_irg_exec_freq_state_inconsistent(ir_graph *irg) {
+  if (get_irg_exec_freq_state(irg) == exec_freq_consistent)
+    set_irg_exec_freq_state(irg, exec_freq_inconsistent);
+}
+
+void set_irp_exec_freq_state(exec_freq_state s) {
+  irp->execfreq_state = s;
+}
+
+exec_freq_state get_irp_exec_freq_state(void) {
+  return irp->execfreq_state;
+}
+
+/* Sets irp and all irg exec freq states to inconsistent if it is set to consistent. */
+void            set_irp_exec_freq_state_inconsistent(void) {
+  if (get_irp_exec_freq_state() != exec_freq_none) {
+    int i, n_irgs = get_irp_n_irgs();
+    set_irp_exec_freq_state(exec_freq_inconsistent);
+    for (i = 0; i < n_irgs; ++i) {
+      ir_graph *irg = get_irp_irg(i);
+      if (get_irg_exec_freq_state(irg) != exec_freq_none)
+       irg->execfreq_state = exec_freq_inconsistent;
+    }
+  }
 }