OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [cuc/] [timings.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 879 markom
/* timings.c -- OpenRISC Custom Unit Compiler, timing and size estimation
2
 *    Copyright (C) 2002 Marko Mlinar, markom@opencores.org
3
 *
4
 *    This file is part of OpenRISC 1000 Architectural Simulator.
5
 *
6
 *    This program is free software; you can redistribute it and/or modify
7
 *    it under the terms of the GNU General Public License as published by
8
 *    the Free Software Foundation; either version 2 of the License, or
9
 *    (at your option) any later version.
10
 *
11
 *    This program is distributed in the hope that it will be useful,
12
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *    GNU General Public License for more details.
15
 *
16
 *    You should have received a copy of the GNU General Public License
17
 *    along with this program; if not, write to the Free Software
18
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <stdarg.h>
23
#include <assert.h>
24
#include <math.h>
25 1308 phoenix
#include <string.h>
26
 
27 1350 nogj
#include "config.h"
28
 
29
#ifdef HAVE_INTTYPES_H
30
#include <inttypes.h>
31
#endif
32
 
33
#include "port.h"
34
#include "arch.h"
35 1308 phoenix
#include "abstract.h"
36 897 markom
#include "sim-config.h"
37 879 markom
#include "cuc.h"
38
#include "insn.h"
39
 
40
static cuc_timing_table *timing_table;
41 897 markom
static double max_bb_delay;
42 879 markom
 
43 883 markom
/* Returns instruction delay */
44
double insn_time (cuc_insn *ii)
45 879 markom
{
46 930 markom
  if (ii->opt[2] & OPT_CONST) {
47
    if (ii->opt[1] & OPT_CONST) return 0.;
48
    else return timing_table[ii->index].delayi;
49
  } else return timing_table[ii->index].delay;
50 879 markom
}
51
 
52 883 markom
/* Returns instruction size */
53
double insn_size (cuc_insn *ii)
54
{
55 930 markom
  double s = (ii->opt[2] & OPT_CONST) ? timing_table[ii->index].sizei
56
          : timing_table[ii->index].size;
57
  if (ii->opt[1] & OPT_CONST) return 0.;
58
  if (ii->type & IT_COND && (ii->index == II_CMOV || ii->index == II_ADD)) return s / 32.;
59
  else return s;
60 883 markom
}
61
 
62
/* Returns normal instruction size */
63
double ii_size (int index, int imm)
64
{
65
  if (imm) return timing_table[index].sizei;
66
  else return timing_table[index].size;
67
}
68
 
69 879 markom
/* Returns dataflow tree height in cycles */
70
static double max_delay (cuc_func *f, int b)
71
{
72
  double max_d = 0.;
73
  double *d;
74
  cuc_bb *bb = &f->bb[b];
75
  int i, j;
76
  d = (double *) malloc (sizeof (double) * bb->ninsn);
77
  for (i = 0; i < bb->ninsn; i++) {
78
    double md = 0.;
79
    for (j = 0; j < MAX_OPERANDS; j++) {
80
      int op = bb->insn[i].op[j];
81
      if (bb->insn[i].opt[j] & OPT_REF && op >= 0 && REF_BB (op) == b && REF_I (op) < i) {
82
        double t = d[REF_I (op)];
83
        if (t > md) md = t;
84
      }
85
    }
86
    d[i] = md + insn_time (&bb->insn[i]);
87
    if (d[i] > max_d) max_d = d[i];
88
  }
89
  free (d);
90 997 markom
  //PRINTF ("max_d%i=%f\n", b, max_d);
91 879 markom
  return max_d;
92
}
93
 
94
/* Calculates memory delay of a single run of a basic block */
95
static int memory_delay (cuc_func *f, int b)
96
{
97
  int i;
98
  int d = 0;
99
  for (i = 0; i < f->nmsched; i++)
100
    if (REF_BB (f->msched[i]) == b) {
101 907 markom
      if (f->mtype[i] & MT_STORE) {
102 897 markom
        if (!(f->mtype[i] & MT_BURST) || f->mtype[i] & MT_BURSTE) d += runtime.cuc.mdelay[2];
103
        else d += runtime.cuc.mdelay[3];
104 907 markom
      } else if (f->mtype[i] & MT_LOAD) {
105 897 markom
        if (!(f->mtype[i] & MT_BURST) || f->mtype[i] & MT_BURSTE) d += runtime.cuc.mdelay[0];
106
        else d += runtime.cuc.mdelay[1];
107 879 markom
      }
108
    }
109 997 markom
  //PRINTF ("md%i=%i\n", b, d);
110 879 markom
  return d;
111
}
112
 
113
/* Cuts the tree and marks registers */
114
void cut_tree (cuc_func *f, int b, double sd)
115
{
116
  int i, j;
117
  double *depths;
118
  cuc_bb *bb = &f->bb[b];
119
  depths = (double *) malloc (sizeof (double) * bb->ninsn);
120
 
121
  for (i = 0; i < bb->ninsn; i++) {
122
    double md = 0.;
123
    int mg = 0;
124
    for (j = 0; j < MAX_OPERANDS; j++) {
125
      int op = bb->insn[i].op[j];
126
      if (bb->insn[i].opt[j] & OPT_REF && op >= 0 && REF_BB (op) == b && REF_I (op) < i) {
127
        double t = depths[REF_I (op)];
128
        if (f->INSN(op).type & IT_CUT) {
129
          if (f->INSN(op).tmp + 1 >= mg) {
130
            if (f->INSN(op).tmp + 1 > mg) md = 0.;
131
            mg = f->INSN(op).tmp + 1;
132
            if (t > md) md = t;
133
          }
134
        } else {
135
          if (f->INSN(op).tmp >= mg) {
136
            if (f->INSN(op).tmp > mg) md = 0.;
137
            mg = f->INSN(op).tmp;
138
            if (t > md) md = t;
139
          }
140
        }
141
      }
142
    }
143 997 markom
    //PRINTF ("%2x md%.1f ", i, md);
144 879 markom
    md += insn_time (&bb->insn[i]);
145 997 markom
    //PRINTF ("md%.1f mg%i %.1f\n", md, mg, sd);
146 879 markom
    bb->insn[i].tmp = mg;
147
    if (md > sd) {
148
      bb->insn[i].type |= IT_CUT;
149 897 markom
      if (md > runtime.cuc.cycle_duration)
150 879 markom
        log ("WARNING: operation t%x_%x may need to be registered inbetween\n", b, i);
151
      depths[i] = 0.;
152
    } else depths[i] = md;
153
  }
154
  free (depths);
155
}
156
 
157
/* How many cycles we need now to get through the BB */
158
static int new_bb_cycles (cuc_func *f, int b, int cut)
159
{
160
  long d;
161
  double x = max_delay (f, b);
162 897 markom
  d = ceil (x / runtime.cuc.cycle_duration);
163 879 markom
  if (d < 1) d = 1;
164 897 markom
  if (cut && x > runtime.cuc.cycle_duration) cut_tree (f, b, x / d);
165 879 markom
 
166
  if (x / d > max_bb_delay) max_bb_delay = x / d;
167 883 markom
 
168 879 markom
  return memory_delay (f, b) + d;
169
}
170
 
171
/* Cuts the tree and marks registers */
172
void mark_cut (cuc_func *f)
173
{
174
  int b, i;
175
  for (b = 0; b < f->num_bb; b++)
176
    for (i = 0; i < f->bb[b].ninsn; i++)
177
      f->bb[b].insn[i].tmp = 0; /* Set starting groups */
178 897 markom
  if (config.cuc.no_multicycle)
179 879 markom
    for (b = 0; b < f->num_bb; b++)
180
      new_bb_cycles (f, b, 1);
181
}
182
 
183
/* Returns basic block circuit area */
184
static double bb_size (cuc_bb *bb)
185
{
186
  int i;
187
  double d = 0.;
188
  for (i = 0; i < bb->ninsn; i++) {
189
    if (bb->insn[i].opt[2] & OPT_CONST)
190
      d = d + timing_table[bb->insn[i].index].sizei;
191
    else d = d + timing_table[bb->insn[i].index].size;
192
  }
193
  return d;
194
}
195
 
196
/* Recalculates bb[].cnt values, based on generated profile file */
197
void recalc_cnts (cuc_func *f, char *bb_filename)
198
{
199
  int i, r, b, prevbb = -1, prevcnt = 0;
200
  int buf[256];
201
  const int bufsize = 256;
202
  FILE *fi = fopen (bb_filename, "rb");
203
 
204
  assert (fi);
205
 
206
  /* initialize counts */
207
  for (b = 0; b < f->num_bb; b++) f->bb[b].cnt = 0;
208
 
209
  /* read control flow from file and set counts */
210
  do {
211
    r = fread (buf, sizeof (int), bufsize, fi);
212
    for (i = 0; i < r; i++) {
213
      b = f->init_bb_reloc[buf[i]];
214
      if (b < 0) continue;
215
      /* Were we in the loop? */
216
      if (b == prevbb) {
217
        prevcnt++;
218
      } else {
219
        /* End the block */
220 932 markom
        if (prevbb >= 0 && prevbb != BBID_START)
221
          f->bb[prevbb].cnt += prevcnt / f->bb[prevbb].unrolled + 1;
222 879 markom
        prevcnt = 0;
223
        prevbb = b;
224
      }
225
    }
226
  } while (r == bufsize);
227
 
228
  fclose (fi);
229
}
230
 
231
/* Analizes current version of design and places results into timings structure */
232
void analyse_timings (cuc_func *f, cuc_timings *timings)
233
{
234
  long new_time = 0;
235
  double size = 0.;
236 883 markom
  int b, i;
237 879 markom
 
238 883 markom
  /* Add time needed for mtspr/mfspr */
239
  for (i = 0; i < MAX_REGS; i++) if (f->used_regs[i]) new_time++;
240
  new_time++; /* always one mfspr at the end */
241
  new_time *= f->num_runs;
242
 
243 879 markom
  max_bb_delay = 0.;
244
  for (b = 0; b < f->num_bb; b++) {
245
    new_time += new_bb_cycles (f, b, 0) * f->bb[b].cnt;
246
    size = size + bb_size (&f->bb[b]);
247
  }
248
  timings->new_time = new_time;
249
  timings->size = size;
250
  log ("Max circuit delay %.2fns; max circuit clock speed %.1fMHz\n",
251
                  max_bb_delay, 1000. / max_bb_delay);
252
}
253
 
254
/* Loads in the specified timings table */
255
void load_timing_table (char *filename)
256
{
257
  int i;
258
  FILE *fi;
259
 
260
  log ("Loading timings from %s\n", filename);
261 897 markom
  log ("Using clock delay %.2fns (frequency %.0fMHz)\n", runtime.cuc.cycle_duration,
262
                 1000. / runtime.cuc.cycle_duration);
263 879 markom
  assert (fi = fopen (filename, "rt"));
264
 
265
  timing_table = (cuc_timing_table *)malloc ((II_LAST + 1) * sizeof (cuc_timing_table));
266
  assert (timing_table);
267
  for (i = 0; i <= II_LAST; i++) {
268
    timing_table[i].size = -1.;
269
    timing_table[i].sizei = -1.;
270
    timing_table[i].delay = -1.;
271
    timing_table[i].delayi = -1.;
272
  }
273
 
274
  while (!feof(fi)) {
275
    char tmp[256];
276
    int index;
277
    if (fscanf (fi, "%s", tmp) != 1) break;
278
    if (tmp[0] == '#') {
279
      while (!feof (fi) && fgetc (fi) != '\n');
280
      continue;
281
    }
282
    for (i = 0; i <= II_LAST; i++)
283
      if (strcmp (known[i].name, tmp) == 0) {
284
        index = i;
285
        break;
286
      }
287
    assert (index <= II_LAST);
288
    i = index;
289
    if (fscanf (fi, "%lf%lf%lf%lf\n", &timing_table[i].size,
290
                &timing_table[i].sizei, &timing_table[i].delay, &timing_table[i].delayi) != 4) break;
291 997 markom
    /*PRINTF ("!%s size %f,%f delay %f,%f\n", known[i].name, timing_table[i].size,
292 879 markom
                    timing_table[i].sizei, timing_table[i].delay, timing_table[i].delayi);*/
293
  }
294
 
295
  /* Was everything initialized? */
296
  for (i = 0; i <= II_LAST; i++) {
297
    assert (timing_table[i].size >= 0 && timing_table[i].sizei >= 0
298
     && timing_table[i].delay >= 0 && timing_table[i].delayi >= 0);
299 997 markom
    /*PRINTF ("%s size %f,%f delay %f,%f\n", known[i], timing_table[i].size,
300 879 markom
                    timing_table[i].sizei, timing_table[i].delay, timing_table[i].delayi);*/
301
  }
302
 
303
  fclose (fi);
304
}
305
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.