Annotation of gforth/engine/peephole.c, revision 1.11

1.1       anton       1: /* Peephole optimization routines and tables
                      2: 
1.11    ! anton       3:   Copyright (C) 2001,2002,2003,2007 Free Software Foundation, Inc.
1.1       anton       4: 
                      5:   This file is part of Gforth.
                      6: 
                      7:   Gforth is free software; you can redistribute it and/or
                      8:   modify it under the terms of the GNU General Public License
1.10      anton       9:   as published by the Free Software Foundation, either version 3
1.1       anton      10:   of the License, or (at your option) any later version.
                     11: 
                     12:   This program is distributed in the hope that it will be useful,
                     13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15:   GNU General Public License for more details.
                     16: 
                     17:   You should have received a copy of the GNU General Public License
1.10      anton      18:   along with this program; if not, see http://www.gnu.org/licenses/.
1.1       anton      19: */
                     20: 
                     21: #include "config.h"
                     22: #include "forth.h"
1.2       anton      23: #include <stdlib.h>
1.4       ak042      24: #include <string.h>
                     25: #include <assert.h>
1.1       anton      26: 
                     27: /* the numbers in this struct are primitive indices */
                     28: typedef struct Combination {
                     29:   int prefix;
                     30:   int lastprim;
                     31:   int combination_prim;
                     32: } Combination;
                     33: 
                     34: Combination peephole_table[] = {
1.9       anton      35: #include PEEPHOLE_I
1.1       anton      36: };
                     37: 
1.6       anton      38: #ifdef PRINT_SUPER_LENGTHS
                     39: char *prim_names[] = {
1.9       anton      40: #include PRIM_NAMES_I
1.6       anton      41: };
                     42: 
                     43: Combination *find_super(Cell prim)
                     44: {
                     45:   Cell i;
                     46: 
                     47:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
                     48:     if (peephole_table[i].combination_prim == prim)
                     49:       return &peephole_table[i];
                     50:   }
                     51:   return NULL;
                     52: }
                     53: 
                     54: Cell sum_length(Cell prim)
                     55: {
                     56:   Combination *super=find_super(prim);
                     57: 
                     58:   if (super)
                     59:     return sum_length(super->prefix)+prim_length(super->lastprim);
                     60:   else
                     61:     return prim_length(prim);
                     62: }
                     63: 
                     64: void print_prim(Cell prim)
                     65: {
                     66:   fprintf(stderr, "%s", prim_names[prim]);
                     67: }  
                     68: 
                     69: void print_super(Cell prim)
                     70: {
                     71:   Combination *super=find_super(prim);
                     72:   
                     73:   if (super) {
                     74:     print_super(super->prefix);
                     75:     fprintf(stderr, " ");
                     76:     print_prim(super->lastprim);
                     77:   } else {
                     78:     print_prim(prim);
                     79:   }
                     80: }
                     81: 
                     82: void print_super_lengths()
                     83: {
                     84:   Cell i;
                     85: 
                     86:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
                     87:     Cell super_length = prim_length(peephole_table[i].combination_prim);
                     88:     Cell sum_super_length = sum_length(peephole_table[i].combination_prim);
                     89: 
                     90:     fprintf(stderr, "%6.4f %3d %3d ", ((double)super_length)/sum_super_length,
                     91:            super_length, sum_super_length);
                     92:     print_super(peephole_table[i].combination_prim);
                     93:     fprintf(stderr,"\n");
                     94:   }
                     95: }
                     96: #endif
                     97: 
1.3       anton      98: int use_super = 1;
                     99: 
                    100: typedef Xt Inst;
1.1       anton     101: 
1.3       anton     102: typedef struct Peeptable_entry {
                    103:   struct Peeptable_entry *next;
                    104:   Inst prefix;
                    105:   Inst lastprim;
                    106:   Inst combination_prim;
                    107: } Peeptable_entry;
1.1       anton     108: 
1.3       anton     109: #define HASH_SIZE 1024
                    110: #define hash(_i1,_i2) (((((Cell)(_i1))^((Cell)(_i2)))>>4)&(HASH_SIZE-1))
1.1       anton     111: 
1.3       anton     112: Cell peeptable;
1.1       anton     113: 
1.3       anton     114: Cell prepare_peephole_table(Inst insts[])
1.1       anton     115: {
                    116:   Cell i;
1.3       anton     117:   Peeptable_entry **pt = (Peeptable_entry **)calloc(HASH_SIZE,sizeof(Peeptable_entry *));
1.1       anton     118: 
1.3       anton     119:   for (i=0; i<sizeof(peephole_table)/sizeof(peephole_table[0]); i++) {
1.1       anton     120:     Combination *c = &peephole_table[i];
1.3       anton     121:     Peeptable_entry *p = (Peeptable_entry *)malloc(sizeof(Peeptable_entry));
                    122:     Cell h;
                    123:     p->prefix =           insts[c->prefix];
                    124:     p->lastprim =         insts[c->lastprim];
                    125:     p->combination_prim = insts[c->combination_prim];
                    126:     h = hash(p->prefix,p->lastprim);
                    127:     p->next = pt[h];
                    128:     pt[h] = p;
1.1       anton     129:   }
1.3       anton     130:   return (Cell)pt;
                    131: }
                    132: 
                    133: Inst peephole_opt(Inst inst1, Inst inst2, Cell peeptable)
                    134: {
                    135:   Peeptable_entry **pt = (Peeptable_entry **)peeptable;
                    136:   Peeptable_entry *p;
                    137: 
                    138:   if (use_super == 0)
                    139:       return 0;
                    140:   for (p = pt[hash(inst1,inst2)]; p != NULL; p = p->next)
                    141:     if (inst1 == p->prefix && inst2 == p->lastprim)
                    142:       return p->combination_prim;
                    143:   return NULL;
1.1       anton     144: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>