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 897

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

powered by: WebSVN 2.1.0

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