20 #include "pattern_dmp.h"
24 * just be make some things clear :-), the
27 #define HASH_MAP(type) pset_##type
29 typedef pset pset_pattern_entry_t;
31 typedef unsigned char BYTE;
33 /** Maximum size of the pattern store. */
34 #define PATTERN_STORE_SIZE 2048
40 typedef struct _code_buf_t {
41 BYTE *next; /**< Next byte address to be written. */
42 BYTE *end; /**< End address of the buffer. */
43 BYTE *start; /**< Start address of the buffer. */
44 unsigned hash; /**< The hash value for the buffer content. */
45 unsigned overrun; /**< flag set if the buffer was overrun */
52 VLC_7BIT = 0x00, /**< 8 bit code, carrying 7 bits payload */
53 VLC_14BIT = 0x80, /**< 16 bit code, carrying 14 bits payload */
54 VLC_21BIT = 0xC0, /**< 24 bit code, carrying 21 bits payload */
55 VLC_28BIT = 0xE0, /**< 32 bit code, carrying 28 bits payload */
56 VLC_32BIT = 0xF0, /**< 40 bit code, carrying 32 bits payload */
58 VLC_TAG_FIRST = 0xF1, /**< First possible tag value. */
59 VLC_TAG_ICONST = 0xFB, /**< Encodes an integer constant. */
60 VLC_TAG_EMPTY = 0xFC, /**< Encodes an empty entity. */
61 VLC_TAG_OPTION = 0xFD, /**< Options exists. */
62 VLC_TAG_REF = 0xFE, /**< Special tag, next code is an ID. */
63 VLC_TAG_END = 0xFF, /**< End tag. */
67 * An entry for holding one pattern.
69 typedef struct _pattern_entry_t {
70 counter_t count; /**< Amount of pattern occurance. */
71 unsigned len; /**< The length of the VLC encoded buffer. */
72 BYTE buf[1]; /**< The buffer containing the VLC encoded pattern. */
76 * Current options for the pattern matcher.
79 OPT_WITH_MODE = 0x00000001, /**< use modes */
80 OPT_ENC_DAG = 0x00000002, /**< encode DAGs, not terms */
81 OPT_WITH_ICONST = 0x00000004, /**< encode integer constants */
82 OPT_PERSIST_PATTERN = 0x00000008, /**< persistent pattern hash */
89 typedef struct _pattern_info_t {
90 int enable; /**< If non-zero, this module is enabled. */
91 struct obstack obst; /**< An obstack containing the counters. */
92 HASH_MAP(pattern_entry_t) *pattern_hash; /**< A hash map containing the pattern. */
93 unsigned bound; /**< Lowest value for pattern output. */
94 unsigned options; /**< Current option mask. */
95 unsigned min_depth; /**< Minimum pattern depth. */
96 unsigned max_depth; /**< Maximum pattern depth. */
102 static pattern_info_t _status, *status = &_status;
105 * Compare two pattern for its occurance counter.
107 static int pattern_count_cmp(const void *elt, const void *key) {
110 pattern_entry_t **e1 = (pattern_entry_t **)elt;
111 pattern_entry_t **e2 = (pattern_entry_t **)key;
113 /* we want it sorted in descending order */
114 cmp = cnt_cmp(&(*e2)->count, &(*e1)->count);
117 } /* pattern_count_cmp */
120 * Compare two pattern for its pattern hash.
122 static int pattern_cmp(const void *elt, const void *key) {
123 const pattern_entry_t *e1 = elt;
124 const pattern_entry_t *e2 = key;
125 int diff = e1->len - e2->len;
130 return memcmp(e1->buf, e2->buf, e1->len);
134 * Initialize a code buffer.
136 * @param buf the code buffer
137 * @param data a buffer address
138 * @param len the length of the data buffer
140 static void init_buf(CODE_BUFFER *buf, BYTE *data, unsigned len) {
143 buf->end = data + len;
144 buf->hash = 0x2BAD4; /* An arbitrary seed. */
149 * Put a byte into the buffer.
151 * @param buf the code buffer
152 * @param byte the byte to write
154 * The hash value for the buffer content is updated.
156 static INLINE void put_byte(CODE_BUFFER *buf, BYTE byte) {
157 if (buf->next < buf->end) {
159 buf->hash = (buf->hash * 9) ^ byte;
166 * Returns the current length of a buffer.
168 * @param buf the code buffer
170 * @return the length of the buffer content
172 static unsigned buf_lenght(const CODE_BUFFER *buf) {
173 return buf->next - buf->start;
177 * Returns the current content of a buffer.
179 * @param buf the code buffer
181 * @return the start address of the buffer content
183 static const BYTE *buf_content(const CODE_BUFFER *buf) {
188 * Returns the hash value of a buffer.
190 * @param buf the code buffer
192 * @return the hash value of the buffer content
194 static unsigned buf_hash(const CODE_BUFFER *buf) {
199 * Returns non-zero if a buffer overrun has occurred.
201 * @param buf the code buffer
203 static unsigned buf_overrun(const CODE_BUFFER *buf) {
208 * Returns the next byte from the buffer WITHOUT dropping.
210 * @param buf the code buffer
212 * @return the next byte from the code buffer
214 static INLINE BYTE look_byte(CODE_BUFFER *buf) {
215 if (buf->next < buf->end)
221 * Returns the next byte from the buffer WITH dropping.
223 * @param buf the code buffer
225 * @return the next byte from the code buffer
227 static INLINE BYTE get_byte(CODE_BUFFER *buf) {
228 if (buf->next < buf->end)
233 #define BITS(n) (1 << (n))
236 * Put a 32bit value into the buffer.
238 * @param buf the code buffer
239 * @param code the code to be written into the buffer
241 static void put_code(CODE_BUFFER *buf, unsigned code) {
242 if (code < BITS(7)) {
243 put_byte(buf, VLC_7BIT | code);
244 } else if (code < BITS(6 + 8)) {
245 put_byte(buf, VLC_14BIT | (code >> 8));
247 } else if (code < BITS(5 + 8 + 8)) {
248 put_byte(buf, VLC_21BIT | (code >> 16));
249 put_byte(buf, code >> 8);
251 } else if (code < BITS(4 + 8 + 8 + 8)) {
252 put_byte(buf, VLC_28BIT | (code >> 24));
253 put_byte(buf, code >> 16);
254 put_byte(buf, code >> 8);
257 put_byte(buf, VLC_32BIT);
258 put_byte(buf, code >> 24);
259 put_byte(buf, code >> 16);
260 put_byte(buf, code >> 8);
265 #define BIT_MASK(n) ((1 << (n)) - 1)
268 * Get 32 bit from the buffer.
270 * @param buf the code buffer
272 * @return next 32bit value from the code buffer
274 static unsigned get_code(CODE_BUFFER *buf) {
275 unsigned code = get_byte(buf);
277 if (code < VLC_14BIT)
279 if (code < VLC_21BIT)
280 return ((code & BIT_MASK(6)) << 8) | get_byte(buf);
281 if (code < VLC_28BIT) {
282 code = ((code & BIT_MASK(5)) << 16) | (get_byte(buf) << 8);
283 code |= get_byte(buf);
286 if (code < VLC_32BIT) {
287 code = ((code & BIT_MASK(4)) << 24) | (get_byte(buf) << 16);
288 code |= get_byte(buf) << 8;
289 code |= get_byte(buf);
292 if (code == VLC_32BIT) {
293 code = get_byte(buf) << 24;
294 code |= get_byte(buf) << 16;
295 code |= get_byte(buf) << 8;
296 code |= get_byte(buf);
299 /* should not happen */
300 assert(0 && "Wrong code in buffer");
306 * Put a tag into the buffer.
308 * @param buf the code buffer
309 * @param tag the tag to write to the code buffer
311 static void put_tag(CODE_BUFFER *buf, BYTE tag) {
312 assert(tag >= VLC_TAG_FIRST && "invalid tag");
318 * Returns the next tag or zero if the next code isn't a tag.
320 * @param buf the code buffer
322 * @return the next tag in the code buffer
324 static BYTE next_tag(CODE_BUFFER *buf) {
325 BYTE b = look_byte(buf);
327 if (b >= VLC_TAG_FIRST)
328 return get_byte(buf);
333 * An Environment for the pattern encoder.
335 typedef struct _codec_enc_t {
336 CODE_BUFFER *buf; /**< The current code buffer. */
337 set *id_set; /**< A set containing all already seen Firm nodes. */
338 unsigned curr_id; /**< The current node id. */
339 unsigned options; /**< The encoding options. */
340 pattern_dumper_t *dmp; /**< The dumper for the decoder. */
346 typedef struct _addr_entry_t {
347 void *addr; /**< the address */
348 unsigned id; /**< associated ID */
352 * Compare two addresses.
354 static int addr_cmp(const void *p1, const void *p2, size_t size) {
355 const addr_entry_t *e1 = p1;
356 const addr_entry_t *e2 = p2;
358 return e1->addr != e2->addr;
362 * Encodes an IR-node, recursive worker.
364 * @return reached depth
366 static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) {
367 addr_entry_t entry, *r_entry;
372 ir_opcode code = get_irn_opcode(node);
374 /* insert the node into our ID map */
376 entry.id = env->curr_id;
378 s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), HASH_PTR(node));
379 r_entry = (addr_entry_t *)s_entry->dptr;
381 if (r_entry->id != env->curr_id) {
382 /* already in the map, add an REF */
383 put_tag(env->buf, VLC_TAG_REF);
384 put_code(env->buf, r_entry->id);
388 /* a new entry, proceed */
392 put_code(env->buf, (unsigned)code);
394 /* do we need the mode ? */
395 if (env->options & OPT_WITH_MODE) {
396 ir_mode *mode = get_irn_mode(node);
399 /* FIXME: not 64bit save */
400 put_code(env->buf, (unsigned)mode);
402 put_tag(env->buf, VLC_TAG_EMPTY);
405 /* do we need integer constants */
406 if (env->options & OPT_WITH_ICONST) {
407 if (code == iro_Const) {
408 tarval *tv = get_Const_tarval(node);
410 if (tarval_is_long(tv)) {
411 long v = get_tarval_long(tv);
413 put_tag(env->buf, VLC_TAG_ICONST);
414 put_code(env->buf, v);
421 if (max_depth <= 0) {
422 put_code(env->buf, 0);
426 preds = get_irn_arity(node);
427 put_code(env->buf, preds);
430 if (is_op_commutative(get_irn_op(node))) {
431 ir_node *l = get_binop_left(node);
432 ir_node *r = get_binop_right(node);
433 int opcode_diff = (int)get_irn_opcode(l) - (int)get_irn_opcode(r);
435 if (opcode_diff > 0) {
439 } else if (opcode_diff == 0 && l != r) {
440 /* Both nodes have the same opcode, but are different.
441 Need a better method here to decide which goes to the left side. */
444 /* special handling for commutative operators */
445 depth = _encode_node(l, max_depth, env);
448 depth = _encode_node(r, max_depth, env);
452 for (i = 0; i < preds; ++i) {
453 ir_node *n = get_irn_n(node, i);
455 depth = _encode_node(n, max_depth, env);
464 * Encode a DAG starting by the IR-node node.
466 * @param node The root node of the graph
467 * @param buf The code buffer to store the bitstring in
468 * @param max_depth The maximum depth for descending
470 * @return The depth of the encoded graph (without cycles)
472 static int encode_node(ir_node *node, CODE_BUFFER *buf, int max_depth) {
476 /* initialize the encoder environment */
478 env.curr_id = 1; /* 0 is used for special purpose */
479 env.options = status->options;
482 if (env.options & OPT_ENC_DAG)
483 env.id_set = new_set(addr_cmp, 32);
487 /* encode options if any for the decoder */
489 put_tag(buf, VLC_TAG_OPTION);
490 put_code(buf, env.options);
493 res = _encode_node(node, max_depth, &env);
495 if (env.id_set != NULL)
498 return max_depth - res;
502 * Decode an IR-node, recursive walker.
504 static void _decode_node(unsigned parent, int position, codec_env_t *env) {
507 unsigned mode_code = 0;
511 code = next_tag(env->buf);
512 if (code == VLC_TAG_REF) { /* it's a REF */
513 code = get_code(env->buf);
519 * the mode of a Firm edge can be either computed from its target or
520 * from its source and position. We must take the second approach because
521 * we don't know the target here, it's a ref.
523 pattern_dump_edge(env->dmp, code, parent, position, edge_mode);
526 /* dump the node ref */
527 pattern_dump_ref(env->dmp, code);
533 op_code = get_code(env->buf);
535 /* get the mode if encoded */
536 if (env->options & OPT_WITH_MODE) {
537 if (next_tag(env->buf) != VLC_TAG_EMPTY) {
538 mode_code = get_code(env->buf);
542 /* check, if a ICONST attribute is given */
543 if (next_tag(env->buf) == VLC_TAG_ICONST) {
544 iconst = get_code(env->buf);
553 * the mode of a Firm edge can be either computed from its target or
554 * from its source and position. We take the second approach because
555 * we need it anyway for ref's.
557 pattern_dump_edge(env->dmp, env->curr_id, parent, position, edge_mode);
561 parent = env->curr_id;
562 pattern_dump_node(env->dmp, parent, op_code, mode_code, attr);
564 /* ok, we have a new ID */
567 code = next_tag(env->buf);
568 if (code != VLC_TAG_END) {
569 /* more info, do recursion */
572 preds = get_code(env->buf);
574 pattern_start_children(env->dmp, parent);
575 for (i = 0; i < preds; ++i) {
576 _decode_node(parent, i, env);
578 pattern_finish_children(env->dmp, parent);
586 static void decode_node(BYTE *b, unsigned len, pattern_dumper_t *dump) {
589 unsigned code, options = 0;
591 init_buf(&buf, b, len);
594 env.curr_id = 1; /* 0 is used for special purpose */
598 code = next_tag(&buf);
599 if (code == VLC_TAG_OPTION) {
600 options = get_code(&buf);
602 env.options = options;
604 _decode_node(0, 0, &env);
608 * The environment for the pattern calculation.
610 typedef struct _pattern_env {
611 int max_depth; /**< maximum depth for pattern generation. */
615 * Returns the associates pattern_entry_t for a CODE_BUF.
617 * @param buf the code buffer
618 * @param set the hash table containing all pattern entries
620 * @return the associated pattern_entry_t for the given code buffer
622 * If the code content was never seen before, a new pattern_entry is created
625 static pattern_entry_t *pattern_get_entry(CODE_BUFFER *buf, pset *set) {
626 pattern_entry_t *key, *elem;
627 unsigned len = buf_lenght(buf);
630 key = obstack_alloc(&status->obst, offsetof(pattern_entry_t, buf) + len);
634 memcpy(key->buf, buf_content(buf), len);
636 hash = buf_hash(buf);
638 elem = pset_find(set, key, hash);
640 obstack_free(&status->obst, key);
644 cnt_clr(&key->count);
645 return pset_insert(set, key, hash);
646 } /* pattern_get_entry */
649 * Increase the count for a pattern.
651 * @param buf the code buffer containing the pattern
652 * @param depth the pattern depth
654 * @note Single node patterns are ignored
656 static void count_pattern(CODE_BUFFER *buf, int depth) {
657 pattern_entry_t *entry;
659 /* ignore single node pattern (i.e. constants) */
661 entry = pattern_get_entry(buf, status->pattern_hash);
664 cnt_inc(&entry->count);
666 } /* count_pattern */
669 * Pre-walker for nodes pattern calculation.
671 static void calc_nodes_pattern(ir_node *node, void *ctx) {
672 pattern_env_t *env = ctx;
673 BYTE buffer[PATTERN_STORE_SIZE];
677 init_buf(&buf, buffer, sizeof(buffer));
678 depth = encode_node(node, &buf, env->max_depth);
680 if (buf_overrun(&buf)) {
681 fprintf(stderr, "Pattern store: buffer overrun at size %d. Pattern ignored.\n", sizeof(buffer));
683 count_pattern(&buf, depth);
684 } /* calc_nodes_pattern */
687 * Store all collected patterns.
689 * @param fname filename for storage
691 static void store_pattern(const char *fname) {
693 pattern_entry_t *entry;
694 int i, count = pset_count(status->pattern_hash);
699 f = fopen(fname, "wb");
705 fwrite("FPS1", 4, 1, f);
706 fwrite(&count, sizeof(count), 1, f);
708 for (i = 0, entry = pset_first(status->pattern_hash);
710 entry = pset_next(status->pattern_hash), ++i) {
711 fwrite(entry, offsetof(pattern_entry_t, buf) + entry->len, 1, f);
714 } /* store_pattern */
717 * Read collected patterns from a file.
719 * @param fname filename
721 static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) {
723 pattern_entry_t *entry, tmp;
727 HASH_MAP(pattern_entry_t) *pattern_hash = new_pset(pattern_cmp, 8);
728 BYTE buffer[PATTERN_STORE_SIZE];
731 f = fopen(fname, "rb");
737 fread(magic, 4, 1, f);
739 fread(&count, sizeof(count), 1, f);
740 if (memcmp(magic, "FPS1", 4) != 0 || count <= 0) {
741 fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname);
746 /* read all pattern entries and put them into the hash table. */
747 for (i = 0; i < count; ++i) {
748 init_buf(&buf, buffer, sizeof(buffer));
749 fread(&tmp, offsetof(pattern_entry_t, buf), 1, f);
750 for (j = 0; j < tmp.len; ++j)
751 put_byte(&buf, fgetc(f));
752 entry = pattern_get_entry(&buf, pattern_hash);
753 memcpy(&entry->count, &tmp.count, sizeof(entry->count));
757 printf("Read %d pattern from %s\n", count, fname);
758 assert(pset_count(pattern_hash) == count);
764 * Write the collected patterns to a VCG file for inspection.
766 * @param fname name of the VCG file to create
768 static void pattern_output(const char *fname) {
769 pattern_entry_t *entry;
770 pattern_entry_t **pattern_arr;
771 pattern_dumper_t *dump;
772 int i, count = pset_count(status->pattern_hash);
774 printf("\n%d pattern detected\n", count);
779 /* creates a dumper */
780 dump = new_vcg_dumper(fname, 100);
782 pattern_arr = xmalloc(sizeof(*pattern_arr) * count);
783 for (i = 0, entry = pset_first(status->pattern_hash);
785 entry = pset_next(status->pattern_hash), ++i) {
786 pattern_arr[i] = entry;
792 qsort(pattern_arr, count, sizeof(*pattern_arr), pattern_count_cmp);
794 for (i = 0; i < count; ++i) {
795 entry = pattern_arr[i];
796 if (cnt_to_uint(&entry->count) < status->bound)
800 pattern_dump_new_pattern(dump, &entry->count);
801 decode_node(entry->buf, entry->len, dump);
802 pattern_dump_finish_pattern(dump);
807 } /* pattern_output */
810 * Calculates the pattern history.
812 void stat_calc_pattern_history(ir_graph *irg) {
816 if (! status->enable)
819 /* do NOT count the const code IRG */
820 if (irg == get_const_code_irg())
823 for (i = status->min_depth; i <= status->max_depth; ++i) {
825 irg_walk_graph(irg, calc_nodes_pattern, NULL, &env);
827 } /* stat_calc_pattern_history */
830 * Initializes the pattern history.
832 void stat_init_pattern_history(int enable) {
833 HASH_MAP(pattern_entry_t) *pattern_hash = NULL;
835 status->enable = enable;
840 status->options = /* OPT_WITH_MODE | */ OPT_ENC_DAG | OPT_WITH_ICONST | OPT_PERSIST_PATTERN;
841 status->min_depth = 3;
842 status->max_depth = 5;
844 obstack_init(&status->obst);
846 /* create the hash-table */
847 if (status->options & OPT_PERSIST_PATTERN)
848 pattern_hash = read_pattern("pattern.fps");
849 if (pattern_hash == NULL)
850 pattern_hash = new_pset(pattern_cmp, 8);
851 status->pattern_hash = pattern_hash;
852 } /* stat_init_pattern_history */
855 * Finish the pattern history.
857 void stat_finish_pattern_history(const char *fname) {
858 if (! status->enable)
861 store_pattern("pattern.fps");
862 pattern_output("pattern.vcg");
864 del_pset(status->pattern_hash);
865 obstack_free(&status->obst, NULL);
868 } /* stat_finish_pattern_history */