2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm. Pattern history.
23 * @author Michael Beck
39 #include "pattern_dmp.h"
42 #ifdef FIRM_STATISTICS
45 * just be make some things clear :-), the
48 #define HASH_MAP(type) pset_##type
50 typedef pset pset_pattern_entry_t;
52 typedef unsigned char BYTE;
54 /** Maximum size of the pattern store. */
55 #define PATTERN_STORE_SIZE 2048
61 typedef struct _code_buf_t {
62 BYTE *next; /**< Next byte address to be written. */
63 BYTE *end; /**< End address of the buffer. */
64 BYTE *start; /**< Start address of the buffer. */
65 unsigned hash; /**< The hash value for the buffer content. */
66 unsigned overrun; /**< flag set if the buffer was overrun */
73 VLC_7BIT = 0x00, /**< 8 bit code, carrying 7 bits payload */
74 VLC_14BIT = 0x80, /**< 16 bit code, carrying 14 bits payload */
75 VLC_21BIT = 0xC0, /**< 24 bit code, carrying 21 bits payload */
76 VLC_28BIT = 0xE0, /**< 32 bit code, carrying 28 bits payload */
77 VLC_32BIT = 0xF0, /**< 40 bit code, carrying 32 bits payload */
79 VLC_TAG_FIRST = 0xF1, /**< First possible tag value. */
80 VLC_TAG_ICONST = 0xFB, /**< Encodes an integer constant. */
81 VLC_TAG_EMPTY = 0xFC, /**< Encodes an empty entity. */
82 VLC_TAG_OPTION = 0xFD, /**< Options exists. */
83 VLC_TAG_REF = 0xFE, /**< Special tag, next code is an ID. */
84 VLC_TAG_END = 0xFF, /**< End tag. */
88 * An entry for holding one pattern.
90 typedef struct _pattern_entry_t {
91 counter_t count; /**< Amount of pattern occurance. */
92 unsigned len; /**< The length of the VLC encoded buffer. */
93 BYTE buf[1]; /**< The buffer containing the VLC encoded pattern. */
97 * Current options for the pattern matcher.
100 OPT_WITH_MODE = 0x00000001, /**< use modes */
101 OPT_ENC_DAG = 0x00000002, /**< encode DAGs, not terms */
102 OPT_WITH_ICONST = 0x00000004, /**< encode integer constants */
103 OPT_PERSIST_PATTERN = 0x00000008, /**< persistent pattern hash */
110 typedef struct _pattern_info_t {
111 int enable; /**< If non-zero, this module is enabled. */
112 struct obstack obst; /**< An obstack containing the counters. */
113 HASH_MAP(pattern_entry_t) *pattern_hash; /**< A hash map containing the pattern. */
114 unsigned bound; /**< Lowest value for pattern output. */
115 unsigned options; /**< Current option mask. */
116 unsigned min_depth; /**< Minimum pattern depth. */
117 unsigned max_depth; /**< Maximum pattern depth. */
123 static pattern_info_t _status, *status = &_status;
126 * Compare two pattern for its occurance counter.
128 static int pattern_count_cmp(const void *elt, const void *key) {
131 pattern_entry_t **e1 = (pattern_entry_t **)elt;
132 pattern_entry_t **e2 = (pattern_entry_t **)key;
134 /* we want it sorted in descending order */
135 cmp = cnt_cmp(&(*e2)->count, &(*e1)->count);
138 } /* pattern_count_cmp */
141 * Compare two pattern for its pattern hash.
143 static int pattern_cmp(const void *elt, const void *key) {
144 const pattern_entry_t *e1 = elt;
145 const pattern_entry_t *e2 = key;
146 int diff = e1->len - e2->len;
151 return memcmp(e1->buf, e2->buf, e1->len);
155 * Initialize a code buffer.
157 * @param buf the code buffer
158 * @param data a buffer address
159 * @param len the length of the data buffer
161 static void init_buf(CODE_BUFFER *buf, BYTE *data, unsigned len) {
164 buf->end = data + len;
165 buf->hash = 0x2BAD4; /* An arbitrary seed. */
170 * Put a byte into the buffer.
172 * @param buf the code buffer
173 * @param byte the byte to write
175 * The hash value for the buffer content is updated.
177 static inline void put_byte(CODE_BUFFER *buf, BYTE byte) {
178 if (buf->next < buf->end) {
180 buf->hash = (buf->hash * 9) ^ byte;
187 * Returns the current length of a buffer.
189 * @param buf the code buffer
191 * @return the length of the buffer content
193 static unsigned buf_lenght(const CODE_BUFFER *buf) {
194 return buf->next - buf->start;
198 * Returns the current content of a buffer.
200 * @param buf the code buffer
202 * @return the start address of the buffer content
204 static const BYTE *buf_content(const CODE_BUFFER *buf) {
209 * Returns the hash value of a buffer.
211 * @param buf the code buffer
213 * @return the hash value of the buffer content
215 static unsigned buf_hash(const CODE_BUFFER *buf) {
220 * Returns non-zero if a buffer overrun has occurred.
222 * @param buf the code buffer
224 static unsigned buf_overrun(const CODE_BUFFER *buf) {
229 * Returns the next byte from the buffer WITHOUT dropping.
231 * @param buf the code buffer
233 * @return the next byte from the code buffer
235 static inline BYTE look_byte(CODE_BUFFER *buf) {
236 if (buf->next < buf->end)
242 * Returns the next byte from the buffer WITH dropping.
244 * @param buf the code buffer
246 * @return the next byte from the code buffer
248 static inline BYTE get_byte(CODE_BUFFER *buf) {
249 if (buf->next < buf->end)
254 #define BITS(n) (1 << (n))
257 * Put a 32bit value into the buffer.
259 * @param buf the code buffer
260 * @param code the code to be written into the buffer
262 static void put_code(CODE_BUFFER *buf, unsigned code) {
263 if (code < BITS(7)) {
264 put_byte(buf, VLC_7BIT | code);
265 } else if (code < BITS(6 + 8)) {
266 put_byte(buf, VLC_14BIT | (code >> 8));
268 } else if (code < BITS(5 + 8 + 8)) {
269 put_byte(buf, VLC_21BIT | (code >> 16));
270 put_byte(buf, code >> 8);
272 } else if (code < BITS(4 + 8 + 8 + 8)) {
273 put_byte(buf, VLC_28BIT | (code >> 24));
274 put_byte(buf, code >> 16);
275 put_byte(buf, code >> 8);
278 put_byte(buf, VLC_32BIT);
279 put_byte(buf, code >> 24);
280 put_byte(buf, code >> 16);
281 put_byte(buf, code >> 8);
286 #define BIT_MASK(n) ((1 << (n)) - 1)
289 * Get 32 bit from the buffer.
291 * @param buf the code buffer
293 * @return next 32bit value from the code buffer
295 static unsigned get_code(CODE_BUFFER *buf) {
296 unsigned code = get_byte(buf);
298 if (code < VLC_14BIT)
300 if (code < VLC_21BIT)
301 return ((code & BIT_MASK(6)) << 8) | get_byte(buf);
302 if (code < VLC_28BIT) {
303 code = ((code & BIT_MASK(5)) << 16) | (get_byte(buf) << 8);
304 code |= get_byte(buf);
307 if (code < VLC_32BIT) {
308 code = ((code & BIT_MASK(4)) << 24) | (get_byte(buf) << 16);
309 code |= get_byte(buf) << 8;
310 code |= get_byte(buf);
313 if (code == VLC_32BIT) {
314 code = get_byte(buf) << 24;
315 code |= get_byte(buf) << 16;
316 code |= get_byte(buf) << 8;
317 code |= get_byte(buf);
320 /* should not happen */
321 assert(0 && "Wrong code in buffer");
327 * Put a tag into the buffer.
329 * @param buf the code buffer
330 * @param tag the tag to write to the code buffer
332 static void put_tag(CODE_BUFFER *buf, BYTE tag) {
333 assert(tag >= VLC_TAG_FIRST && "invalid tag");
339 * Returns the next tag or zero if the next code isn't a tag.
341 * @param buf the code buffer
343 * @return the next tag in the code buffer
345 static BYTE next_tag(CODE_BUFFER *buf) {
346 BYTE b = look_byte(buf);
348 if (b >= VLC_TAG_FIRST)
349 return get_byte(buf);
354 * An Environment for the pattern encoder.
356 typedef struct _codec_enc_t {
357 CODE_BUFFER *buf; /**< The current code buffer. */
358 set *id_set; /**< A set containing all already seen Firm nodes. */
359 unsigned curr_id; /**< The current node id. */
360 unsigned options; /**< The encoding options. */
361 pattern_dumper_t *dmp; /**< The dumper for the decoder. */
367 typedef struct _addr_entry_t {
368 void *addr; /**< the address */
369 unsigned id; /**< associated ID */
373 * Compare two addresses.
375 static int addr_cmp(const void *p1, const void *p2, size_t size) {
376 const addr_entry_t *e1 = p1;
377 const addr_entry_t *e2 = p2;
380 return e1->addr != e2->addr;
384 * Encodes an IR-node, recursive worker.
386 * @return reached depth
388 static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) {
389 addr_entry_t entry, *r_entry;
394 ir_opcode code = get_irn_opcode(node);
396 /* insert the node into our ID map */
398 entry.id = env->curr_id;
400 s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), HASH_PTR(node));
401 r_entry = (addr_entry_t *)s_entry->dptr;
403 if (r_entry->id != env->curr_id) {
404 /* already in the map, add an REF */
405 put_tag(env->buf, VLC_TAG_REF);
406 put_code(env->buf, r_entry->id);
410 /* a new entry, proceed */
414 put_code(env->buf, (unsigned)code);
416 /* do we need the mode ? */
417 if (env->options & OPT_WITH_MODE) {
418 ir_mode *mode = get_irn_mode(node);
421 put_code(env->buf, stat_find_mode_index(mode));
423 put_tag(env->buf, VLC_TAG_EMPTY);
426 /* do we need integer constants */
427 if (env->options & OPT_WITH_ICONST) {
428 if (code == iro_Const) {
429 tarval *tv = get_Const_tarval(node);
431 if (tarval_is_long(tv)) {
432 long v = get_tarval_long(tv);
434 put_tag(env->buf, VLC_TAG_ICONST);
435 put_code(env->buf, v);
442 if (max_depth <= 0) {
443 put_code(env->buf, 0);
447 preds = get_irn_arity(node);
448 put_code(env->buf, preds);
451 if (is_op_commutative(get_irn_op(node))) {
452 ir_node *l = get_binop_left(node);
453 ir_node *r = get_binop_right(node);
454 int opcode_diff = (int)get_irn_opcode(l) - (int)get_irn_opcode(r);
456 if (opcode_diff > 0) {
460 } else if (opcode_diff == 0 && l != r) {
461 /* Both nodes have the same opcode, but are different.
462 Need a better method here to decide which goes to the left side. */
465 /* special handling for commutative operators */
466 depth = _encode_node(l, max_depth, env);
469 depth = _encode_node(r, max_depth, env);
473 for (i = 0; i < preds; ++i) {
474 ir_node *n = get_irn_n(node, i);
476 depth = _encode_node(n, max_depth, env);
485 * Encode a DAG starting by the IR-node node.
487 * @param node The root node of the graph
488 * @param buf The code buffer to store the bitstring in
489 * @param max_depth The maximum depth for descending
491 * @return The depth of the encoded graph (without cycles)
493 static int encode_node(ir_node *node, CODE_BUFFER *buf, int max_depth) {
497 /* initialize the encoder environment */
499 env.curr_id = 1; /* 0 is used for special purpose */
500 env.options = status->options;
503 if (env.options & OPT_ENC_DAG)
504 env.id_set = new_set(addr_cmp, 32);
508 /* encode options if any for the decoder */
510 put_tag(buf, VLC_TAG_OPTION);
511 put_code(buf, env.options);
514 res = _encode_node(node, max_depth, &env);
516 if (env.id_set != NULL)
519 return max_depth - res;
523 * Decode an IR-node, recursive walker.
525 static void _decode_node(unsigned parent, int position, codec_env_t *env) {
528 unsigned mode_code = 0;
532 code = next_tag(env->buf);
533 if (code == VLC_TAG_REF) { /* it's a REF */
534 code = get_code(env->buf);
540 * the mode of a Firm edge can be either computed from its target or
541 * from its source and position. We must take the second approach because
542 * we don't know the target here, it's a ref.
544 pattern_dump_edge(env->dmp, code, parent, position, edge_mode);
547 /* dump the node ref */
548 pattern_dump_ref(env->dmp, code);
554 op_code = get_code(env->buf);
556 /* get the mode if encoded */
557 if (env->options & OPT_WITH_MODE) {
558 if (next_tag(env->buf) != VLC_TAG_EMPTY) {
559 mode_code = get_code(env->buf);
563 /* check, if a ICONST attribute is given */
564 if (next_tag(env->buf) == VLC_TAG_ICONST) {
565 iconst = get_code(env->buf);
574 * the mode of a Firm edge can be either computed from its target or
575 * from its source and position. We take the second approach because
576 * we need it anyway for ref's.
578 pattern_dump_edge(env->dmp, env->curr_id, parent, position, edge_mode);
582 parent = env->curr_id;
583 pattern_dump_node(env->dmp, parent, op_code, mode_code, attr);
585 /* ok, we have a new ID */
588 code = next_tag(env->buf);
589 if (code != VLC_TAG_END) {
590 /* more info, do recursion */
593 preds = get_code(env->buf);
595 pattern_start_children(env->dmp, parent);
596 for (i = 0; i < preds; ++i) {
597 _decode_node(parent, i, env);
599 pattern_finish_children(env->dmp, parent);
607 static void decode_node(BYTE *b, unsigned len, pattern_dumper_t *dump) {
610 unsigned code, options = 0;
612 init_buf(&buf, b, len);
615 env.curr_id = 1; /* 0 is used for special purpose */
619 code = next_tag(&buf);
620 if (code == VLC_TAG_OPTION) {
621 options = get_code(&buf);
623 env.options = options;
625 _decode_node(0, 0, &env);
629 * The environment for the pattern calculation.
631 typedef struct _pattern_env {
632 int max_depth; /**< maximum depth for pattern generation. */
636 * Returns the associates pattern_entry_t for a CODE_BUF.
638 * @param buf the code buffer
639 * @param set the hash table containing all pattern entries
641 * @return the associated pattern_entry_t for the given code buffer
643 * If the code content was never seen before, a new pattern_entry is created
646 static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set) {
647 pattern_entry_t *key, *elem;
648 unsigned len = buf_lenght(buf);
651 key = OALLOCF(&status->obst, pattern_entry_t, buf, len);
653 memcpy(key->buf, buf_content(buf), len);
655 hash = buf_hash(buf);
657 elem = pset_find(set, key, hash);
659 obstack_free(&status->obst, key);
663 cnt_clr(&key->count);
664 return pset_insert(set, key, hash);
665 } /* pattern_get_entry */
668 * Increase the count for a pattern.
670 * @param buf the code buffer containing the pattern
671 * @param depth the pattern depth
673 * @note Single node patterns are ignored
675 static void count_pattern(CODE_BUFFER *buf, int depth) {
676 pattern_entry_t *entry;
678 /* ignore single node pattern (i.e. constants) */
680 entry = pattern_get_entry(buf, status->pattern_hash);
683 cnt_inc(&entry->count);
685 } /* count_pattern */
688 * Pre-walker for nodes pattern calculation.
690 static void calc_nodes_pattern(ir_node *node, void *ctx) {
691 pattern_env_t *env = ctx;
692 BYTE buffer[PATTERN_STORE_SIZE];
696 init_buf(&buf, buffer, sizeof(buffer));
697 depth = encode_node(node, &buf, env->max_depth);
699 if (buf_overrun(&buf)) {
700 fprintf(stderr, "Pattern store: buffer overrun at size %u. Pattern ignored.\n", (unsigned) sizeof(buffer));
702 count_pattern(&buf, depth);
703 } /* calc_nodes_pattern */
706 * Store all collected patterns.
708 * @param fname filename for storage
710 static void store_pattern(const char *fname) {
712 pattern_entry_t *entry;
713 int i, count = pset_count(status->pattern_hash);
718 f = fopen(fname, "wb");
724 fwrite("FPS1", 4, 1, f);
725 fwrite(&count, sizeof(count), 1, f);
727 for (i = 0, entry = pset_first(status->pattern_hash);
729 entry = pset_next(status->pattern_hash), ++i) {
730 fwrite(entry, offsetof(pattern_entry_t, buf) + entry->len, 1, f);
733 } /* store_pattern */
736 * Read collected patterns from a file.
738 * @param fname filename
740 static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) {
742 pattern_entry_t *entry, tmp;
746 HASH_MAP(pattern_entry_t) *pattern_hash = new_pset(pattern_cmp, 8);
747 BYTE buffer[PATTERN_STORE_SIZE];
750 f = fopen(fname, "rb");
756 fread(magic, 4, 1, f);
758 fread(&count, sizeof(count), 1, f);
759 if (memcmp(magic, "FPS1", 4) != 0 || count <= 0) {
760 fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname);
765 /* read all pattern entries and put them into the hash table. */
766 for (i = 0; i < count; ++i) {
767 init_buf(&buf, buffer, sizeof(buffer));
768 fread(&tmp, offsetof(pattern_entry_t, buf), 1, f);
769 for (j = 0; j < tmp.len; ++j)
770 put_byte(&buf, fgetc(f));
771 entry = pattern_get_entry(&buf, pattern_hash);
772 memcpy(&entry->count, &tmp.count, sizeof(entry->count));
776 printf("Read %d pattern from %s\n", count, fname);
777 assert(pset_count(pattern_hash) == count);
783 * Write the collected patterns to a VCG file for inspection.
785 * @param fname name of the VCG file to create
787 static void pattern_output(const char *fname) {
788 pattern_entry_t *entry;
789 pattern_entry_t **pattern_arr;
790 pattern_dumper_t *dump;
791 int i, count = pset_count(status->pattern_hash);
793 printf("\n%d pattern detected\n", count);
798 /* creates a dumper */
799 dump = new_vcg_dumper(fname, 100);
801 pattern_arr = XMALLOCN(pattern_entry_t*, count);
802 for (i = 0, entry = pset_first(status->pattern_hash);
804 entry = pset_next(status->pattern_hash), ++i) {
805 pattern_arr[i] = entry;
811 qsort(pattern_arr, count, sizeof(*pattern_arr), pattern_count_cmp);
813 for (i = 0; i < count; ++i) {
814 entry = pattern_arr[i];
815 if (cnt_to_uint(&entry->count) < status->bound)
819 pattern_dump_new_pattern(dump, &entry->count);
820 decode_node(entry->buf, entry->len, dump);
821 pattern_dump_finish_pattern(dump);
826 } /* pattern_output */
829 * Calculates the pattern history.
831 void stat_calc_pattern_history(ir_graph *irg) {
835 if (! status->enable)
838 /* do NOT count the const code IRG */
839 if (irg == get_const_code_irg())
842 for (i = status->min_depth; i <= status->max_depth; ++i) {
844 irg_walk_graph(irg, calc_nodes_pattern, NULL, &env);
846 } /* stat_calc_pattern_history */
849 * Initializes the pattern history.
851 void stat_init_pattern_history(int enable) {
852 HASH_MAP(pattern_entry_t) *pattern_hash = NULL;
854 status->enable = enable;
859 status->options = /* OPT_WITH_MODE | */ OPT_ENC_DAG | OPT_WITH_ICONST | OPT_PERSIST_PATTERN;
860 status->min_depth = 3;
861 status->max_depth = 5;
863 obstack_init(&status->obst);
865 /* create the hash-table */
866 if (status->options & OPT_PERSIST_PATTERN)
867 pattern_hash = read_pattern("pattern.fps");
868 if (pattern_hash == NULL)
869 pattern_hash = new_pset(pattern_cmp, 8);
870 status->pattern_hash = pattern_hash;
871 } /* stat_init_pattern_history */
874 * Finish the pattern history.
876 void stat_finish_pattern_history(const char *fname) {
878 if (! status->enable)
881 store_pattern("pattern.fps");
882 pattern_output("pattern.vcg");
884 del_pset(status->pattern_hash);
885 obstack_free(&status->obst, NULL);
888 } /* stat_finish_pattern_history */
890 #endif /* FIRM_STATISTICS */