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 1308

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

powered by: WebSVN 2.1.0

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