#include "gaussseidel.h"
-#include "firm_common_t.h"
#include "set.h"
#include "hashptr.h"
#include "debug.h"
#include "irloop.h"
#include "irgwalk.h"
#include "iredges.h"
+#include "irouts.h"
#include "irprintf.h"
#include "irtools.h"
#include "irhooks.h"
return x;
}
+/*
+ * Determine probability that predecessor pos takes this cf edge.
+ */
static double
get_cf_probability(ir_node *bb, int pos, double loop_weight)
{
double sum = 0.0;
- double cur = 0.0;
+ double cur = 1.0;
+ double inv_loop_weight = 1./loop_weight;
const ir_node *pred = get_Block_cfgpred_block(bb, pos);
const ir_loop *pred_loop;
int pred_depth;
const ir_edge_t *edge;
+ const ir_loop *loop;
+ int depth;
+ int d;
if (is_Bad(pred))
return 0;
+ loop = get_irn_loop(bb);
+ depth = get_loop_depth(loop);
pred_loop = get_irn_loop(pred);
pred_depth = get_loop_depth(pred_loop);
- cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
+ for (d = depth; d < pred_depth; ++d) {
+ cur *= inv_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;
+ const ir_node *succ = get_edge_src_irn(edge);
+ const ir_loop *succ_loop = get_irn_loop(succ);
+ int succ_depth = get_loop_depth(succ_loop);
+
+ double fac = 1.0;
+ for (d = succ_depth; d < pred_depth; ++d) {
+ fac *= inv_loop_weight;
+ }
+ sum += fac;
}
return cur/sum;
execfreq->set = new_set(cmp_freq, 32);
memset(&execfreq->hook, 0, sizeof(execfreq->hook));
+
+ // set reasonable values to convert double execfreq to ulong execfreq
+ execfreq->m = 1.0;
+
execfreq->hook.context = execfreq;
execfreq->hook.hook._hook_node_info = exec_freq_node_info;
register_hook(hook_node_info, &execfreq->hook);
{
gs_matrix_t *mat;
int size;
+ int n_keepalives;
int idx;
freq_t *freq, *s, *e;
ir_exec_freq *ef;
+ ir_node *end = get_irg_end(irg);
set *freqs;
dfs_t *dfs;
double *x;
freq = set_insert_freq(freqs, bb);
freq->idx = idx;
+ /* Sum of (execution frequency of predecessor * probability of cf edge) ... */
for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
ir_node *pred = get_Block_cfgpred_block(bb, i);
int pred_idx = size - dfs_get_post_num(dfs, pred) - 1;
gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
}
+ /* ... equals my execution frequency */
gs_matrix_set(mat, idx, idx, -1.0);
}
dfs_free(dfs);
/*
- * Add a loop from end to start.
+ * Add an edge from end to start.
* The problem is then an eigenvalue problem:
* Solve A*x = 1*x => (A-I)x = 0
*/
s = set_find_freq(freqs, get_irg_start_block(irg));
+
e = set_find_freq(freqs, get_irg_end_block(irg));
if (e->idx >= 0)
gs_matrix_set(mat, s->idx, e->idx, 1.0);
+ /*
+ * Also add an edge for each kept block to start.
+ *
+ * This avoid strange results for e.g. an irg containing a exit()-call
+ * which block has no cfg successor.
+ */
+ n_keepalives = get_End_n_keepalives(end);
+ for (idx = n_keepalives - 1; idx >= 0; --idx) {
+ ir_node *keep = get_End_keepalive(end, idx);
+
+ if (is_Block(keep) && get_Block_n_cfg_outs(keep) == 0) {
+ freq_t *k = set_find_freq(freqs, keep);
+ if (k->idx >= 0)
+ gs_matrix_set(mat, s->idx, k->idx, 1.0);
+ }
+ }
+
/* solve the system and delete the matrix */
solve_lgs(mat, x, size);
gs_delete_matrix(mat);