Add -c to ar to remove a diagnostic, when the archive is created.
[libfirm] / ir / stat / pattern.c
index 564213d..a112561 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"
 
@@ -39,8 +38,8 @@
 #include "counter.h"
 #include "pattern_dmp.h"
 #include "hashptr.h"
-
-#ifdef FIRM_STATISTICS
+#include "error.h"
+#include "lc_printf.h"
 
 /*
  * just be make some things clear :-), the
@@ -53,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. */
@@ -88,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;
 
@@ -108,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. */
@@ -144,14 +143,13 @@ static int pattern_count_cmp(const void *elt, const void *key)
  */
 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;
+       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 */
 
 /**
@@ -161,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;
@@ -195,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 */
@@ -331,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 */
 
 /**
@@ -368,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. */
@@ -379,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;
@@ -389,13 +385,29 @@ typedef struct _addr_entry_t {
  */
 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;
+       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 = get_irp_n_modes();
+
+       for (i = 0; i < n; ++i) {
+               if (get_irp_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.
  *
@@ -408,7 +420,7 @@ static int _encode_node(ir_node *node, int max_depth, codec_env_t *env)
        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;
@@ -435,7 +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)
-                       put_code(env->buf, stat_find_mode_index(mode));
+                       put_code(env->buf, find_mode_index(mode));
                else
                        put_tag(env->buf, VLC_TAG_EMPTY);
        }  /* if */
@@ -443,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);
@@ -623,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;
@@ -648,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;
 
@@ -666,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);
@@ -675,14 +687,14 @@ static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set)
 
        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 */
 
 /**
@@ -711,7 +723,7 @@ static void count_pattern(CODE_BUFFER *buf, int depth)
  */
 static void calc_nodes_pattern(ir_node *node, void *ctx)
 {
-       pattern_env_t   *env = ctx;
+       pattern_env_t   *env = (pattern_env_t*)ctx;
        BYTE            buffer[PATTERN_STORE_SIZE];
        CODE_BUFFER     buf;
        int             depth;
@@ -720,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 */
@@ -734,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 i, count = pset_count(status->pattern_hash);
 
        if (count <= 0)
                return;
@@ -748,9 +760,9 @@ 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);
+       for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash);
             entry && i < count;
-            entry = pset_next(status->pattern_hash), ++i) {
+            entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) {
                fwrite(entry, offsetof(pattern_entry_t, buf) + entry->len, 1, f);
        }  /* for */
        fclose(f);
@@ -765,7 +777,7 @@ 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);
@@ -794,11 +806,11 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname)
                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;
@@ -814,20 +826,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 = pset_first(status->pattern_hash);
+       for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash);
             entry && i < count;
-            entry = pset_next(status->pattern_hash), ++i) {
+            entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) {
                pattern_arr[i] =  entry;
        }  /* for */
        assert(count == i);
@@ -915,5 +927,3 @@ void stat_finish_pattern_history(const char *fname)
 
        status->enable = 0;
 }  /* stat_finish_pattern_history */
-
-#endif /* FIRM_STATISTICS */