2 * Copyright (C) 1995-2011 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
40 #include "pattern_dmp.h"
43 #include "lc_printf.h"
45 #ifdef FIRM_STATISTICS
48 * just be make some things clear :-), the
51 #define HASH_MAP(type) pset_##type
53 typedef pset pset_pattern_entry_t;
55 typedef unsigned char BYTE;
57 /** Maximum size of the pattern store. */
58 #define PATTERN_STORE_SIZE 2048
64 typedef struct code_buf_t {
65 BYTE *next; /**< Next byte address to be written. */
66 BYTE *end; /**< End address of the buffer. */
67 BYTE *start; /**< Start address of the buffer. */
68 unsigned hash; /**< The hash value for the buffer content. */
69 unsigned overrun; /**< flag set if the buffer was overrun */
76 VLC_7BIT = 0x00, /**< 8 bit code, carrying 7 bits payload */
77 VLC_14BIT = 0x80, /**< 16 bit code, carrying 14 bits payload */
78 VLC_21BIT = 0xC0, /**< 24 bit code, carrying 21 bits payload */
79 VLC_28BIT = 0xE0, /**< 32 bit code, carrying 28 bits payload */
80 VLC_32BIT = 0xF0, /**< 40 bit code, carrying 32 bits payload */
82 VLC_TAG_FIRST = 0xF1, /**< First possible tag value. */
83 VLC_TAG_ICONST = 0xFB, /**< Encodes an integer constant. */
84 VLC_TAG_EMPTY = 0xFC, /**< Encodes an empty entity. */
85 VLC_TAG_OPTION = 0xFD, /**< Options exists. */
86 VLC_TAG_REF = 0xFE, /**< Special tag, next code is an ID. */
87 VLC_TAG_END = 0xFF, /**< End tag. */
91 * An entry for holding one pattern.
93 typedef struct pattern_entry_t {
94 counter_t count; /**< Amount of pattern occurance. */
95 size_t len; /**< The length of the VLC encoded buffer. */
96 BYTE buf[1]; /**< The buffer containing the VLC encoded pattern. */
100 * Current options for the pattern matcher.
103 OPT_WITH_MODE = 0x00000001, /**< use modes */
104 OPT_ENC_DAG = 0x00000002, /**< encode DAGs, not terms */
105 OPT_WITH_ICONST = 0x00000004, /**< encode integer constants */
106 OPT_PERSIST_PATTERN = 0x00000008, /**< persistent pattern hash */
113 typedef struct pattern_info_t {
114 int enable; /**< If non-zero, this module is enabled. */
115 struct obstack obst; /**< An obstack containing the counters. */
116 HASH_MAP(pattern_entry_t) *pattern_hash; /**< A hash map containing the pattern. */
117 unsigned bound; /**< Lowest value for pattern output. */
118 unsigned options; /**< Current option mask. */
119 unsigned min_depth; /**< Minimum pattern depth. */
120 unsigned max_depth; /**< Maximum pattern depth. */
126 static pattern_info_t _status, *status = &_status;
129 * Compare two pattern for its occurance counter.
131 static int pattern_count_cmp(const void *elt, const void *key)
135 pattern_entry_t **e1 = (pattern_entry_t **)elt;
136 pattern_entry_t **e2 = (pattern_entry_t **)key;
138 /* we want it sorted in descending order */
139 cmp = cnt_cmp(&(*e2)->count, &(*e1)->count);
142 } /* pattern_count_cmp */
145 * Compare two pattern for its pattern hash.
147 static int pattern_cmp(const void *elt, const void *key)
149 const pattern_entry_t *e1 = (const pattern_entry_t*)elt;
150 const pattern_entry_t *e2 = (const pattern_entry_t*)key;
152 if (e1->len == e2->len)
153 return memcmp(e1->buf, e2->buf, e1->len);
155 return e1->len < e2->len ? -1 : +1;
159 * Initialize a code buffer.
161 * @param buf the code buffer
162 * @param data a buffer address
163 * @param len the length of the data buffer
165 static void init_buf(CODE_BUFFER *buf, BYTE *data, size_t len)
169 buf->end = data + len;
170 buf->hash = 0x2BAD4; /* An arbitrary seed. */
175 * Put a byte into the buffer.
177 * @param buf the code buffer
178 * @param byte the byte to write
180 * The hash value for the buffer content is updated.
182 static inline void put_byte(CODE_BUFFER *buf, BYTE byte)
184 if (buf->next < buf->end) {
186 buf->hash = (buf->hash * 9) ^ byte;
193 * Returns the current length of a buffer.
195 * @param buf the code buffer
197 * @return the length of the buffer content
199 static size_t buf_lenght(const CODE_BUFFER *buf)
201 return buf->next - buf->start;
205 * Returns the current content of a buffer.
207 * @param buf the code buffer
209 * @return the start address of the buffer content
211 static const BYTE *buf_content(const CODE_BUFFER *buf)
217 * Returns the hash value of a buffer.
219 * @param buf the code buffer
221 * @return the hash value of the buffer content
223 static unsigned buf_hash(const CODE_BUFFER *buf)
229 * Returns non-zero if a buffer overrun has occurred.
231 * @param buf the code buffer
233 static unsigned buf_overrun(const CODE_BUFFER *buf)
239 * Returns the next byte from the buffer WITHOUT dropping.
241 * @param buf the code buffer
243 * @return the next byte from the code buffer
245 static inline BYTE look_byte(CODE_BUFFER *buf)
247 if (buf->next < buf->end)
253 * Returns the next byte from the buffer WITH dropping.
255 * @param buf the code buffer
257 * @return the next byte from the code buffer
259 static inline BYTE get_byte(CODE_BUFFER *buf)
261 if (buf->next < buf->end)
266 #define BITS(n) (1 << (n))
269 * Put a 32bit value into the buffer.
271 * @param buf the code buffer
272 * @param code the code to be written into the buffer
274 static void put_code(CODE_BUFFER *buf, unsigned code)
276 if (code < BITS(7)) {
277 put_byte(buf, VLC_7BIT | code);
278 } else if (code < BITS(6 + 8)) {
279 put_byte(buf, VLC_14BIT | (code >> 8));
281 } else if (code < BITS(5 + 8 + 8)) {
282 put_byte(buf, VLC_21BIT | (code >> 16));
283 put_byte(buf, code >> 8);
285 } else if (code < BITS(4 + 8 + 8 + 8)) {
286 put_byte(buf, VLC_28BIT | (code >> 24));
287 put_byte(buf, code >> 16);
288 put_byte(buf, code >> 8);
291 put_byte(buf, VLC_32BIT);
292 put_byte(buf, code >> 24);
293 put_byte(buf, code >> 16);
294 put_byte(buf, code >> 8);
299 #define BIT_MASK(n) ((1 << (n)) - 1)
302 * Get 32 bit from the buffer.
304 * @param buf the code buffer
306 * @return next 32bit value from the code buffer
308 static unsigned get_code(CODE_BUFFER *buf)
310 unsigned code = get_byte(buf);
312 if (code < VLC_14BIT)
314 if (code < VLC_21BIT)
315 return ((code & BIT_MASK(6)) << 8) | get_byte(buf);
316 if (code < VLC_28BIT) {
317 code = ((code & BIT_MASK(5)) << 16) | (get_byte(buf) << 8);
318 code |= get_byte(buf);
321 if (code < VLC_32BIT) {
322 code = ((code & BIT_MASK(4)) << 24) | (get_byte(buf) << 16);
323 code |= get_byte(buf) << 8;
324 code |= get_byte(buf);
327 if (code == VLC_32BIT) {
328 code = get_byte(buf) << 24;
329 code |= get_byte(buf) << 16;
330 code |= get_byte(buf) << 8;
331 code |= get_byte(buf);
334 /* should not happen */
335 panic("Wrong code in buffer");
339 * Put a tag into the buffer.
341 * @param buf the code buffer
342 * @param tag the tag to write to the code buffer
344 static void put_tag(CODE_BUFFER *buf, BYTE tag)
346 assert(tag >= VLC_TAG_FIRST && "invalid tag");
352 * Returns the next tag or zero if the next code isn't a tag.
354 * @param buf the code buffer
356 * @return the next tag in the code buffer
358 static BYTE next_tag(CODE_BUFFER *buf)
360 BYTE b = look_byte(buf);
362 if (b >= VLC_TAG_FIRST)
363 return get_byte(buf);
368 * An Environment for the pattern encoder.
370 typedef struct codec_enc_t {
371 CODE_BUFFER *buf; /**< The current code buffer. */
372 set *id_set; /**< A set containing all already seen Firm nodes. */
373 unsigned curr_id; /**< The current node id. */
374 unsigned options; /**< The encoding options. */
375 pattern_dumper_t *dmp; /**< The dumper for the decoder. */
381 typedef struct addr_entry_t {
382 void *addr; /**< the address */
383 unsigned id; /**< associated ID */
387 * Compare two addresses.
389 static int addr_cmp(const void *p1, const void *p2, size_t size)
391 const addr_entry_t *e1 = (const addr_entry_t*)p1;
392 const addr_entry_t *e2 = (const addr_entry_t*)p2;
395 return e1->addr != e2->addr;
399 * Return the index of a (existing) mode.
401 static size_t find_mode_index(const ir_mode *mode)
403 size_t i, n = get_irp_n_modes();
405 for (i = 0; i < n; ++i) {
406 if (get_irp_mode(i) == mode)
409 /* should really not happen */
410 assert(!"Cound not find index of mode in find_mode_index()");
415 * Encodes an IR-node, recursive worker.
417 * @return reached depth
419 static int _encode_node(ir_node *node, int max_depth, codec_env_t *env)
421 addr_entry_t entry, *r_entry;
426 unsigned code = get_irn_opcode(node);
428 /* insert the node into our ID map */
430 entry.id = env->curr_id;
432 s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), HASH_PTR(node));
433 r_entry = (addr_entry_t *)s_entry->dptr;
435 if (r_entry->id != env->curr_id) {
436 /* already in the map, add an REF */
437 put_tag(env->buf, VLC_TAG_REF);
438 put_code(env->buf, r_entry->id);
442 /* a new entry, proceed */
446 put_code(env->buf, (unsigned)code);
448 /* do we need the mode ? */
449 if (env->options & OPT_WITH_MODE) {
450 ir_mode *mode = get_irn_mode(node);
453 put_code(env->buf, find_mode_index(mode));
455 put_tag(env->buf, VLC_TAG_EMPTY);
458 /* do we need integer constants */
459 if (env->options & OPT_WITH_ICONST) {
460 if (code == iro_Const) {
461 ir_tarval *tv = get_Const_tarval(node);
463 if (tarval_is_long(tv)) {
464 long v = get_tarval_long(tv);
466 put_tag(env->buf, VLC_TAG_ICONST);
467 put_code(env->buf, v);
474 if (max_depth <= 0) {
475 put_code(env->buf, 0);
479 preds = get_irn_arity(node);
480 put_code(env->buf, preds);
483 if (is_op_commutative(get_irn_op(node))) {
484 ir_node *l = get_binop_left(node);
485 ir_node *r = get_binop_right(node);
486 int opcode_diff = (int)get_irn_opcode(l) - (int)get_irn_opcode(r);
488 if (opcode_diff > 0) {
492 } else if (opcode_diff == 0 && l != r) {
493 /* Both nodes have the same opcode, but are different.
494 Need a better method here to decide which goes to the left side. */
497 /* special handling for commutative operators */
498 depth = _encode_node(l, max_depth, env);
501 depth = _encode_node(r, max_depth, env);
505 for (i = 0; i < preds; ++i) {
506 ir_node *n = get_irn_n(node, i);
508 depth = _encode_node(n, max_depth, env);
517 * Encode a DAG starting by the IR-node node.
519 * @param node The root node of the graph
520 * @param buf The code buffer to store the bitstring in
521 * @param max_depth The maximum depth for descending
523 * @return The depth of the encoded graph (without cycles)
525 static int encode_node(ir_node *node, CODE_BUFFER *buf, int max_depth)
530 /* initialize the encoder environment */
532 env.curr_id = 1; /* 0 is used for special purpose */
533 env.options = status->options;
536 if (env.options & OPT_ENC_DAG)
537 env.id_set = new_set(addr_cmp, 32);
541 /* encode options if any for the decoder */
543 put_tag(buf, VLC_TAG_OPTION);
544 put_code(buf, env.options);
547 res = _encode_node(node, max_depth, &env);
549 if (env.id_set != NULL)
552 return max_depth - res;
556 * Decode an IR-node, recursive walker.
558 static void _decode_node(unsigned parent, int position, codec_env_t *env)
562 unsigned mode_code = 0;
566 code = next_tag(env->buf);
567 if (code == VLC_TAG_REF) { /* it's a REF */
568 code = 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 must take the second approach because
576 * we don't know the target here, it's a ref.
578 pattern_dump_edge(env->dmp, code, parent, position, edge_mode);
581 /* dump the node ref */
582 pattern_dump_ref(env->dmp, code);
588 op_code = get_code(env->buf);
590 /* get the mode if encoded */
591 if (env->options & OPT_WITH_MODE) {
592 if (next_tag(env->buf) != VLC_TAG_EMPTY) {
593 mode_code = get_code(env->buf);
597 /* check, if a ICONST attribute is given */
598 if (next_tag(env->buf) == VLC_TAG_ICONST) {
599 iconst = get_code(env->buf);
608 * the mode of a Firm edge can be either computed from its target or
609 * from its source and position. We take the second approach because
610 * we need it anyway for ref's.
612 pattern_dump_edge(env->dmp, env->curr_id, parent, position, edge_mode);
616 parent = env->curr_id;
617 pattern_dump_node(env->dmp, parent, op_code, mode_code, attr);
619 /* ok, we have a new ID */
622 code = next_tag(env->buf);
623 if (code != VLC_TAG_END) {
624 /* more info, do recursion */
627 preds = get_code(env->buf);
629 pattern_start_children(env->dmp, parent);
630 for (i = 0; i < preds; ++i) {
631 _decode_node(parent, i, env);
633 pattern_finish_children(env->dmp, parent);
641 static void decode_node(BYTE *b, size_t len, pattern_dumper_t *dump)
645 unsigned code, options = 0;
647 init_buf(&buf, b, len);
650 env.curr_id = 1; /* 0 is used for special purpose */
654 code = next_tag(&buf);
655 if (code == VLC_TAG_OPTION) {
656 options = get_code(&buf);
658 env.options = options;
660 _decode_node(0, 0, &env);
664 * The environment for the pattern calculation.
666 typedef struct pattern_env {
667 int max_depth; /**< maximum depth for pattern generation. */
671 * Returns the associates pattern_entry_t for a CODE_BUF.
673 * @param buf the code buffer
674 * @param set the hash table containing all pattern entries
676 * @return the associated pattern_entry_t for the given code buffer
678 * If the code content was never seen before, a new pattern_entry is created
681 static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set)
683 pattern_entry_t *key, *elem;
684 size_t len = buf_lenght(buf);
687 key = OALLOCF(&status->obst, pattern_entry_t, buf, len);
689 memcpy(key->buf, buf_content(buf), len);
691 hash = buf_hash(buf);
693 elem = (pattern_entry_t*)pset_find(set, key, hash);
695 obstack_free(&status->obst, key);
699 cnt_clr(&key->count);
700 return (pattern_entry_t*)pset_insert(set, key, hash);
701 } /* pattern_get_entry */
704 * Increase the count for a pattern.
706 * @param buf the code buffer containing the pattern
707 * @param depth the pattern depth
709 * @note Single node patterns are ignored
711 static void count_pattern(CODE_BUFFER *buf, int depth)
713 pattern_entry_t *entry;
715 /* ignore single node pattern (i.e. constants) */
717 entry = pattern_get_entry(buf, status->pattern_hash);
720 cnt_inc(&entry->count);
722 } /* count_pattern */
725 * Pre-walker for nodes pattern calculation.
727 static void calc_nodes_pattern(ir_node *node, void *ctx)
729 pattern_env_t *env = (pattern_env_t*)ctx;
730 BYTE buffer[PATTERN_STORE_SIZE];
734 init_buf(&buf, buffer, sizeof(buffer));
735 depth = encode_node(node, &buf, env->max_depth);
737 if (buf_overrun(&buf)) {
738 lc_fprintf(stderr, "Pattern store: buffer overrun at size %zu. Pattern ignored.\n", sizeof(buffer));
740 count_pattern(&buf, depth);
741 } /* calc_nodes_pattern */
744 * Store all collected patterns.
746 * @param fname filename for storage
748 static void store_pattern(const char *fname)
751 pattern_entry_t *entry;
752 size_t i, count = pset_count(status->pattern_hash);
757 f = fopen(fname, "wb");
763 fwrite("FPS1", 4, 1, f);
764 fwrite(&count, sizeof(count), 1, f);
766 for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash);
768 entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) {
769 fwrite(entry, offsetof(pattern_entry_t, buf) + entry->len, 1, f);
772 } /* store_pattern */
775 * Read collected patterns from a file.
777 * @param fname filename
779 static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname)
782 pattern_entry_t *entry, tmp;
786 HASH_MAP(pattern_entry_t) *pattern_hash = new_pset(pattern_cmp, 8);
787 BYTE buffer[PATTERN_STORE_SIZE];
790 f = fopen(fname, "rb");
796 fread(magic, 4, 1, f);
798 fread(&count, sizeof(count), 1, f);
799 if (memcmp(magic, "FPS1", 4) != 0 || count <= 0) {
800 fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname);
805 /* read all pattern entries and put them into the hash table. */
806 for (i = 0; i < count; ++i) {
807 init_buf(&buf, buffer, sizeof(buffer));
808 fread(&tmp, offsetof(pattern_entry_t, buf), 1, f);
809 for (j = 0; j < tmp.len; ++j)
810 put_byte(&buf, fgetc(f));
811 entry = pattern_get_entry(&buf, pattern_hash);
812 memcpy(&entry->count, &tmp.count, sizeof(entry->count));
816 lc_printf("Read %zu pattern from %s\n", count, fname);
817 assert(pset_count(pattern_hash) == count);
823 * Write the collected patterns to a VCG file for inspection.
825 * @param fname name of the VCG file to create
827 static void pattern_output(const char *fname)
829 pattern_entry_t *entry;
830 pattern_entry_t **pattern_arr;
831 pattern_dumper_t *dump;
832 size_t i, count = pset_count(status->pattern_hash);
834 lc_printf("\n%zu pattern detected\n", count);
839 /* creates a dumper */
840 dump = new_vcg_dumper(fname, 100);
842 pattern_arr = XMALLOCN(pattern_entry_t*, count);
843 for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash);
845 entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) {
846 pattern_arr[i] = entry;
852 qsort(pattern_arr, count, sizeof(*pattern_arr), pattern_count_cmp);
854 for (i = 0; i < count; ++i) {
855 entry = pattern_arr[i];
856 if (cnt_to_uint(&entry->count) < status->bound)
860 pattern_dump_new_pattern(dump, &entry->count);
861 decode_node(entry->buf, entry->len, dump);
862 pattern_dump_finish_pattern(dump);
867 } /* pattern_output */
870 * Calculates the pattern history.
872 void stat_calc_pattern_history(ir_graph *irg)
877 if (! status->enable)
880 /* do NOT count the const code IRG */
881 if (irg == get_const_code_irg())
884 for (i = status->min_depth; i <= status->max_depth; ++i) {
886 irg_walk_graph(irg, calc_nodes_pattern, NULL, &env);
888 } /* stat_calc_pattern_history */
891 * Initializes the pattern history.
893 void stat_init_pattern_history(int enable)
895 HASH_MAP(pattern_entry_t) *pattern_hash = NULL;
897 status->enable = enable;
902 status->options = /* OPT_WITH_MODE | */ OPT_ENC_DAG | OPT_WITH_ICONST | OPT_PERSIST_PATTERN;
903 status->min_depth = 3;
904 status->max_depth = 5;
906 obstack_init(&status->obst);
908 /* create the hash-table */
909 if (status->options & OPT_PERSIST_PATTERN)
910 pattern_hash = read_pattern("pattern.fps");
911 if (pattern_hash == NULL)
912 pattern_hash = new_pset(pattern_cmp, 8);
913 status->pattern_hash = pattern_hash;
914 } /* stat_init_pattern_history */
917 * Finish the pattern history.
919 void stat_finish_pattern_history(const char *fname)
922 if (! status->enable)
925 store_pattern("pattern.fps");
926 pattern_output("pattern.vcg");
928 del_pset(status->pattern_hash);
929 obstack_free(&status->obst, NULL);
932 } /* stat_finish_pattern_history */
934 #endif /* FIRM_STATISTICS */