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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [cuc/] [memory.c] - Blame information for rev 941

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

Line No. Rev Author Line
1 879 markom
/* memory.c -- OpenRISC Custom Unit Compiler, memory optimization and scheduling
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 897 markom
#include "sim-config.h"
25 879 markom
#include "cuc.h"
26
#include "insn.h"
27
 
28 941 markom
 
29
/* Cleans memory & data dependencies */
30
void clean_deps (cuc_func *f)
31
{
32
  int b, i;
33
  dep_list *t;
34
  for (b = 0; b < f->num_bb; b++) {
35
    for (i = 0; i < f->bb[b].ninsn; i++) {
36
      t = f->bb[b].insn[i].dep;
37
      while (t) {
38
        dep_list *tmp = t;
39
        t = t->next;
40
        free (tmp);
41
      }
42
      f->bb[b].insn[i].dep = NULL;
43
    }
44
 
45
    t = f->bb[b].mdep;
46
    while (t) {
47
      dep_list *tmp = t;
48
      t = t->next;
49
      free (tmp);
50
    }
51
    f->bb[b].mdep = NULL;
52
  }
53
 
54
  f->nmsched = 0;
55
}
56
 
57 879 markom
/* Checks for memory conflicts between two instructions; returns 1 if detected
58
 
59
static int check_memory_conflict (cuc_func *f, cuc_insn *a, cuc_insn *b, int otype)
60
{
61
  switch (otype) {
62 897 markom
    case MO_EXACT: /* exact */
63
    case MO_STRONG: /* strong */
64 879 markom
      return 1;
65 897 markom
    case MO_WEAK: /* weak */
66 879 markom
      assert (a->type & IT_MEMORY);
67
      assert (b->type & IT_MEMORY);
68
      if ((a->opt[1] & OPT_REF) && f->INSN(a->op[1]).index == II_ADD
69
        &&(b->opt[1] & OPT_REF) && f->INSN(b->op[1]).index == II_ADD) {
70
        int aw, bw;
71
        assert ((aw = II_MEM_WIDTH (a->index)) >= 0);
72
        assert ((bw = II_MEM_WIDTH (b->index)) >= 0);
73
 
74
        a = &f->INSN(a->op[1]);
75
        b = &f->INSN(b->op[1]);
76
        if (a->opt[1] != b->opt[1] || a->op[1] != b->op[1]
77
         || a->opt[2] != OPT_CONST || b->opt[2] != OPT_CONST) return 1;
78
 
79
        /* Check if they overlap */
80
        if (a->op[2] >= b->op[2] && a->op[2] < b->op[2] + bw) return 1;
81
        if (b->op[2] >= a->op[2] && b->op[2] < a->op[2] + aw) return 1;
82
        return 0;
83
      } else return 1;
84 897 markom
    case MO_NONE: /* none */
85 879 markom
      return 0;
86
    default:
87
      assert (0);
88
  }
89
  return 1;
90
}
91
 
92
/* Adds memory dependencies based on ordering type:
93
 
94
void add_memory_dep (cuc_func *f, int otype)
95
{
96
  int b, i;
97
  dep_list *all_mem = NULL;
98
 
99
  for (b = 0; b < f->num_bb; b++) {
100
    cuc_insn *insn = f->bb[b].insn;
101
    for (i = 0; i < f->bb[b].ninsn; i++)
102
      if (insn[i].type & IT_MEMORY) {
103
        dep_list *tmp = all_mem;
104
        while (tmp) {
105
          //printf ("%x %x\n", REF (b,i), tmp->ref);
106
          if (check_memory_conflict (f, &insn[i], &f->INSN(tmp->ref), otype))
107
            add_dep (&insn[i].dep, tmp->ref);
108
          tmp = tmp->next;
109
        }
110
        add_dep (&all_mem, REF (b, i));
111
      }
112
  }
113
  dispose_list (&all_mem);
114
}
115
 
116
/* returns nonzero if a < b */
117
int mem_ordering_cmp (cuc_func *f, cuc_insn *a, cuc_insn *b)
118
{
119
  assert (a->type & IT_MEMORY);
120
  assert (b->type & IT_MEMORY);
121
  if ((a->opt[1] & OPT_REF) && f->INSN(a->op[1]).index == II_ADD
122
    &&(b->opt[1] & OPT_REF) && f->INSN(b->op[1]).index == II_ADD) {
123
    a = &f->INSN(a->op[1]);
124
    b = &f->INSN(b->op[1]);
125
    if (a->opt[1] != b->opt[1] || a->op[1] != b->op[1]
126
     || a->opt[2] != OPT_CONST || b->opt[2] != OPT_CONST) return 0;
127
 
128
    /* Order linearly, we can then join them to bursts */
129
    return a->op[2] < b->op[2];
130
  } else return 0;
131
}
132
 
133
/* Schedule memory accesses
134
 
135
void schedule_memory (cuc_func *f, int otype)
136
{
137
  int b, i, j;
138
  f->nmsched = 0;
139
 
140
  for (b = 0; b < f->num_bb; b++) {
141
    cuc_insn *insn = f->bb[b].insn;
142
    for (i = 0; i < f->bb[b].ninsn; i++)
143
      if (insn[i].type & IT_MEMORY) {
144
        f->msched[f->nmsched++] = REF (b, i);
145 897 markom
        if (otype == MO_NONE || otype == MO_WEAK) insn[i].type |= IT_FLAG1; /* mark unscheduled */
146 879 markom
      }
147
  }
148 937 markom
 
149 879 markom
  for (i = 0; i < f->nmsched; i++)
150 937 markom
    cucdebug (2, "[%x]%x%c ", f->msched[i], f->mtype[i] & MT_WIDTH, (f->mtype[i] & MT_BURST) ? (f->mtype[i] & MT_BURSTE) ? 'E' : 'B' : ' ');
151
  cucdebug (2, "\n");
152
 
153 879 markom
  /* We can reorder just more loose types
154
     We assume, that memory accesses are currently in valid (but not neccesserly)
155
     optimal order */
156 897 markom
  if (otype == MO_WEAK || otype == MO_NONE) {
157 879 markom
    for (i = 0; i < f->nmsched; i++) {
158
      int best = i;
159
      int tmp;
160
      for (j = i + 1; j < f->nmsched; j++) if (REF_BB(f->msched[j]) == REF_BB(f->msched[best])) {
161
        if (mem_ordering_cmp (f, &f->INSN (f->msched[j]), &f->INSN(f->msched[best]))) {
162
          /* Check dependencies */
163
          dep_list *t = f->INSN(f->msched[j]).dep;
164
          while (t) {
165
            if (f->INSN(t->ref).type & IT_FLAG1) break;
166
            t = t->next;
167
          }
168
          if (!t) best = j; /* no conflicts -> ok */
169
        }
170
      }
171
 
172
      /* we have to shift instructions up, to maintain valid dependencies
173
         and make space for best candidate */
174
 
175
      /* make local copy */
176
      tmp = f->msched[best];
177
      for (j = best; j > i; j--) f->msched[j] = f->msched[j - 1];
178
      f->msched[i] = tmp;
179
      f->INSN(f->msched[i]).type &= ~IT_FLAG1; /* mark scheduled */
180
    }
181
  }
182
 
183
  for (i = 0; i < f->nmsched; i++)
184 937 markom
    cucdebug (2, "[%x]%x%c ", f->msched[i], f->mtype[i] & MT_WIDTH, (f->mtype[i] & MT_BURST) ? (f->mtype[i] & MT_BURSTE) ? 'E' : 'B' : ' ');
185
  cucdebug (2, "\n");
186 879 markom
 
187 904 markom
  /* Assign memory types */
188 879 markom
  for (i = 0; i < f->nmsched; i++) {
189
    cuc_insn *a = &f->INSN(f->msched[i]);
190 907 markom
    f->mtype[i] = !II_IS_LOAD(a->index) ? MT_STORE : MT_LOAD;
191 879 markom
    f->mtype[i] |= II_MEM_WIDTH (a->index);
192
    if (a->type & IT_SIGNED) f->mtype[i] |= MT_SIGNED;
193
  }
194
 
195 904 markom
  /* Check if they address the same location, so we can join them */
196
  if (otype == MO_WEAK || otype == MO_NONE) {
197
    for (i = 1, j = 1; i < f->nmsched; i++)
198
      /* Exclude memory stores and different memory types */
199 907 markom
      if (f->mtype[i - 1] == f->mtype[i] && f->mtype[i] & MT_LOAD) {
200 904 markom
        cuc_insn *a = &f->INSN(f->msched[i - 1]);
201
        cuc_insn *b = &f->INSN(f->msched[i]);
202
        if ((a->opt[1] & OPT_REF) && f->INSN(a->op[1]).index == II_ADD
203
          &&(b->opt[1] & OPT_REF) && f->INSN(b->op[1]).index == II_ADD) {
204
          a = &f->INSN(a->op[1]);
205
          b = &f->INSN(b->op[1]);
206
          /* Not in usual form? */
207
          if (a->opt[1] != b->opt[1] || a->op[1] != b->op[1]
208
           || a->opt[2] != OPT_CONST || b->opt[2] != OPT_CONST) goto keep;
209
 
210
          //printf ("%i %i, ", a->op[2], b->op[2]);
211
 
212
          /* Check if they are the same => do not copy */
213
          if (a->op[2] == b->op[2]
214
            && REF_BB(f->msched[i - 1]) == REF_BB(f->msched[i])) {
215
            /* yes => remove actual instruction */
216
            int t1 = MIN (f->msched[i - 1], f->msched[i]);
217
            int t2 = MAX (f->msched[i - 1], f->msched[i]);
218
            int b, i, j;
219
            cucdebug (2, "Removing %x_%x and using %x_%x instead.\n",
220
              REF_BB(t2), REF_I(t2), REF_BB(t1), REF_I(t1));
221
            change_insn_type (&f->INSN(t2), II_NOP);
222
            /* Update references */
223
            for (b = 0; b < f->num_bb; b++)
224
              for (i = 0; i < f->bb[b].ninsn; i++)
225
                for (j = 0; j < MAX_OPERANDS; j++)
226
                  if (f->bb[b].insn[i].opt[j] & OPT_REF && f->bb[b].insn[i].op[j] == t2)
227
                    f->bb[b].insn[i].op[j] = t1;
228
 
229
          } else goto keep;
230 937 markom
        } else goto keep;
231 904 markom
      } else {
232
keep:
233
        f->msched[j] = f->msched[i];
234
        f->mtype[j++] = f->mtype[i];
235
      }
236
    f->nmsched = j;
237
  }
238
 
239 937 markom
  for (i = 0; i < f->nmsched; i++)
240
    cucdebug (2, "[%x]%x%c ", f->msched[i], f->mtype[i] & MT_WIDTH, (f->mtype[i] & MT_BURST) ? (f->mtype[i] & MT_BURSTE) ? 'E' : 'B' : ' ');
241
  cucdebug (2, "\n");
242
  if (cuc_debug > 5) print_cuc_bb (f, "AFTER_MEM_REMOVAL");
243
 
244 897 markom
  if (config.cuc.enable_bursts) {
245 879 markom
    //printf ("\n");
246
    for (i = 1; i < f->nmsched; i++) {
247
      cuc_insn *a = &f->INSN(f->msched[i - 1]);
248
      cuc_insn *b = &f->INSN(f->msched[i]);
249
      int aw = f->mtype[i - 1] & MT_WIDTH;
250
 
251
      if ((a->opt[1] & OPT_REF) && f->INSN(a->op[1]).index == II_ADD
252
        &&(b->opt[1] & OPT_REF) && f->INSN(b->op[1]).index == II_ADD) {
253
        a = &f->INSN(a->op[1]);
254
        b = &f->INSN(b->op[1]);
255
        /* Not in usual form? */
256
        if (a->opt[1] != b->opt[1] || a->op[1] != b->op[1]
257
         || a->opt[2] != OPT_CONST || b->opt[2] != OPT_CONST) continue;
258
 
259
        //printf ("%i %i, ", a->op[2], b->op[2]);
260
 
261
        /* Check if they touch together */
262
        if (a->op[2] + aw == b->op[2]) {
263
          /* yes => do burst */
264
          f->mtype[i - 1] &= ~MT_BURSTE;
265
          f->mtype[i - 1] |= MT_BURST;
266
          f->mtype[i] |= MT_BURST | MT_BURSTE;
267
        }
268
      }
269
    }
270
  }
271
 
272
  for (i = 0; i < f->nmsched; i++)
273 937 markom
    cucdebug (2, "[%x]%x%c ", f->msched[i], f->mtype[i] & MT_WIDTH, (f->mtype[i] & MT_BURST) ? (f->mtype[i] & MT_BURSTE) ? 'E' : 'B' : ' ');
274
  cucdebug (2, "\n");
275 879 markom
 
276
  /* We don't need dependencies in non-memory instructions */
277
  for (b = 0; b < f->num_bb; b++) {
278
    cuc_insn *insn = f->bb[b].insn;
279
    for (i = 0; i < f->bb[b].ninsn; i++) if (!(insn[i].type & IT_MEMORY))
280
      dispose_list (&insn[i].dep);
281
  }
282
 
283
  /* Reduce number of dependecies, keeping just direct dependencies, based on memory schedule */
284
  {
285 907 markom
    int lastl[3] = {-1, -1, -1};
286
    int lasts[3] = {-1, -1, -1};
287
    int lastc[3] = {-1, -1, -1};
288
    int last_load = -1, last_store = -1, last_call = -1;
289 879 markom
    for (i = 0; i < f->nmsched; i++) {
290 907 markom
      int t = f->mtype[i] & MT_LOAD ? 0 : f->mtype[i] & MT_STORE ? 1 : 2;
291 879 markom
      int maxl = lastl[t];
292
      int maxs = lasts[t];
293 907 markom
      int maxc = lastc[t];
294 879 markom
      dep_list *tmp = f->INSN(f->msched[i]).dep;
295
      while (tmp) {
296
        if (f->INSN(tmp->ref).type & IT_MEMORY && REF_BB(tmp->ref) == REF_BB(f->msched[i])) {
297 937 markom
          printf ("%i %x %x\n", i, f->msched[i], tmp->ref);
298 879 markom
          /* Search for the reference */
299
          for (j = 0; j < f->nmsched; j++) if (f->msched[j] == tmp->ref) break;
300
          assert (j < f->nmsched);
301 907 markom
          if (f->mtype[j] & MT_STORE) {
302 879 markom
            if (maxs < j) maxs = j;
303 907 markom
          } else if (f->mtype[j] & MT_LOAD) {
304 879 markom
            if (maxl < j) maxl = j;
305 907 markom
          } else if (f->mtype[j] & MT_CALL) {
306
            if (maxc < j) maxc = j;
307 879 markom
          }
308
        }
309
        tmp = tmp->next;
310
      }
311
      dispose_list (&f->INSN(f->msched[i]).dep);
312 907 markom
      if (f->mtype[i] & MT_STORE) {
313 879 markom
        maxs = last_store;
314
        last_store = i;
315 907 markom
      } else if (f->mtype[i] & MT_LOAD) {
316 879 markom
        maxl = last_load;
317
        last_load = i;
318 907 markom
      } else if (f->mtype[i] & MT_CALL) {
319
        maxc = last_call;
320
        last_call = i;
321 879 markom
      }
322
 
323
      if (maxl > lastl[t]) {
324
        add_dep (&f->INSN(f->msched[i]).dep, f->msched[maxl]);
325
        lastl[t] = maxl;
326
      }
327
      if (maxs > lasts[t]) {
328
        add_dep (&f->INSN(f->msched[i]).dep, f->msched[maxs]);
329
        lasts[t] = maxs;
330
      }
331 907 markom
      if (maxc > lastc[t]) {
332
        add_dep (&f->INSN(f->msched[i]).dep, f->msched[maxc]);
333
        lastc[t] = maxc;
334
      }
335 879 markom
      //printf ("%i(%i)> ml %i(%i) ms %i(%i) lastl %i %i lasts %i %i last_load %i last_store %i\n", i, f->msched[i], maxl, f->msched[maxl], maxs, f->msched[maxs], lastl[0], lastl[1], lasts[0], lasts[1], last_load, last_store);
336
 
337
      /* What we have to wait to finish this BB? */
338
      if (i + 1 >= f->nmsched || REF_BB(f->msched[i + 1]) != REF_BB(f->msched[i])) {
339
        if (last_load > lastl[t]) {
340
          add_dep (&f->bb[REF_BB(f->msched[i])].mdep, f->msched[last_load]);
341
          lastl[t] = last_load;
342
        }
343
        if (last_store > lasts[t]) {
344
          add_dep (&f->bb[REF_BB(f->msched[i])].mdep, f->msched[last_store]);
345
          lasts[t] = last_store;
346
        }
347 907 markom
        if (last_call > lastc[t]) {
348
          add_dep (&f->bb[REF_BB(f->msched[i])].mdep, f->msched[last_call]);
349
          lastc[t] = last_call;
350
        }
351 879 markom
      }
352
    }
353
  }
354
}

powered by: WebSVN 2.1.0

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