Use foreach_pset().
[libfirm] / ir / stat / pattern.c
index fa8fcde..9f0a011 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -21,7 +21,6 @@
  * @file
  * @brief   Statistics for Firm. Pattern history.
  * @author  Michael Beck
- * @version $Id$
  */
 #include "config.h"
 
@@ -40,8 +39,7 @@
 #include "pattern_dmp.h"
 #include "hashptr.h"
 #include "error.h"
-
-#ifdef FIRM_STATISTICS
+#include "lc_printf.h"
 
 /*
  * just be make some things clear :-), the
@@ -91,7 +89,7 @@ enum vlc_code_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;
 
@@ -147,12 +145,11 @@ 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;
-       int diff = e1->len - e2->len;
 
-       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 */
 
 /**
@@ -162,7 +159,7 @@ 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;
@@ -186,7 +183,7 @@ static inline void put_byte(CODE_BUFFER *buf, BYTE byte)
                buf->hash = (buf->hash * 9) ^ byte;
        } else {
                buf->overrun = 1;
-       }  /* if */
+       }
 }  /* put_byte */
 
 /**
@@ -196,7 +193,7 @@ 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 */
@@ -395,16 +392,20 @@ static int addr_cmp(const void *p1, const void *p2, size_t size)
        return e1->addr != e2->addr;
 }  /* addr_cmp */
 
-static unsigned find_mode_index(const ir_mode *mode)
+/**
+ * Return the index of a (existing) mode.
+ */
+static size_t find_mode_index(const ir_mode *mode)
 {
-       int n = get_irp_n_modes();
-       int i;
+       size_t i, n = ir_get_n_modes();
 
        for (i = 0; i < n; ++i) {
-               if (get_irp_mode(i) == mode)
+               if (ir_get_mode(i) == mode)
                        return i;
        }
-       return -1;
+       /* should really not happen */
+       assert(!"Cound not find index of mode in find_mode_index()");
+       return (size_t)-1;
 }
 
 /**
@@ -425,7 +426,7 @@ static int _encode_node(ir_node *node, int max_depth, codec_env_t *env)
        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) {
@@ -634,7 +635,7 @@ 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;
@@ -677,7 +678,7 @@ typedef struct pattern_env {
 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 = OALLOCF(&status->obst, pattern_entry_t, buf, len);
@@ -731,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 %u. Pattern ignored.\n", (unsigned) 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 */
@@ -745,7 +746,7 @@ 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;
@@ -759,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 = (pattern_entry_t*)pset_first(status->pattern_hash);
-            entry && i < count;
-            entry = (pattern_entry_t*)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);
@@ -776,12 +775,13 @@ 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) {
@@ -789,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 */
 
 /**
@@ -825,21 +831,20 @@ 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 = XMALLOCN(pattern_entry_t*, count);
-       for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash);
-            entry && i < count;
-            entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) {
-               pattern_arr[i] =  entry;
+       i           = 0;
+       foreach_pset(status->pattern_hash, pattern_entry_t*, entry) {
+               pattern_arr[i++] =  entry;
        }  /* for */
        assert(count == i);
        count = i;
@@ -926,5 +931,3 @@ void stat_finish_pattern_history(const char *fname)
 
        status->enable = 0;
 }  /* stat_finish_pattern_history */
-
-#endif /* FIRM_STATISTICS */