X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fstat%2Fpattern.c;h=9f0a0110e1b8b68b0eb326e9d597d086b42e0214;hb=26b9008643da89a023fd6839ded5b9abf21ef328;hp=00ab38fc656115465ea58afa0ce69ff76605f805;hpb=f430a768a5b70618597871d6bbc1c212d2b325c1;p=libfirm diff --git a/ir/stat/pattern.c b/ir/stat/pattern.c index 00ab38fc6..9f0a0110e 100644 --- a/ir/stat/pattern.c +++ b/ir/stat/pattern.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -21,16 +21,14 @@ * @file * @brief Statistics for Firm. Pattern history. * @author Michael Beck - * @version $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif +#include "config.h" #include #include #include +#include "pattern.h" #include "ident.h" #include "irnode_t.h" #include "irgwalk.h" @@ -40,6 +38,8 @@ #include "counter.h" #include "pattern_dmp.h" #include "hashptr.h" +#include "error.h" +#include "lc_printf.h" /* * just be make some things clear :-), the @@ -52,13 +52,13 @@ typedef pset pset_pattern_entry_t; typedef unsigned char BYTE; /** Maximum size of the pattern store. */ -#define PATTERN_STORE_SIZE 2048 +#define PATTERN_STORE_SIZE 2048 /** * The code buffer. */ -typedef struct _code_buf_t { +typedef struct code_buf_t { BYTE *next; /**< Next byte address to be written. */ BYTE *end; /**< End address of the buffer. */ BYTE *start; /**< Start address of the buffer. */ @@ -87,9 +87,9 @@ enum vlc_code_t { /* * An entry for holding one pattern. */ -typedef struct _pattern_entry_t { +typedef struct pattern_entry_t { counter_t count; /**< Amount of pattern occurance. */ - unsigned len; /**< The length of the VLC encoded buffer. */ + size_t len; /**< The length of the VLC encoded buffer. */ BYTE buf[1]; /**< The buffer containing the VLC encoded pattern. */ } pattern_entry_t; @@ -107,7 +107,7 @@ enum options_t { /** * Pattern info. */ -typedef struct _pattern_info_t { +typedef struct pattern_info_t { int enable; /**< If non-zero, this module is enabled. */ struct obstack obst; /**< An obstack containing the counters. */ HASH_MAP(pattern_entry_t) *pattern_hash; /**< A hash map containing the pattern. */ @@ -125,7 +125,8 @@ static pattern_info_t _status, *status = &_status; /** * Compare two pattern for its occurance counter. */ -static int pattern_count_cmp(const void *elt, const void *key) { +static int pattern_count_cmp(const void *elt, const void *key) +{ int cmp; pattern_entry_t **e1 = (pattern_entry_t **)elt; @@ -140,15 +141,15 @@ static int pattern_count_cmp(const void *elt, const void *key) { /** * Compare two pattern for its pattern hash. */ -static int pattern_cmp(const void *elt, const void *key) { - const pattern_entry_t *e1 = elt; - const pattern_entry_t *e2 = key; - int diff = e1->len - e2->len; +static int pattern_cmp(const void *elt, const void *key) +{ + const pattern_entry_t *e1 = (const pattern_entry_t*)elt; + const pattern_entry_t *e2 = (const pattern_entry_t*)key; - if (diff) - return diff; + if (e1->len == e2->len) + return memcmp(e1->buf, e2->buf, e1->len); - return memcmp(e1->buf, e2->buf, e1->len); + return e1->len < e2->len ? -1 : +1; } /* pattern_cmp */ /** @@ -158,7 +159,8 @@ static int pattern_cmp(const void *elt, const void *key) { * @param data a buffer address * @param len the length of the data buffer */ -static void init_buf(CODE_BUFFER *buf, BYTE *data, unsigned len) { +static void init_buf(CODE_BUFFER *buf, BYTE *data, size_t len) +{ buf->start = buf->next = data; buf->end = data + len; @@ -174,13 +176,14 @@ static void init_buf(CODE_BUFFER *buf, BYTE *data, unsigned len) { * * The hash value for the buffer content is updated. */ -static INLINE void put_byte(CODE_BUFFER *buf, BYTE byte) { +static inline void put_byte(CODE_BUFFER *buf, BYTE byte) +{ if (buf->next < buf->end) { *buf->next++ = byte; buf->hash = (buf->hash * 9) ^ byte; } else { buf->overrun = 1; - } /* if */ + } } /* put_byte */ /** @@ -190,7 +193,8 @@ static INLINE void put_byte(CODE_BUFFER *buf, BYTE byte) { * * @return the length of the buffer content */ -static unsigned buf_lenght(const CODE_BUFFER *buf) { +static size_t buf_lenght(const CODE_BUFFER *buf) +{ return buf->next - buf->start; } /* buf_lenght */ @@ -201,7 +205,8 @@ static unsigned buf_lenght(const CODE_BUFFER *buf) { * * @return the start address of the buffer content */ -static const BYTE *buf_content(const CODE_BUFFER *buf) { +static const BYTE *buf_content(const CODE_BUFFER *buf) +{ return buf->start; } /* buf_content */ @@ -212,7 +217,8 @@ static const BYTE *buf_content(const CODE_BUFFER *buf) { * * @return the hash value of the buffer content */ -static unsigned buf_hash(const CODE_BUFFER *buf) { +static unsigned buf_hash(const CODE_BUFFER *buf) +{ return buf->hash; } /* buf_hash */ @@ -221,7 +227,8 @@ static unsigned buf_hash(const CODE_BUFFER *buf) { * * @param buf the code buffer */ -static unsigned buf_overrun(const CODE_BUFFER *buf) { +static unsigned buf_overrun(const CODE_BUFFER *buf) +{ return buf->overrun; } /* buf_overrun */ @@ -232,7 +239,8 @@ static unsigned buf_overrun(const CODE_BUFFER *buf) { * * @return the next byte from the code buffer */ -static INLINE BYTE look_byte(CODE_BUFFER *buf) { +static inline BYTE look_byte(CODE_BUFFER *buf) +{ if (buf->next < buf->end) return *buf->next; return VLC_TAG_END; @@ -245,7 +253,8 @@ static INLINE BYTE look_byte(CODE_BUFFER *buf) { * * @return the next byte from the code buffer */ -static INLINE BYTE get_byte(CODE_BUFFER *buf) { +static inline BYTE get_byte(CODE_BUFFER *buf) +{ if (buf->next < buf->end) return *buf->next++; return VLC_TAG_END; @@ -259,7 +268,8 @@ static INLINE BYTE get_byte(CODE_BUFFER *buf) { * @param buf the code buffer * @param code the code to be written into the buffer */ -static void put_code(CODE_BUFFER *buf, unsigned code) { +static void put_code(CODE_BUFFER *buf, unsigned code) +{ if (code < BITS(7)) { put_byte(buf, VLC_7BIT | code); } else if (code < BITS(6 + 8)) { @@ -292,7 +302,8 @@ static void put_code(CODE_BUFFER *buf, unsigned code) { * * @return next 32bit value from the code buffer */ -static unsigned get_code(CODE_BUFFER *buf) { +static unsigned get_code(CODE_BUFFER *buf) +{ unsigned code = get_byte(buf); if (code < VLC_14BIT) @@ -318,9 +329,7 @@ static unsigned get_code(CODE_BUFFER *buf) { return code; } /* if */ /* should not happen */ - assert(0 && "Wrong code in buffer"); - - return 0; + panic("Wrong code in buffer"); } /* get_code */ /** @@ -329,7 +338,8 @@ static unsigned get_code(CODE_BUFFER *buf) { * @param buf the code buffer * @param tag the tag to write to the code buffer */ -static void put_tag(CODE_BUFFER *buf, BYTE tag) { +static void put_tag(CODE_BUFFER *buf, BYTE tag) +{ assert(tag >= VLC_TAG_FIRST && "invalid tag"); put_byte(buf, tag); @@ -342,7 +352,8 @@ static void put_tag(CODE_BUFFER *buf, BYTE tag) { * * @return the next tag in the code buffer */ -static BYTE next_tag(CODE_BUFFER *buf) { +static BYTE next_tag(CODE_BUFFER *buf) +{ BYTE b = look_byte(buf); if (b >= VLC_TAG_FIRST) @@ -353,7 +364,7 @@ static BYTE next_tag(CODE_BUFFER *buf) { /** * An Environment for the pattern encoder. */ -typedef struct _codec_enc_t { +typedef struct codec_enc_t { CODE_BUFFER *buf; /**< The current code buffer. */ set *id_set; /**< A set containing all already seen Firm nodes. */ unsigned curr_id; /**< The current node id. */ @@ -364,7 +375,7 @@ typedef struct _codec_enc_t { /** * An address entry. */ -typedef struct _addr_entry_t { +typedef struct addr_entry_t { void *addr; /**< the address */ unsigned id; /**< associated ID */ } addr_entry_t; @@ -372,32 +383,50 @@ typedef struct _addr_entry_t { /** * Compare two addresses. */ -static int addr_cmp(const void *p1, const void *p2, size_t size) { - const addr_entry_t *e1 = p1; - const addr_entry_t *e2 = p2; +static int addr_cmp(const void *p1, const void *p2, size_t size) +{ + const addr_entry_t *e1 = (const addr_entry_t*)p1; + const addr_entry_t *e2 = (const addr_entry_t*)p2; (void) size; return e1->addr != e2->addr; } /* addr_cmp */ +/** + * Return the index of a (existing) mode. + */ +static size_t find_mode_index(const ir_mode *mode) +{ + size_t i, n = ir_get_n_modes(); + + for (i = 0; i < n; ++i) { + if (ir_get_mode(i) == mode) + return i; + } + /* should really not happen */ + assert(!"Cound not find index of mode in find_mode_index()"); + return (size_t)-1; +} + /** * Encodes an IR-node, recursive worker. * * @return reached depth */ -static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) { +static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) +{ addr_entry_t entry, *r_entry; set_entry *s_entry; int i, preds; int res, depth; - ir_opcode code = get_irn_opcode(node); + unsigned code = get_irn_opcode(node); /* insert the node into our ID map */ entry.addr = node; entry.id = env->curr_id; - s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), HASH_PTR(node)); + s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), hash_ptr(node)); r_entry = (addr_entry_t *)s_entry->dptr; if (r_entry->id != env->curr_id) { @@ -418,8 +447,7 @@ static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) { ir_mode *mode = get_irn_mode(node); if (mode) - /* FIXME: not 64bit save */ - put_code(env->buf, (unsigned)mode); + put_code(env->buf, find_mode_index(mode)); else put_tag(env->buf, VLC_TAG_EMPTY); } /* if */ @@ -427,7 +455,7 @@ static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) { /* do we need integer constants */ if (env->options & OPT_WITH_ICONST) { if (code == iro_Const) { - tarval *tv = get_Const_tarval(node); + ir_tarval *tv = get_Const_tarval(node); if (tarval_is_long(tv)) { long v = get_tarval_long(tv); @@ -491,7 +519,8 @@ static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) { * * @return The depth of the encoded graph (without cycles) */ -static int encode_node(ir_node *node, CODE_BUFFER *buf, int max_depth) { +static int encode_node(ir_node *node, CODE_BUFFER *buf, int max_depth) +{ codec_env_t env; int res; @@ -523,7 +552,8 @@ static int encode_node(ir_node *node, CODE_BUFFER *buf, int max_depth) { /** * Decode an IR-node, recursive walker. */ -static void _decode_node(unsigned parent, int position, codec_env_t *env) { +static void _decode_node(unsigned parent, int position, codec_env_t *env) +{ unsigned code; unsigned op_code; unsigned mode_code = 0; @@ -605,7 +635,8 @@ static void _decode_node(unsigned parent, int position, codec_env_t *env) { /** * Decode an IR-node. */ -static void decode_node(BYTE *b, unsigned len, pattern_dumper_t *dump) { +static void decode_node(BYTE *b, size_t len, pattern_dumper_t *dump) +{ codec_env_t env; CODE_BUFFER buf; unsigned code, options = 0; @@ -629,7 +660,7 @@ static void decode_node(BYTE *b, unsigned len, pattern_dumper_t *dump) { /** * The environment for the pattern calculation. */ -typedef struct _pattern_env { +typedef struct pattern_env { int max_depth; /**< maximum depth for pattern generation. */ } pattern_env_t; @@ -644,27 +675,26 @@ typedef struct _pattern_env { * If the code content was never seen before, a new pattern_entry is created * and returned. */ -static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set) { +static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set) +{ pattern_entry_t *key, *elem; - unsigned len = buf_lenght(buf); + size_t len = buf_lenght(buf); unsigned hash; - key = obstack_alloc(&status->obst, offsetof(pattern_entry_t, buf) + len); - assert(key); - + key = OALLOCF(&status->obst, pattern_entry_t, buf, len); key->len = len; memcpy(key->buf, buf_content(buf), len); hash = buf_hash(buf); - elem = pset_find(set, key, hash); + elem = (pattern_entry_t*)pset_find(set, key, hash); if (elem != NULL) { obstack_free(&status->obst, key); return elem; } /* if */ cnt_clr(&key->count); - return pset_insert(set, key, hash); + return (pattern_entry_t*)pset_insert(set, key, hash); } /* pattern_get_entry */ /** @@ -675,7 +705,8 @@ static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set) { * * @note Single node patterns are ignored */ -static void count_pattern(CODE_BUFFER *buf, int depth) { +static void count_pattern(CODE_BUFFER *buf, int depth) +{ pattern_entry_t *entry; /* ignore single node pattern (i.e. constants) */ @@ -690,8 +721,9 @@ static void count_pattern(CODE_BUFFER *buf, int depth) { /** * Pre-walker for nodes pattern calculation. */ -static void calc_nodes_pattern(ir_node *node, void *ctx) { - pattern_env_t *env = ctx; +static void calc_nodes_pattern(ir_node *node, void *ctx) +{ + pattern_env_t *env = (pattern_env_t*)ctx; BYTE buffer[PATTERN_STORE_SIZE]; CODE_BUFFER buf; int depth; @@ -700,7 +732,7 @@ static void calc_nodes_pattern(ir_node *node, void *ctx) { depth = encode_node(node, &buf, env->max_depth); if (buf_overrun(&buf)) { - fprintf(stderr, "Pattern store: buffer overrun at size %d. Pattern ignored.\n", sizeof(buffer)); + lc_fprintf(stderr, "Pattern store: buffer overrun at size %zu. Pattern ignored.\n", sizeof(buffer)); } else count_pattern(&buf, depth); } /* calc_nodes_pattern */ @@ -710,10 +742,11 @@ static void calc_nodes_pattern(ir_node *node, void *ctx) { * * @param fname filename for storage */ -static void store_pattern(const char *fname) { +static void store_pattern(const char *fname) +{ FILE *f; pattern_entry_t *entry; - int i, count = pset_count(status->pattern_hash); + size_t count = pset_count(status->pattern_hash); if (count <= 0) return; @@ -727,9 +760,7 @@ static void store_pattern(const char *fname) { fwrite("FPS1", 4, 1, f); fwrite(&count, sizeof(count), 1, f); - for (i = 0, entry = pset_first(status->pattern_hash); - entry && i < count; - entry = pset_next(status->pattern_hash), ++i) { + foreach_pset(status->pattern_hash, pattern_entry_t*, entry) { fwrite(entry, offsetof(pattern_entry_t, buf) + entry->len, 1, f); } /* for */ fclose(f); @@ -740,15 +771,17 @@ static void store_pattern(const char *fname) { * * @param fname filename */ -static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) { +static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) +{ FILE *f; pattern_entry_t *entry, tmp; - int i, count; + size_t i, count; unsigned j; char magic[4]; HASH_MAP(pattern_entry_t) *pattern_hash = new_pset(pattern_cmp, 8); BYTE buffer[PATTERN_STORE_SIZE]; CODE_BUFFER buf; + int res; f = fopen(fname, "rb"); if (! f) { @@ -756,30 +789,36 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) { return NULL; } /* if */ - fread(magic, 4, 1, f); + res = fread(magic, 4, 1, f); + if (res != 1) + goto read_error; count = 0; - fread(&count, sizeof(count), 1, f); - if (memcmp(magic, "FPS1", 4) != 0 || count <= 0) { - fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname); - fclose(f); - return NULL; - } /* if */ + res = fread(&count, sizeof(count), 1, f); + if (res != 1 || memcmp(magic, "FPS1", 4) != 0 || count <= 0) + goto read_error; /* read all pattern entries and put them into the hash table. */ for (i = 0; i < count; ++i) { init_buf(&buf, buffer, sizeof(buffer)); - fread(&tmp, offsetof(pattern_entry_t, buf), 1, f); + res = fread(&tmp, offsetof(pattern_entry_t, buf), 1, f); + if (res != 1) + goto read_error; for (j = 0; j < tmp.len; ++j) put_byte(&buf, fgetc(f)); entry = pattern_get_entry(&buf, pattern_hash); - memcpy(&entry->count, &tmp.count, sizeof(entry->count)); + entry->count = tmp.count; } /* for */ fclose(f); - printf("Read %d pattern from %s\n", count, fname); + lc_printf("Read %zu pattern from %s\n", count, fname); assert(pset_count(pattern_hash) == count); return pattern_hash; + +read_error: + fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname); + fclose(f); + return NULL; } /* read_pattern */ /** @@ -787,25 +826,25 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) { * * @param fname name of the VCG file to create */ -static void pattern_output(const char *fname) { +static void pattern_output(const char *fname) +{ pattern_entry_t *entry; pattern_entry_t **pattern_arr; pattern_dumper_t *dump; - int i, count = pset_count(status->pattern_hash); + size_t i, count = pset_count(status->pattern_hash); - printf("\n%d pattern detected\n", count); + lc_printf("\n%zu pattern detected\n", count); - if (count <= 0) + if (count == 0) return; /* creates a dumper */ dump = new_vcg_dumper(fname, 100); - pattern_arr = xmalloc(sizeof(*pattern_arr) * count); - for (i = 0, entry = pset_first(status->pattern_hash); - entry && i < count; - entry = pset_next(status->pattern_hash), ++i) { - pattern_arr[i] = entry; + pattern_arr = XMALLOCN(pattern_entry_t*, count); + i = 0; + foreach_pset(status->pattern_hash, pattern_entry_t*, entry) { + pattern_arr[i++] = entry; } /* for */ assert(count == i); count = i; @@ -831,7 +870,8 @@ static void pattern_output(const char *fname) { /* * Calculates the pattern history. */ -void stat_calc_pattern_history(ir_graph *irg) { +void stat_calc_pattern_history(ir_graph *irg) +{ pattern_env_t env; unsigned i; @@ -851,7 +891,8 @@ void stat_calc_pattern_history(ir_graph *irg) { /* * Initializes the pattern history. */ -void stat_init_pattern_history(int enable) { +void stat_init_pattern_history(int enable) +{ HASH_MAP(pattern_entry_t) *pattern_hash = NULL; status->enable = enable; @@ -876,7 +917,8 @@ void stat_init_pattern_history(int enable) { /* * Finish the pattern history. */ -void stat_finish_pattern_history(const char *fname) { +void stat_finish_pattern_history(const char *fname) +{ (void) fname; if (! status->enable) return;