X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fana%2Fexecfreq.c;h=e7a4a7a69fc860c43f9f9da298ca185f48cb5b4e;hb=23de0b554f6c6197bfe137b0dccfc0889cac5d93;hp=7276b0a39566728d6524aa158847e1b2962e3ce9;hpb=8ec60164cb40185b52b43ce075002c18f48db9cf;p=libfirm diff --git a/ir/ana/execfreq.c b/ir/ana/execfreq.c index 7276b0a39..e7a4a7a69 100644 --- a/ir/ana/execfreq.c +++ b/ir/ana/execfreq.c @@ -1,23 +1,39 @@ /* - * 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 -//#define USE_GSL +#undef USE_GSL #include #include +#include +#include #ifdef USE_GSL #include @@ -26,69 +42,105 @@ #include "gaussjordan.h" #endif -#include "execfreq.h" - #include "firm_common_t.h" #include "set.h" #include "hashptr.h" +#include "debug.h" #include "irprog_t.h" #include "irgraph_t.h" #include "irnode_t.h" #include "irloop.h" #include "irgwalk.h" -#include "irouts.h" +#include "iredges.h" #include "irprintf.h" +#include "irtools.h" +#include "irhooks.h" #include "execfreq.h" #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s))) +#define MAX_INT_FREQ 1000000 + +typedef struct _freq_t { + const ir_node *irn; + double freq; +} freq_t; + + typedef struct _walkerdata_t { set *set; size_t idx; } walkerdata_t; +struct ir_exec_freq { + set *set; + hook_entry_t hook; + double max; + double min_non_zero; + double m, b; + unsigned infeasible : 1; +}; + static int cmp_freq(const void *a, const void *b, size_t size) { - const freq_t *p = a; - const freq_t *q = b; + const freq_t *p = a; + const freq_t *q = b; + (void) size; - return !(p->irn == q->irn); + return !(p->irn == q->irn); } static freq_t * set_find_freq(set * set, const ir_node * irn) { - freq_t query; + freq_t query; - query.irn = irn; - return set_find(set, &query, sizeof(query), HASH_PTR(irn)); + query.irn = irn; + return set_find(set, &query, sizeof(query), HASH_PTR(irn)); } static freq_t * set_insert_freq(set * set, const ir_node * irn) { - freq_t query; + freq_t query; - query.irn = irn; - query.freq = 0.0; - return set_insert(set, &query, sizeof(query), HASH_PTR(irn)); + query.irn = irn; + query.freq = 0.0; + return set_insert(set, &query, sizeof(query), HASH_PTR(irn)); } double -get_block_execfreq(set * freqs, const ir_node * irn) +get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn) { - assert(is_Block(irn)); + if(!ef->infeasible) { + set *freqs = ef->set; + freq_t *freq; + assert(is_Block(irn)); + freq = set_find_freq(freqs, irn); + assert(freq); + + assert(freq->freq >= 0); + return freq->freq; + } + + return 1.0; +} - freq_t *freq = set_find_freq(freqs, irn); - assert(freq); +unsigned long +get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb) +{ + double f = get_block_execfreq(ef, bb); + int res = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0); - return freq->freq; + // printf("%20.6f %10d\n", f, res); + return res; } -#define ZERO(x) (((x) > 0) ? ((x) < 0.0001) : ((x) > -0.0001)) +#define EPSILON 0.0001 +#define UNDEF(x) !(x > EPSILON) static void block_walker(ir_node * bb, void * data) @@ -133,117 +185,213 @@ solve_lgs(double * A, double * b, size_t size) return NULL; } } -#endif +#endif /* USE_GSL */ static double -get_cf_probability(const ir_node * bb, int pos) +get_cf_probability(ir_node *bb, int pos, double loop_weight) { -#define LOOP_WEIGHT 9.0 + 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; +} - double sum = 0.0; - double cur = 0.0; - int i, - n; - ir_node *pred = get_Block_cfgpred_block(bb, pos); +static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn) +{ + if(is_Block(irn)) { + ir_exec_freq *ef = ctx; + fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn)); + } +} - if(get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred))) { - cur = 1.0; - } else { - cur = LOOP_WEIGHT; - } +ir_exec_freq *create_execfreq(ir_graph *irg) +{ + ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0])); + memset(execfreq, 0, sizeof(execfreq[0])); + execfreq->set = new_set(cmp_freq, 32); - for(i = 0, n = get_Block_n_cfg_outs(pred); i < n; ++i) { - ir_node *succ = get_Block_cfg_out(pred, i); + memset(&execfreq->hook, 0, sizeof(execfreq->hook)); + execfreq->hook.context = execfreq; + execfreq->hook.hook._hook_node_info = exec_freq_node_info; + register_hook(hook_node_info, &execfreq->hook); + (void) irg; - if(get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred))) { - sum += 1.0; - } else { - sum += LOOP_WEIGHT; - } - } + return execfreq; +} - return cur/sum; +void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq) +{ + freq_t *f = set_insert_freq(execfreq->set, block); + f->freq = freq; } -set * -compute_execfreq(ir_graph * irg) +ir_exec_freq * +compute_execfreq(ir_graph * irg, double loop_weight) { - set *freqs = new_set(cmp_freq, 32); - size_t size; - double *matrix; - double *rhs; - size_t i = 0; - freq_t *freq; - walkerdata_t wd; + size_t size; + double *matrix; + double *rhs; + int i; + freq_t *freq; + walkerdata_t wd; + ir_exec_freq *ef; + set *freqs; #ifdef USE_GSL - gsl_vector *x; + gsl_vector *x; #else - double *x; + double *x; #endif - construct_cf_backedges(irg); + ef = xmalloc(sizeof(ef[0])); + memset(ef, 0, sizeof(ef[0])); + ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */ + freqs = ef->set = new_set(cmp_freq, 32); - wd.idx = 0; - wd.set = freqs; + construct_cf_backedges(irg); + /* TODO: edges are corrupt for EDGE_KIND_BLOCK after the local optimize + graph phase merges blocks in the x86 backend */ + edges_deactivate(irg); + edges_activate(irg); + /* edges_assure(irg); */ - irg_block_walk_graph(irg, block_walker, NULL, &wd); + wd.idx = 0; + wd.set = freqs; - size = set_count(freqs); - matrix = malloc(size*size*sizeof(*matrix)); - memset(matrix, 0, size*size*sizeof(*matrix)); - rhs = malloc(size*sizeof(*rhs)); - memset(rhs, 0, size*sizeof(*rhs)); + irg_block_walk_graph(irg, block_walker, NULL, &wd); - set_foreach(freqs, freq) { - const ir_node *bb = freq->irn; - size_t idx = (int)get_irn_link(bb); + size = set_count(freqs); + matrix = xmalloc(size*size*sizeof(*matrix)); + memset(matrix, 0, size*size*sizeof(*matrix)); + rhs = xmalloc(size*sizeof(*rhs)); + memset(rhs, 0, size*sizeof(*rhs)); - matrix[idx*(size+1)] = -1.0; + set_foreach(freqs, freq) { + ir_node *bb = (ir_node *)freq->irn; + size_t idx = (int)get_irn_link(bb); - if(bb == get_irg_start_block(irg)) { - rhs[(int)get_irn_link(bb)] = -1.0; - continue; - } + matrix[idx * (size + 1)] = -1.0; - for(i = 0; i < get_Block_n_cfgpreds(bb); ++i) { - ir_node *pred = get_Block_cfgpred_block(bb, i); - size_t pred_idx = (int)get_irn_link(pred); + if (bb == get_irg_start_block(irg)) { + rhs[(int)get_irn_link(bb)] = -1.0; + continue; + } -// matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred); - matrix[pred_idx + idx*size] += get_cf_probability(bb, i); - } - } + for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) { + ir_node *pred = get_Block_cfgpred_block(bb, i); + size_t pred_idx = (int)get_irn_link(pred); - x = solve_lgs(matrix, rhs, size); - if(x == NULL) { - del_set(freqs); - return NULL; - } + // matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred); + matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight); + } + } + + x = solve_lgs(matrix, rhs, size); + if (x == NULL) { + DEBUG_ONLY(ir_fprintf(stderr, "Debug Warning: Couldn't estimate execution frequencies for %+F\n", irg)); + ef->infeasible = 1; + } else { + ef->max = 0.0; - set_foreach(freqs, freq) { - const ir_node *bb = freq->irn; - size_t idx = PTR_TO_INT(get_irn_link(bb)); + set_foreach(freqs, freq) { + const ir_node *bb = freq->irn; + 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 -// ir_fprintf(stderr, "execfreq %+F: %f\n", bb, freq->freq); - } + + /* get the maximum exec freq */ + ef->max = MAX(ef->max, freq->freq); + + /* Get the minimum non-zero execution frequency. */ + if(freq->freq > 0.0) + ef->min_non_zero = MIN(ef->min_non_zero, freq->freq); + } + + /* compute m and b of the transformation used to convert the doubles into scaled ints */ + { + double smallest_diff = 1.0; + + double l2 = ef->min_non_zero; + double h2 = ef->max; + double l1 = 1.0; + double h1 = MAX_INT_FREQ; + + double *fs = malloc(set_count(freqs) * sizeof(fs[0])); + int i, j, n = 0; + + set_foreach(freqs, freq) + fs[n++] = freq->freq; + + /* + * find the smallest difference of the execution frequencies + * we try to ressolve it with 1 integer. + */ + for(i = 0; i < n; ++i) { + if(fs[i] <= 0.0) + continue; + + for(j = i + 1; j < n; ++j) { + double diff = fabs(fs[i] - fs[j]); + + if(!UNDEF(diff)) + smallest_diff = MIN(diff, smallest_diff); + } + } + + /* according to that the slope of the translation function is 1.0 / smallest diff */ + ef->m = 1.0 / smallest_diff; + + /* the abscissa is then given by */ + ef->b = l1 - ef->m * l2; + + /* + * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ + * set the largest int freq to that upper limit and recompute the translation function + */ + if(ef->m * h2 + ef->b > MAX_INT_FREQ) { + ef->m = (h1 - l1) / (h2 - l2); + ef->b = l1 - ef->m * l2; + } + + // printf("smallest_diff: %g, l1: %f, h1: %f, l2: %f, h2: %f, m: %f, b: %f\n", smallest_diff, l1, h1, l2, h2, ef->m, ef->b); + free(fs); + } #ifdef USE_GSL - gsl_vector_free(x); + gsl_vector_free(x); #endif - free(matrix); + memset(&ef->hook, 0, sizeof(ef->hook)); + ef->hook.context = ef; + ef->hook.hook._hook_node_info = exec_freq_node_info; + register_hook(hook_node_info, &ef->hook); + } - return freqs; + free(matrix); + free(rhs); + + return ef; } void -free_execfreq(set * freqs) +free_execfreq(ir_exec_freq *ef) { - if(freqs) del_set(freqs); + del_set(ef->set); + unregister_hook(hook_node_info, &ef->hook); + free(ef); } - -#undef ELEM