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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [cuc/] [timings.c] - Blame information for rev 759

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* 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
 
26
#include "config.h"
27
 
28
#include "port.h"
29
#include "arch.h"
30
#include "abstract.h"
31
#include "sim-config.h"
32
#include "cuc.h"
33
#include "insn.h"
34
 
35
static cuc_timing_table *timing_table;
36
static double max_bb_delay;
37
 
38
/* Returns instruction delay */
39
double insn_time (cuc_insn *ii)
40
{
41
  if (ii->opt[2] & OPT_CONST) {
42
    if (ii->opt[1] & OPT_CONST) return 0.;
43
    else return timing_table[ii->index].delayi;
44
  } else return timing_table[ii->index].delay;
45
}
46
 
47
/* Returns instruction size */
48
double insn_size (cuc_insn *ii)
49
{
50
  double s = (ii->opt[2] & OPT_CONST) ? timing_table[ii->index].sizei
51
          : timing_table[ii->index].size;
52
  if (ii->opt[1] & OPT_CONST) return 0.;
53
  if (ii->type & IT_COND && (ii->index == II_CMOV || ii->index == II_ADD)) return s / 32.;
54
  else return s;
55
}
56
 
57
/* Returns normal instruction size */
58
double ii_size (int index, int imm)
59
{
60
  if (imm) return timing_table[index].sizei;
61
  else return timing_table[index].size;
62
}
63
 
64
/* Returns dataflow tree height in cycles */
65
static double max_delay (cuc_func *f, int b)
66
{
67
  double max_d = 0.;
68
  double *d;
69
  cuc_bb *bb = &f->bb[b];
70
  int i, j;
71
  d = (double *) malloc (sizeof (double) * bb->ninsn);
72
  for (i = 0; i < bb->ninsn; i++) {
73
    double md = 0.;
74
    for (j = 0; j < MAX_OPERANDS; j++) {
75
      int op = bb->insn[i].op[j];
76
      if (bb->insn[i].opt[j] & OPT_REF && op >= 0 && REF_BB (op) == b && REF_I (op) < i) {
77
        double t = d[REF_I (op)];
78
        if (t > md) md = t;
79
      }
80
    }
81
    d[i] = md + insn_time (&bb->insn[i]);
82
    if (d[i] > max_d) max_d = d[i];
83
  }
84
  free (d);
85
  //PRINTF ("max_d%i=%f\n", b, max_d);
86
  return max_d;
87
}
88
 
89
/* Calculates memory delay of a single run of a basic block */
90
static int memory_delay (cuc_func *f, int b)
91
{
92
  int i;
93
  int d = 0;
94
  for (i = 0; i < f->nmsched; i++)
95
    if (REF_BB (f->msched[i]) == b) {
96
      if (f->mtype[i] & MT_STORE) {
97
        if (!(f->mtype[i] & MT_BURST) || f->mtype[i] & MT_BURSTE) d += runtime.cuc.mdelay[2];
98
        else d += runtime.cuc.mdelay[3];
99
      } else if (f->mtype[i] & MT_LOAD) {
100
        if (!(f->mtype[i] & MT_BURST) || f->mtype[i] & MT_BURSTE) d += runtime.cuc.mdelay[0];
101
        else d += runtime.cuc.mdelay[1];
102
      }
103
    }
104
  //PRINTF ("md%i=%i\n", b, d);
105
  return d;
106
}
107
 
108
/* Cuts the tree and marks registers */
109
void cut_tree (cuc_func *f, int b, double sd)
110
{
111
  int i, j;
112
  double *depths;
113
  cuc_bb *bb = &f->bb[b];
114
  depths = (double *) malloc (sizeof (double) * bb->ninsn);
115
 
116
  for (i = 0; i < bb->ninsn; i++) {
117
    double md = 0.;
118
    int mg = 0;
119
    for (j = 0; j < MAX_OPERANDS; j++) {
120
      int op = bb->insn[i].op[j];
121
      if (bb->insn[i].opt[j] & OPT_REF && op >= 0 && REF_BB (op) == b && REF_I (op) < i) {
122
        double t = depths[REF_I (op)];
123
        if (f->INSN(op).type & IT_CUT) {
124
          if (f->INSN(op).tmp + 1 >= mg) {
125
            if (f->INSN(op).tmp + 1 > mg) md = 0.;
126
            mg = f->INSN(op).tmp + 1;
127
            if (t > md) md = t;
128
          }
129
        } else {
130
          if (f->INSN(op).tmp >= mg) {
131
            if (f->INSN(op).tmp > mg) md = 0.;
132
            mg = f->INSN(op).tmp;
133
            if (t > md) md = t;
134
          }
135
        }
136
      }
137
    }
138
    //PRINTF ("%2x md%.1f ", i, md);
139
    md += insn_time (&bb->insn[i]);
140
    //PRINTF ("md%.1f mg%i %.1f\n", md, mg, sd);
141
    bb->insn[i].tmp = mg;
142
    if (md > sd) {
143
      bb->insn[i].type |= IT_CUT;
144
      if (md > runtime.cuc.cycle_duration)
145
        log ("WARNING: operation t%x_%x may need to be registered inbetween\n", b, i);
146
      depths[i] = 0.;
147
    } else depths[i] = md;
148
  }
149
  free (depths);
150
}
151
 
152
/* How many cycles we need now to get through the BB */
153
static int new_bb_cycles (cuc_func *f, int b, int cut)
154
{
155
  long d;
156
  double x = max_delay (f, b);
157
  d = ceil (x / runtime.cuc.cycle_duration);
158
  if (d < 1) d = 1;
159
  if (cut && x > runtime.cuc.cycle_duration) cut_tree (f, b, x / d);
160
 
161
  if (x / d > max_bb_delay) max_bb_delay = x / d;
162
 
163
  return memory_delay (f, b) + d;
164
}
165
 
166
/* Cuts the tree and marks registers */
167
void mark_cut (cuc_func *f)
168
{
169
  int b, i;
170
  for (b = 0; b < f->num_bb; b++)
171
    for (i = 0; i < f->bb[b].ninsn; i++)
172
      f->bb[b].insn[i].tmp = 0; /* Set starting groups */
173
  if (config.cuc.no_multicycle)
174
    for (b = 0; b < f->num_bb; b++)
175
      new_bb_cycles (f, b, 1);
176
}
177
 
178
/* Returns basic block circuit area */
179
static double bb_size (cuc_bb *bb)
180
{
181
  int i;
182
  double d = 0.;
183
  for (i = 0; i < bb->ninsn; i++) {
184
    if (bb->insn[i].opt[2] & OPT_CONST)
185
      d = d + timing_table[bb->insn[i].index].sizei;
186
    else d = d + timing_table[bb->insn[i].index].size;
187
  }
188
  return d;
189
}
190
 
191
/* Recalculates bb[].cnt values, based on generated profile file */
192
void recalc_cnts (cuc_func *f, char *bb_filename)
193
{
194
  int i, r, b, prevbb = -1, prevcnt = 0;
195
  int buf[256];
196
  const int bufsize = 256;
197
  FILE *fi = fopen (bb_filename, "rb");
198
 
199
  assert (fi);
200
 
201
  /* initialize counts */
202
  for (b = 0; b < f->num_bb; b++) f->bb[b].cnt = 0;
203
 
204
  /* read control flow from file and set counts */
205
  do {
206
    r = fread (buf, sizeof (int), bufsize, fi);
207
    for (i = 0; i < r; i++) {
208
      b = f->init_bb_reloc[buf[i]];
209
      if (b < 0) continue;
210
      /* Were we in the loop? */
211
      if (b == prevbb) {
212
        prevcnt++;
213
      } else {
214
        /* End the block */
215
        if (prevbb >= 0 && prevbb != BBID_START)
216
          f->bb[prevbb].cnt += prevcnt / f->bb[prevbb].unrolled + 1;
217
        prevcnt = 0;
218
        prevbb = b;
219
      }
220
    }
221
  } while (r == bufsize);
222
 
223
  fclose (fi);
224
}
225
 
226
/* Analizes current version of design and places results into timings structure */
227
void analyse_timings (cuc_func *f, cuc_timings *timings)
228
{
229
  long new_time = 0;
230
  double size = 0.;
231
  int b, i;
232
 
233
  /* Add time needed for mtspr/mfspr */
234
  for (i = 0; i < MAX_REGS; i++) if (f->used_regs[i]) new_time++;
235
  new_time++; /* always one mfspr at the end */
236
  new_time *= f->num_runs;
237
 
238
  max_bb_delay = 0.;
239
  for (b = 0; b < f->num_bb; b++) {
240
    new_time += new_bb_cycles (f, b, 0) * f->bb[b].cnt;
241
    size = size + bb_size (&f->bb[b]);
242
  }
243
  timings->new_time = new_time;
244
  timings->size = size;
245
  log ("Max circuit delay %.2fns; max circuit clock speed %.1fMHz\n",
246
                  max_bb_delay, 1000. / max_bb_delay);
247
}
248
 
249
/* Loads in the specified timings table */
250
void load_timing_table (char *filename)
251
{
252
  int i;
253
  FILE *fi;
254
 
255
  log ("Loading timings from %s\n", filename);
256
  log ("Using clock delay %.2fns (frequency %.0fMHz)\n", runtime.cuc.cycle_duration,
257
                 1000. / runtime.cuc.cycle_duration);
258
  assert (fi = fopen (filename, "rt"));
259
 
260
  timing_table = (cuc_timing_table *)malloc ((II_LAST + 1) * sizeof (cuc_timing_table));
261
  assert (timing_table);
262
  for (i = 0; i <= II_LAST; i++) {
263
    timing_table[i].size = -1.;
264
    timing_table[i].sizei = -1.;
265
    timing_table[i].delay = -1.;
266
    timing_table[i].delayi = -1.;
267
  }
268
 
269
  while (!feof(fi)) {
270
    char tmp[256];
271 82 jeremybenn
    int index = 0;
272 19 jeremybenn
    if (fscanf (fi, "%s", tmp) != 1) break;
273
    if (tmp[0] == '#') {
274
      while (!feof (fi) && fgetc (fi) != '\n');
275
      continue;
276
    }
277
    for (i = 0; i <= II_LAST; i++)
278
      if (strcmp (known[i].name, tmp) == 0) {
279
        index = i;
280
        break;
281
      }
282
    assert (index <= II_LAST);
283
    i = index;
284
    if (fscanf (fi, "%lf%lf%lf%lf\n", &timing_table[i].size,
285
                &timing_table[i].sizei, &timing_table[i].delay, &timing_table[i].delayi) != 4) break;
286
    /*PRINTF ("!%s size %f,%f delay %f,%f\n", known[i].name, timing_table[i].size,
287
                    timing_table[i].sizei, timing_table[i].delay, timing_table[i].delayi);*/
288
  }
289
 
290
  /* Was everything initialized? */
291
  for (i = 0; i <= II_LAST; i++) {
292
    assert (timing_table[i].size >= 0 && timing_table[i].sizei >= 0
293
     && timing_table[i].delay >= 0 && timing_table[i].delayi >= 0);
294
    /*PRINTF ("%s size %f,%f delay %f,%f\n", known[i], timing_table[i].size,
295
                    timing_table[i].sizei, timing_table[i].delay, timing_table[i].delayi);*/
296
  }
297
 
298
  fclose (fi);
299
}
300
 

powered by: WebSVN 2.1.0

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