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 930

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

powered by: WebSVN 2.1.0

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