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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [cuc/] [memory.c] - Blame information for rev 937

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

powered by: WebSVN 2.1.0

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