for i in *.[ch]; do sed -e 's/Copyrigth/Copyright/g' -i modeconv.h; done
[libfirm] / ir / ana / execfreq.c
index 5b3833c..3927724 100644 (file)
@@ -1,15 +1,29 @@
 /*
- * Project:     libFIRM
- * File name:   ir/ana/execfreq.c
- * Purpose:     Compute an estimate of basic block executions.
- * Author:      Adam M. Szalkowski
- * Modified by:
- * Created:     28.05.2006
- * CVS-ID:      $Id$
- * Copyright:   (c) 2006 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
+/**
+ * @file
+ * @brief       Compute an estimate of basic block executions.
+ * @author      Adam M. Szalkowski
+ * @date        28.05.2006
+ * @version     $Id$
+ */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -28,8 +42,6 @@
 #include "gaussjordan.h"
 #endif
 
-#include "execfreq.h"
-
 #include "firm_common_t.h"
 #include "set.h"
 #include "hashptr.h"
@@ -39,7 +51,7 @@
 #include "irnode_t.h"
 #include "irloop.h"
 #include "irgwalk.h"
-#include "irouts.h"
+#include "iredges.h"
 #include "irprintf.h"
 #include "irhooks.h"
 
@@ -124,7 +136,8 @@ get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
        return res;
 }
 
-#define ZERO(x)   (fabs(x) < 0.0001)
+#define EPSILON                0.0001
+#define UNDEF(x)    !(x > EPSILON)
 
 static void
 block_walker(ir_node * bb, void * data)
@@ -174,20 +187,23 @@ solve_lgs(double * A, double * b, size_t size)
 static double
 get_cf_probability(ir_node *bb, int pos, double loop_weight)
 {
-  double  sum = 0.0;
-  double  cur = 0.0;
-  int     i;
-  ir_node *pred = get_Block_cfgpred_block(bb, pos);
-
-  cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
-
-  for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
-    ir_node *succ = get_Block_cfg_out(pred, i);
-
-    sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
-  }
+       double  sum = 0.0;
+       double  cur = 0.0;
+       const ir_node *pred = get_Block_cfgpred_block(bb, pos);
+       const ir_loop *pred_loop = get_irn_loop(pred);
+       int pred_depth = get_loop_depth(pred_loop);
+       const ir_edge_t *edge;
+
+       cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
+
+       foreach_block_succ(pred, edge) {
+               const ir_node *block = get_edge_src_irn(edge);
+               const ir_loop *loop = get_irn_loop(block);
+               int depth = get_loop_depth(loop);
+               sum += depth < pred_depth ? 1.0 : loop_weight;
+       }
 
-  return cur/sum;
+       return cur/sum;
 }
 
 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
@@ -241,6 +257,7 @@ compute_execfreq(ir_graph * irg, double loop_weight)
        freqs = ef->set = new_set(cmp_freq, 32);
 
        construct_cf_backedges(irg);
+       edges_assure(irg);
 
        wd.idx = 0;
        wd.set = freqs;
@@ -286,9 +303,9 @@ compute_execfreq(ir_graph * irg, double loop_weight)
                size_t        idx = PTR_TO_INT(get_irn_link(bb));
 
 #ifdef USE_GSL
-               freq->freq = ZERO(gsl_vector_get(x, idx)) ? 0.0 : gsl_vector_get(x, idx);
+               freq->freq = UNDEF(gsl_vector_get(x, idx)) ? EPSILON : gsl_vector_get(x, idx);
 #else
-               freq->freq = ZERO(x[idx]) ? 0.0 : x[idx];
+               freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx];
 #endif
 
                /* get the maximum exec freq */
@@ -325,7 +342,7 @@ compute_execfreq(ir_graph * irg, double loop_weight)
                        for(j = i + 1; j < n; ++j) {
                                double diff = fabs(fs[i] - fs[j]);
 
-                               if(!ZERO(diff))
+                               if(!UNDEF(diff))
                                        smallest_diff = MIN(diff, smallest_diff);
                        }
                }
@@ -369,5 +386,3 @@ free_execfreq(ir_exec_freq *ef)
        unregister_hook(hook_node_info, &ef->hook);
        free(ef);
 }
-
-#undef ELEM