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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_65/] [or1ksim/] [cuc/] [bb.c] - Blame information for rev 915

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

Line No. Rev Author Line
1 879 markom
/* bb.c -- OpenRISC Custom Unit Compiler, Basic Block handling
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
#include "abstract.h"
26 879 markom
#include "cuc.h"
27
#include "insn.h"
28
#include "support/profile.h"
29
 
30
/* Print out basic blocks */
31
void print_cuc_bb (cuc_func *f, char *s)
32
{
33
  int i;
34
  printf ("------- %s -------\n", s);
35
  for (i = 0; i < f->num_bb; i++) {
36
    if (f->bb[i].insn) printf ("\n---- BB%-2x * %x ---- ", i, f->bb[i].cnt);
37
    else printf ("BB%-2x: %4x-%-4x", i, f->bb[i].first, f->bb[i].last);
38
    printf (" type %02x tmp %i ", f->bb[i].type, f->bb[i].tmp);
39
    if (f->bb[i].next[0] >= 0) printf ("next %2x ", f->bb[i].next[0]);
40
    else printf ("next * ");
41
    if (f->bb[i].next[1] >= 0) printf ("%2x ", f->bb[i].next[1]);
42
    else printf ("* ");
43
    if (f->bb[i].prev[0] >= 0) printf ("prev %2x ", f->bb[i].prev[0]);
44
    else printf ("prev * ");
45
    if (f->bb[i].prev[1] >= 0) printf ("%2x\n", f->bb[i].prev[1]);
46
    else printf ("*\n");
47
 
48
    if (f->bb[i].insn) print_insns (f->bb[i].insn, f->bb[i].ninsn, 0);
49
  }
50
  printf ("\n");
51 897 markom
  fflush (stdout);
52 879 markom
}
53
 
54
/* Copies src basic block into destination */
55
cuc_bb *cpy_bb (cuc_bb *dest, cuc_bb *src)
56
{
57 897 markom
  int i, j;
58
  assert (dest != src);
59 879 markom
  *dest = *src;
60
  assert (dest->insn = malloc (sizeof (cuc_insn) * src->ninsn));
61
  for (i = 0; i < src->ninsn; i++)
62
    dest->insn[i] = src->insn[i];
63
  if (src->ntim) {
64
    assert (dest->tim = malloc (sizeof (cuc_timings) * src->ntim));
65 897 markom
    for (i = 0; i < src->ntim; i++) {
66
      dest->tim[i] = src->tim[i];
67
      if (src->tim[i].nshared) {
68
        assert (dest->tim[i].shared = malloc (sizeof (int) * src->tim[i].nshared));
69
        for (j = 0; j < src->tim[i].nshared; j++)
70
          dest->tim[i].shared[j] = src->tim[i].shared[j];
71
      }
72
    }
73 879 markom
  }
74
}
75
 
76
/* Duplicates function */
77
cuc_func *dup_func (cuc_func *f)
78
{
79
  cuc_func *n = (cuc_func *) malloc (sizeof (cuc_func));
80
  int b, i;
81
  for (b = 0; b < f->num_bb; b++) cpy_bb (&n->bb[b], &f->bb[b]);
82
  n->num_bb = f->num_bb;
83
  assert (n->init_bb_reloc = (int *)malloc (sizeof (int) * f->num_init_bb));
84
  for (b = 0; b < f->num_init_bb; b++) n->init_bb_reloc[b] = f->init_bb_reloc[b];
85
  n->num_init_bb = f->num_init_bb;
86 915 markom
  for (i = 0; i < MAX_REGS; i++) {
87
    n->saved_regs[i] = f->saved_regs[i];
88
    n->lur[i] = f->lur[i];
89
    n->used_regs[i] = f->used_regs[i];
90
  }
91 879 markom
  n->start_addr = f->start_addr;
92
  n->end_addr = f->end_addr;
93
  n->orig_time = f->orig_time;
94
  n->nmsched = f->nmsched;
95
  for (i = 0; i < f->nmsched; i++) {
96
    n->msched[i] = f->msched[i];
97
    n->mtype[i] = f->mtype[i];
98
  }
99 906 markom
  n->nfdeps = f->nfdeps;
100
  if (f->nfdeps) {
101
    f->fdeps = (cuc_func **) malloc (sizeof (cuc_func *) * f->nfdeps);
102
    for (i = 0; i < f->nfdeps; i++) n->fdeps[i] = f->fdeps[i];
103
  }
104 879 markom
  return n;
105
}
106
 
107
/* Releases memory allocated by function */
108
void free_func (cuc_func *f)
109
{
110
  int b, i;
111
  for (b = 0; b < f->num_bb; b++) {
112
    for (i = 0; i < f->bb[b].ninsn; i++)
113
      dispose_list (&f->bb[b].insn[i].dep);
114
    if (f->bb[b].insn) free (f->bb[b].insn);
115 897 markom
    for (i = 0; i < f->bb[b].ntim; i++)
116
      if (f->bb[b].tim[i].nshared && f->bb[b].tim[i].shared)
117
        free (f->bb[b].tim[i].shared);
118 879 markom
    if (f->bb[b].tim && f->bb[b].ntim) free (f->bb[b].tim);
119
  }
120
  free (f);
121
}
122
 
123
 
124
/* Recalculates last_used_reg */
125
void recalc_last_used_reg (cuc_func *f, int b)
126
{
127
  int i;
128
  cuc_bb *bb = &f->bb[b];
129
 
130
  /* rebuild last used reg array */
131
  if (bb->insn[0].index == II_LRBB) bb->last_used_reg[LRBB_REG] = 0;
132
  else bb->last_used_reg[LRBB_REG] = -1;
133
 
134
  for (i = 1; i < MAX_REGS - 1; i++) bb->last_used_reg[i] = -1;
135
 
136
    /* Create references */
137
  for (i = 0; i < bb->ninsn; i++) {
138
    int k;
139
    /* Now check for destination operand(s) */
140
    for (k = 0; k < MAX_OPERANDS; k++) if (bb->insn[i].opt[k] & OPT_DEST)
141
      if ((bb->insn[i].opt[k] & ~OPT_DEST) == OPT_REGISTER
142
        && (int)bb->insn[i].op[k] >= 0) {
143
        bb->last_used_reg[bb->insn[i].op[k]] = REF (b, i);
144
      }
145
  }
146
}
147
 
148
/* Set the BB limits */
149
void detect_bb (cuc_func *f)
150
{
151
  int i, j, end_bb = 0, eb = 0;
152
 
153
  /* Mark block starts/ends */
154
  for (i = 0; i < num_insn; i++) {
155
    if (end_bb) insn[i].type |= IT_BBSTART;
156
    end_bb = 0;
157
    if (insn[i].type & IT_BRANCH) {
158
      int jt = insn[i].op[0];
159
      insn[i].type |= IT_BBEND;
160
      end_bb = 1;
161
      if (jt < 0 || jt >= num_insn) {
162
        fprintf (stderr, "Instruction #%i:Jump out of function '%s'.\n", i, insn[i].disasm);
163
        exit (1);
164
      }
165
      if (jt > 0) insn[jt - 1].type |= IT_BBEND;
166
      insn[jt].type |= IT_BBSTART;
167
    }
168
  }
169
 
170
  /* Initialize bb array */
171
  insn[0].type |= IT_BBSTART;
172
  insn[num_insn - 1].type |= IT_BBEND;
173
  f->num_bb = 0;
174
  for (i = 0; i < num_insn; i++) {
175
    if (insn[i].type & IT_BBSTART) {
176
      f->bb[f->num_bb].first = i;
177
      f->bb[f->num_bb].cnt = 0;
178
    }
179
    /* Determine repetitions of a loop */
180
    if (insn[i].type & IT_BBEND) {
181
      f->bb[f->num_bb].type = 0;
182
      f->bb[f->num_bb].last = i;
183
      f->bb[f->num_bb].next[0] = f->bb[f->num_bb].next[1] = -1;
184
      f->bb[f->num_bb].tmp = 0;
185
      f->bb[f->num_bb].ntim = 0;
186
      f->num_bb++;
187
      assert (f->num_bb < MAX_BB);
188
    }
189
  }
190 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "AFTER_INIT");
191 879 markom
 
192
  /* Build forward connections between BBs */
193
  for (i = 0; i < f->num_bb; i++)
194
    if (insn[f->bb[i].last].type & IT_BRANCH) {
195
      int j;
196
      assert (insn[f->bb[i].last].index == II_BF);
197
      /* Find block this instruction jumps to */
198
      for (j = 0; j < f->num_bb; j++)
199
        if (f->bb[j].first == insn[f->bb[i].last].op[0]) break;
200
      assert (j < f->num_bb);
201
 
202
      /* Convert the jump address to BB link */
203
      insn[f->bb[i].last].op[0] = j; insn[f->bb[i].last].opt[0] = OPT_BB;
204
 
205
      /* Make a link */
206
      f->bb[i].next[0] = j;
207
      if (++f->bb[j].tmp > 2) eb++;
208
      f->bb[i].next[1] = i + 1;
209
      if (++f->bb[i + 1].tmp > 2) eb++;
210
    } else if (f->bb[i].last == num_insn - 1) { /* Last instruction doesn't have to do anything */
211
      f->bb[i].type |= BB_END;
212
    } else {
213
      f->bb[i].next[0] = i + 1;
214
      if (++f->bb[i + 1].tmp > 2) eb++;
215
    }
216
 
217 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "AFTER_NEXT");
218 879 markom
 
219
  /* Build backward connections, but first insert artificial blocks
220
   * to handle more than 2 connections */
221 883 markom
  cucdebug (6, "artificial %i %i\n", f->num_bb, eb);
222 879 markom
  end_bb = f->num_bb + eb;
223
  for (i = f->num_bb - 1; i >= 0; i--) {
224
    j = f->bb[i].tmp;
225
    if (f->bb[i].tmp > 2) f->bb[i].tmp = -f->bb[i].tmp;
226
    f->bb[--end_bb] = f->bb[i];
227
    reloc[i] = end_bb;
228
    while (j-- > 2) {
229
      f->bb[--end_bb].first = f->bb[i].first;
230
      f->bb[end_bb].last = -1;
231
      f->bb[end_bb].next[0] = -1;
232
      f->bb[end_bb].next[1] = -1;
233
      f->bb[end_bb].tmp = 0;
234
      f->bb[end_bb].cnt = f->bb[i].cnt;
235
      f->bb[end_bb].ntim = 0;
236
    }
237
  }
238
  f->num_bb += eb;
239
 
240
  /* relocate jump instructions */
241
  for (i = 0; i < num_insn; i++)
242
    for (j = 0; j < MAX_OPERANDS; j++)
243
      if (insn[i].opt[j] & OPT_BB)
244
        insn[i].op[j] = reloc[insn[i].op[j]];
245 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "AFTER_INSERT-reloc");
246 879 markom
  for (i = 0; i < f->num_bb; i++) {
247
    if (f->bb[i].next[0] >= 0) {
248
      int t = reloc[f->bb[i].next[0]];
249
      if (f->bb[t].tmp < 0) {
250
        f->bb[t].tmp = -f->bb[t].tmp;
251
        t -= f->bb[t].tmp - 2;
252
      } else if (f->bb[t].tmp > 2) t -= f->bb[t].tmp-- - 2;
253
      f->bb[i].next[0] = t;
254
    }
255
    if (f->bb[i].next[1] >= 0) {
256
      int t = reloc[f->bb[i].next[1]];
257
      if (f->bb[t].tmp < 0) {
258
        f->bb[t].tmp = -f->bb[t].tmp;
259
        t -= f->bb[t].tmp - 2;
260
      } else if (f->bb[t].tmp > 2) t -= f->bb[t].tmp-- - 2;
261
      f->bb[i].next[1] = t;
262
    }
263
    /* artificial blocks do not have relocations, hardcode them */
264
    if (f->bb[i].last < 0) f->bb[i].next[0] = i + 1;
265
  }
266 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "AFTER_INSERT");
267 879 markom
 
268
  /* Uncoditional branched do not continue to next block */
269
  for (i = 0; i < f->num_bb; i++) {
270
    cuc_insn *ii;
271
    if (f->bb[i].last < 0) continue;
272
    ii = &insn[f->bb[i].last];
273
    /* Unconditional branch? */
274
    if (ii->type & IT_BRANCH && ii->opt[1] & OPT_CONST) {
275
      change_insn_type (ii, II_NOP);
276
      if (f->bb[i].next[1] == i + 1) f->bb[i].next[0] = f->bb[i].next[1];
277
      f->bb[i].next[1] = -1;
278
    }
279
  }
280 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "AFTER_UNCOND_JUMP");
281 879 markom
 
282
  /* Add backward connections */
283
  for (i = 0; i < f->num_bb; i++)
284
    f->bb[i].prev[0] = f->bb[i].prev[1] = -1;
285
 
286
  for (i = 0; i < f->num_bb; i++) {
287
    if (f->bb[i].next[0] >= 0) {
288
      int t = f->bb[i].next[0];
289
      if (f->bb[t].prev[0] < 0) f->bb[t].prev[0] = i;
290
      else {
291
        assert (f->bb[t].prev[1] < 0);
292
        f->bb[t].prev[1] = i;
293
      }
294
    }
295
    if (f->bb[i].next[1] >= 0) {
296
      int t = f->bb[i].next[1];
297
      if (f->bb[t].prev[0] < 0) f->bb[t].prev[0] = i;
298
      else {
299
        assert (f->bb[t].prev[1] < 0);
300
        f->bb[t].prev[1] = i;
301
      }
302
    }
303
  }
304 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "AFTER_PREV");
305 879 markom
}
306
 
307 905 markom
/* We do a quick check if there are some anomalies with references */
308
void cuc_check (cuc_func *f)
309
{
310
  int i, j, k;
311
  for (i = 0; i < f->num_bb; i++) {
312
    if (!f->bb[i].insn && f->bb[i].ninsn) {
313
      printf ("Anomaly detected at BB%x (insn NULL)\n", i);
314
      print_cuc_bb (f, "ANOMALY");
315
      exit (1);
316
    }
317
    for (j = 0; j < f->bb[i].ninsn; j++)
318
      for (k = 0; k < MAX_OPERANDS; k++)
319
        if (f->bb[i].insn[j].opt[k] & OPT_REF) {
320
          int t = f->bb[i].insn[j].op[k];
321
          if (REF_BB(t) >= f->num_bb || REF_I (t) >= f->bb[REF_BB(t)].ninsn) {
322
            printf ("Anomaly detected at %x.%x[%i]\n", i, j, k);
323
            print_cuc_bb (f, "ANOMALY");
324
            exit (1);
325
          }
326
        }
327
  }
328
}
329
 
330 879 markom
/* Build basic blocks */
331
void build_bb (cuc_func *f)
332
{
333
  int i, j, k;
334
  for (i = 0; i < f->num_bb; i++) {
335
    if (f->bb[i].last < 0) f->bb[i].ninsn = MAX_REGS - 1;
336
    else f->bb[i].ninsn = f->bb[i].last - f->bb[i].first + 1 + MAX_REGS - 1;
337
    assert (f->bb[i].ninsn >= MAX_REGS - 1);
338
    f->bb[i].insn = (cuc_insn *) malloc (sizeof (cuc_insn) * f->bb[i].ninsn);
339
    assert (f->bb[i].insn);
340
    f->bb[i].nmemory = 0;
341
    f->bb[i].unrolled = 1;
342
 
343
    /* Save space for conditional moves, exclude r0, place lrbb instead */
344
    change_insn_type (&f->bb[i].insn[0], II_LRBB);
345
    strcpy (f->bb[i].insn[0].disasm, "lrbb");
346
    f->bb[i].insn[0].type = IT_UNUSED;
347
    f->bb[i].insn[0].dep = NULL;
348
    f->bb[i].insn[0].op[0] = LRBB_REG; f->bb[i].insn[0].opt[0] = OPT_REGISTER | OPT_DEST;
349
    f->bb[i].insn[0].opt[1] = OPT_LRBB;
350
    f->bb[i].insn[0].opt[2] = f->bb[i].insn[0].opt[3] = OPT_NONE;
351
    for (j = 1; j < MAX_REGS - 1; j++) {
352
      change_insn_type (&f->bb[i].insn[j], II_CMOV);
353
      strcpy (f->bb[i].insn[j].disasm, "cmov");
354
      f->bb[i].insn[j].type = 0;
355
      f->bb[i].insn[j].dep = NULL;
356
      f->bb[i].insn[j].opt[0] = f->bb[i].insn[j].opt[1] = f->bb[i].insn[j].opt[2] = OPT_REGISTER;
357
      f->bb[i].insn[j].opt[0] |= OPT_DEST;
358
      f->bb[i].insn[j].op[0] = f->bb[i].insn[j].op[1] = f->bb[i].insn[j].op[2] = j;
359
      f->bb[i].insn[j].op[3] = LRBB_REG; f->bb[i].insn[j].opt[3] = OPT_REGISTER;
360
    }
361 897 markom
 
362
    /* Relocate instructions */
363 879 markom
    for (j = MAX_REGS - 1; j < f->bb[i].ninsn; j++) {
364
      f->bb[i].insn[j] = insn[f->bb[i].first + j - (MAX_REGS - 1)];
365
      for (k = 0; k < MAX_OPERANDS; k++)
366
        if (f->bb[i].insn[j].opt[k] & OPT_REF) {
367
          int b1;
368
          for (b1 = 0; b1 < i; b1++)
369 898 markom
            if (f->bb[b1].first <= (signed) f->bb[i].insn[j].op[k]
370
              && (signed)f->bb[i].insn[j].op[k] <= f->bb[b1].last) break;
371 879 markom
          assert (b1 < f->num_bb);
372
          f->bb[i].insn[j].op[k] = REF (b1, f->bb[i].insn[j].op[k] - f->bb[b1].first + MAX_REGS - 1);
373
        }
374
      if (f->bb[i].insn[j].type & IT_MEMORY) f->bb[i].nmemory++;
375
    }
376
  }
377 905 markom
  cuc_check (f);
378 879 markom
}
379
 
380
/* type == 0; keep predecessor condition
381
 * type == 1; keep successor condition
382
 * type == 2; join loop unrolled blocks */
383
static void join_bb (cuc_func *f, int pred, int succ, int type)
384
{
385 905 markom
  int i, j, k, n1, n2, ninsn, add_cond = 0;
386 879 markom
  unsigned long cond_op, cond_opt;
387
  cuc_insn *insn;
388
 
389 905 markom
  if (cuc_debug) cuc_check (f);
390 897 markom
  cucdebug (3, "%x <= %x+%x (%i)\n", pred, pred, succ, type);
391
  cucdebug (3, "%x %x\n", f->bb[pred].ninsn, f->bb[succ].ninsn);
392
  if (cuc_debug >= 3) fflush (stdout);
393 879 markom
 
394 905 markom
  n1 = f->bb[pred].ninsn;
395
  n2 = f->bb[succ].ninsn;
396
  if (n1 <= 0
397
   || !(f->bb[pred].insn[n1 - 1].type & IT_BRANCH)) type = 1;
398 879 markom
  if (type == 0 && f->bb[succ].prev[0] == f->bb[succ].next[0]) add_cond = 1;
399 905 markom
  if (type == 2) add_cond = 1;
400
 
401
  assert (f->bb[pred].next[0] == f->bb[succ].next[0] || type != 2); /* not supported */
402 879 markom
 
403 905 markom
  ninsn = n1 + n2 + (type == 1 ? 0 : 1) + (add_cond ? MAX_REGS : 0);
404 879 markom
 
405
  insn = (cuc_insn *) malloc (ninsn * sizeof (cuc_insn));
406 905 markom
  for (i = 0; i < n1; i++) insn[i] = f->bb[pred].insn[i];
407
  /* when type == 0, we move the last (jump) instruction to the end */
408 879 markom
  if (type == 0 || type == 2) {
409 905 markom
    /* Move first branch instruction to the end */
410
    assert (insn[n1 - 1].type & IT_BRANCH);
411
    insn[ninsn - 1] = insn[n1 - 1];
412
    cond_op = insn[n1 - 1].op[1];
413
    cond_opt = insn[n1 - 1].opt[1];
414
 
415
    /* Remove old branch */
416
    change_insn_type (&insn[n1 - 1], II_NOP);
417 879 markom
  }
418
  /* Copy second block */
419 905 markom
  for (i = 0; i < n2; i++) insn[i + n1] = f->bb[succ].insn[i];
420 902 markom
 
421
  /* and when type == 2, we may need to add sfor instruction, to quit when either is true */
422
  if (type == 2) {
423 905 markom
    /* Move second branch instruction to the end */
424
    if (insn[n1 + n2 - 1].type & IT_BRANCH) {
425
      insn[ninsn - 1] = insn[n1 + n2 - 1];
426
 
427
      /* Use conditional from cmov FLAG_REG, c_p, c_s, c_p */
428
      insn[ninsn - 1].op[1] = REF (pred, n1 + n2 + FLAG_REG); insn[ninsn - 1].opt[1] = OPT_REF;
429
 
430
      /* Remove old one */
431
      change_insn_type (&insn[n1 + n2 - 1], II_NOP);
432
    } else change_insn_type (&insn[ninsn - 1], II_NOP); /* do not use branch slot */
433 902 markom
  }
434
 
435 905 markom
#if 1
436 902 markom
  /* LRBB at start of succ BB is not valid, it should be set when */
437 905 markom
  if (insn[n1].index == II_LRBB) {
438 902 markom
    assert (0); /* not tested yet */
439 905 markom
    change_insn_type (&insn[n1], II_NOP);
440
    for (i = n1; i < ninsn; i++)
441
      if (insn[i].index == II_CMOV && insn[i].op[3] == REF (pred, n1)) {
442 902 markom
        assert (insn[i].opt[3] == OPT_REF);
443
        insn[i].op[3] = cond_op;
444
        insn[i].opt[3] = cond_opt;
445
        if (f->bb[pred].next[0] != succ) {
446
          unsigned long t; /* negate conditional -- exchange */
447
          assert (f->bb[pred].next[1] == succ);
448
          t = insn[i].op[1];
449
          insn[i].op[1] = insn[i].op[2];
450
          insn[i].op[2] = t;
451
          t = insn[i].opt[1];
452
          insn[i].opt[1] = insn[i].opt[2];
453
          insn[i].opt[2] = t;
454
        }
455
      }
456
  }
457 905 markom
#endif
458 879 markom
 
459
  for (i = 0; i < ninsn; i++) reloc[i] = -1;
460
 
461
  /* Add conditional instructions if required */
462
  if (add_cond) {
463
    recalc_last_used_reg (f, pred);
464
    recalc_last_used_reg (f, succ);
465
 
466
    /* r0 -- add nop for it */
467 905 markom
    change_insn_type (&insn[n1 + n2], II_NOP);
468 879 markom
    for (i = 1; i < MAX_REGS; i++) {
469 905 markom
      cuc_insn *ii = &insn[n1 + n2 + i];
470 879 markom
      int a = f->bb[pred].last_used_reg[i];
471
      int b = f->bb[succ].last_used_reg[i];
472
 
473
      if (b < 0) change_insn_type (ii, II_NOP);
474
      else if (a < 0) {
475
        change_insn_type (ii, II_ADD);
476
        ii->type = 0;
477
        ii->dep = NULL;
478
        ii->op[0] = i; ii->opt[0] = OPT_REGISTER | OPT_DEST;
479
        ii->op[1] = b; ii->opt[1] = OPT_REF;
480
        ii->op[2] = 0; ii->opt[2] = OPT_CONST;
481 905 markom
        ii->opt[3] = OPT_NONE;
482
        if (i == FLAG_REG) ii->type |= IT_COND;
483 879 markom
      } else if (b >= 0) {
484
        change_insn_type (ii, II_CMOV);
485
        ii->type = 0;
486
        ii->dep = NULL;
487
        ii->op[0] = i; ii->opt[0] = OPT_REGISTER | OPT_DEST;
488
        ii->op[1] = a; ii->opt[1] = OPT_REF;
489
        ii->op[2] = b; ii->opt[2] = OPT_REF;
490
        ii->op[3] = cond_op; ii->opt[3] = cond_opt;
491 905 markom
        reloc[REF_I(a)] = REF (pred, n1 + n2 + i);
492
        if (i == FLAG_REG) ii->type |= IT_COND;
493 879 markom
      }
494
      sprintf (ii->disasm, "cmov (join BB)");
495
    }
496
  }
497
 
498 905 markom
  if (cuc_debug) cuc_check (f);
499 879 markom
  f->bb[pred].type |= f->bb[succ].type;
500
  i = 0;
501
  assert (f->bb[pred].next[0] >= 0);
502
  switch (type) {
503
  case 0:
504
    if (f->bb[pred].next[0] == succ) f->bb[pred].next[0] = f->bb[succ].next[0];
505
    if (f->bb[pred].next[1] == succ) f->bb[pred].next[1] = f->bb[succ].next[0];
506
    assert (f->bb[succ].next[1] < 0);
507
    break;
508
  case 1:
509
    f->bb[pred].next[0] = f->bb[succ].next[0];
510
    f->bb[pred].next[1] = f->bb[succ].next[1];
511
    break;
512 905 markom
  case 2:
513
    f->bb[pred].next[0] = f->bb[succ].next[0];
514
    f->bb[pred].next[1] = f->bb[succ].next[1];
515
    break;
516 879 markom
  }
517
  if (f->bb[pred].next[0] == f->bb[pred].next[1]) f->bb[pred].next[1] = -1;
518
  f->bb[succ].type = BB_DEAD;
519 905 markom
  /* remove branch instruction, if there is only one successor */
520
  if (f->bb[pred].next[1] < 0 && insn[ninsn - 1].type & IT_BRANCH
521
    && f->bb[pred].next[0] != pred) /* not when end BB, loop */
522
    change_insn_type (&insn[ninsn - 1], II_NOP);
523 879 markom
 
524
  /* Set max count */
525
  if (f->bb[pred].cnt < f->bb[succ].cnt) f->bb[pred].cnt = f->bb[succ].cnt;
526
  f->bb[pred].ninsn = ninsn;
527 905 markom
  f->bb[succ].ninsn = 0;
528 879 markom
  free (f->bb[pred].insn); f->bb[pred].insn = NULL;
529
  free (f->bb[succ].insn); f->bb[succ].insn = NULL;
530
  f->bb[pred].insn = insn;
531
  for (i = 0; i < f->num_bb; i++) if (!(f->bb[i].type & BB_DEAD)) {
532
    if (f->bb[i].prev[0] == succ) f->bb[i].prev[0] = pred;
533
    if (f->bb[i].prev[1] == succ) f->bb[i].prev[1] = pred;
534
    if (f->bb[i].prev[0] == f->bb[i].prev[1]) f->bb[i].prev[1] = -1;
535
    for (j = 0; j < f->bb[i].ninsn; j++)
536
      for (k = 0; k < MAX_OPERANDS; k++)
537
        if (f->bb[i].insn[j].opt[k] & OPT_REF) {
538
          /* Check if we are referencing successor BB -> relocate to second part of
539
             the new block */
540
          if (REF_BB (f->bb[i].insn[j].op[k]) == succ) {
541 905 markom
            int t = f->bb[i].insn[j].op[k];
542
            int ndest = REF (pred, REF_I (t) + n1);
543
            //printf ("%x: %x %x\n", REF(i, j), t, ndest);
544 879 markom
 
545 905 markom
            /* We've found a reference to succ. block, being removed, relocate */
546
            f->bb[i].insn[j].op[k] = ndest;
547 879 markom
          } else if (REF_BB(f->bb[i].insn[j].op[k]) == pred) {
548
            if (i != pred && reloc[REF_I(f->bb[i].insn[j].op[k])] >= 0) {
549
              f->bb[i].insn[j].op[k] = reloc[REF_I(f->bb[i].insn[j].op[k])];
550
            }
551
          }
552
        }
553
  }
554
 
555 905 markom
  if (cuc_debug) cuc_check (f);
556 883 markom
  if (cuc_debug >= 3) print_cuc_bb (f, "join");
557 879 markom
}
558
 
559
/* Optimize basic blocks */
560
void optimize_bb (cuc_func *f)
561
{
562
  int i, j;
563
remove_lrbb:
564
  /* we can remove lrbb instructions from blocks with just one predecessor */
565
  for (i = 0; i < f->num_bb; i++) if (!(f->bb[i].type & BB_DEAD)) {
566
    if (f->bb[i].prev[0] >= 0 && f->bb[i].prev[1] < 0) { /* exactly one predecessor */
567
      for (j = 0; j < f->bb[i].ninsn; j++)
568
        if (f->bb[i].insn[j].index == II_LRBB) {
569
          cuc_insn *t;
570 883 markom
          cucdebug (4, "-lrbb %i.%i\n", i, j);
571 879 markom
 
572
          /* Change to add LRBB, 0, 0 */
573
          change_insn_type (&f->bb[i].insn[j], II_ADD);
574
          f->bb[i].insn[j].type &= ~IT_VOLATILE;
575
          t = &f->bb[f->bb[i].prev[0]].insn[f->bb[f->bb[i].prev[0]].ninsn - 1];
576
          f->bb[i].insn[j].opt[1] = f->bb[i].insn[j].opt[2] = OPT_CONST;
577
          f->bb[i].insn[j].op[1] = f->bb[i].insn[j].op[2] = 0; /* always use left block */
578
          f->bb[i].insn[j].opt[3] = OPT_NONE;
579
 
580
          /* If the predecessor still has a conditional jump instruction, we must be careful.
581
             This could only have occured when we found out that next[0] == next[1] and have
582
             joined them. Now we will link lrbb and correct the situation */
583
          if (t->type & IT_BRANCH) { /* We must set a reference to branch result */
584
            f->bb[i].insn[j].opt[1] = t->opt[1];
585
            f->bb[i].insn[j].op[1] = t->op[1];
586
            change_insn_type (t, II_NOP);
587
          }
588
        }
589
    }
590
  }
591
 
592
  /* Type 0 joining
593
     1. link between pred & succ
594
     2. no memory accesses in succ
595
     3. optional pred's second successors
596
     4. max. one succ's successors */
597
  for (i = 0; i < f->num_bb; i++) if (!(f->bb[i].type & BB_DEAD))
598
    if (f->bb[i].prev[0] >= 0 && f->bb[i].prev[1] < 0 /* one predecessor */
599
     && f->bb[i].next[1] < 0 /* max. one successor */
600
     && f->bb[i].nmemory == 0) {                  /* and no memory acceses */
601
      join_bb (f, f->bb[i].prev[0], i, 0);
602
      goto remove_lrbb;
603
    }
604
 
605
  /* Type 1 joining
606
     1. link between pred & succ
607
     2. no other pred's successors
608
     3. no other succ's predecessors */
609
  for (i = 0; i < f->num_bb; i++) if (!(f->bb[i].type & BB_DEAD))
610
    if (f->bb[i].prev[0] >= 0 && f->bb[i].prev[1] < 0 /* one predecessor */
611
     && f->bb[f->bb[i].prev[0]].next[0] >= 0 && f->bb[f->bb[i].prev[0]].next[1] < 0) { /* one successor */
612
      join_bb (f, f->bb[i].prev[0], i, 1);
613
      goto remove_lrbb;
614
    }
615
 
616
#if 1
617
  /* Type 2 joining
618
     1. link between pred & succ
619
     2. succ has exactly one predeccessor
620
     3. pred & succ share common successor
621
     4. optional succ's second successor */
622
  for (i = 0; i < f->num_bb; i++) if (!(f->bb[i].type & BB_DEAD))
623
    if (f->bb[i].prev[0] >= 0 && f->bb[i].prev[1] < 0) { /* one predecessor */
624
      int p = f->bb[i].prev[0];
625 905 markom
#if 0 /* not yet supported */
626
      if (f->bb[p].next[0] == i
627
       && (f->bb[i].next[1] == f->bb[p].next[1]
628
        || f->bb[i].next[1] == f->bb[p].next[0])) {
629
        join_bb (f, p, i, 2);
630 897 markom
        goto remove_lrbb;
631
      }
632 905 markom
#endif
633
      if (f->bb[p].next[1] == i
634
       && (f->bb[i].next[0] == f->bb[p].next[1]
635
        || f->bb[i].next[0] == f->bb[p].next[0])) {
636
        join_bb (f, p, i, 2);
637
        goto remove_lrbb;
638
      }
639 879 markom
    }
640
#endif
641
}
642
 
643
/* Removes BBs marked as dead */
644
void remove_dead_bb (cuc_func *f)
645
{
646
  int i, j, k, d = 0;
647
 
648
  for (i = 0; i < f->num_bb; i++) if (f->bb[i].type & BB_DEAD) {
649
    if (f->bb[i].insn) free (f->bb[i].insn);
650
    f->bb[i].insn = NULL;
651
    reloc[i] = -1;
652
  } else {
653
    reloc[i] = d;
654
    f->bb[d++] = f->bb[i];
655
  }
656
  f->num_bb = d;
657
 
658
  /* relocate initial blocks */
659
  for (i = 0; i < f->num_init_bb; i++)
660
    f->init_bb_reloc[i] = reloc[f->init_bb_reloc[i]];
661
 
662
  /* repair references */
663
  for (i = 0; i < f->num_bb; i++) if (!(f->bb[i].type & BB_DEAD)) {
664
    if (f->bb[i].prev[0] >= 0) assert ((f->bb[i].prev[0] = reloc[f->bb[i].prev[0]]) >= 0);
665
    if (f->bb[i].prev[1] >= 0) assert ((f->bb[i].prev[1] = reloc[f->bb[i].prev[1]]) >= 0);
666
    if (f->bb[i].next[0] >= 0) assert ((f->bb[i].next[0] = reloc[f->bb[i].next[0]]) >= 0);
667
    if (f->bb[i].next[1] >= 0) assert ((f->bb[i].next[1] = reloc[f->bb[i].next[1]]) >= 0);
668
    if (f->bb[i].prev[0] == f->bb[i].prev[1]) f->bb[i].prev[1] = -1;
669
    if (f->bb[i].next[0] == f->bb[i].next[1]) f->bb[i].next[1] = -1;
670
 
671
    for (j = 0; j < f->bb[i].ninsn; j++)
672
      for (k = 0; k < MAX_OPERANDS; k++)
673
        if (f->bb[i].insn[j].opt[k] & OPT_BB && (signed)f->bb[i].insn[j].op[k] >= 0)
674
          assert ((f->bb[i].insn[j].op[k] = reloc[f->bb[i].insn[j].op[k]]) >= 0);
675
        else if (f->bb[i].insn[j].opt[k] & OPT_REF) {
676
          int t = f->bb[i].insn[j].op[k];
677
          assert (reloc[REF_BB(t)] >= 0);
678
          f->bb[i].insn[j].op[k] = REF (reloc[REF_BB(t)], REF_I (t));
679
        }
680
  }
681
}
682
 
683
/* Recursive calculation of dependencies */
684
static int reg_dep_rec (cuc_func *f, int cur)
685
{
686
  int i, j;
687
  cuc_insn *insn = f->bb[cur].insn;
688
 
689
  //printf ("\n %i", cur); 
690
  /* Spread only, do not loop */
691
  if (f->bb[cur].tmp) return;
692
  f->bb[cur].tmp = 1;
693
  //printf ("!   ");
694
 
695
  for (i = 0; i < f->bb[cur].ninsn; i++) {
696
    /* Check for destination operand(s) */
697
    for (j = 0; j < MAX_OPERANDS; j++) if (insn[i].opt[j] & OPT_DEST)
698
      if ((insn[i].opt[j] & ~OPT_DEST) == OPT_REGISTER && (signed)insn[i].op[j] >= 0) {
699
        //printf ("%i:%i,%x ", insn[i].op[j], i, REF (cur, i));
700
        assert (insn[i].op[j] > 0 && insn[i].op[j] < MAX_REGS); /* r0 should never be dest */
701
        f->bb[cur].last_used_reg[insn[i].op[j]] = REF (cur, i);
702
      }
703
  }
704
 
705
  if (f->bb[cur].next[0] >= 0) reg_dep_rec (f, f->bb[cur].next[0]);
706
  if (f->bb[cur].next[1] >= 0) reg_dep_rec (f, f->bb[cur].next[1]);
707
}
708
 
709
/* Detect register dependencies */
710
void reg_dep (cuc_func *f)
711
{
712
  int i, b, c;
713
 
714
  /* Set dead blocks */
715
  for (b = 0; b < f->num_bb; b++) {
716
    f->bb[b].tmp = 0;
717
    for (i = 0; i < MAX_REGS; i++) f->bb[b].last_used_reg[i] = -1;
718
  }
719
 
720
  /* Start with first block and set dependecies of all reachable blocks */
721
  /* At the same time set last_used_regs */
722
  reg_dep_rec (f, 0);
723
 
724
  for (i = 0; i < f->num_bb; i++)
725
    if (f->bb[i].tmp) f->bb[i].tmp = 0;
726
    else f->bb[i].type |= BB_DEAD;
727
 
728
  /* Detect loops; mark BBs where loops must be broken */
729
  for (c = 0; c < f->num_bb; c++) {
730
    int min = 3, minb;
731
 
732
    /* search though all non-visited for minimum number of unvisited predecessors */
733
    for (b = 0; b < f->num_bb; b++) if (!f->bb[b].tmp) {
734
      int tmp = 0;
735
      if (f->bb[b].prev[0] >= 0 && !f->bb[f->bb[b].prev[0]].tmp) tmp++;
736
      if (f->bb[b].prev[1] >= 0 && !f->bb[f->bb[b].prev[1]].tmp) tmp++;
737
      if (tmp < min) {
738
        minb = b;
739
        min = tmp;
740
        if (tmp == 0) break; /* We already have the best one */
741
      }
742
    }
743
    b = minb;
744
    f->bb[b].tmp = 1; /* Mark visited */
745 883 markom
    cucdebug (3, "minb %i min %i\n", minb, min);
746 879 markom
    if (min) { /* We just broke the loop */
747
      f->bb[b].type |= BB_INLOOP;
748
    }
749
  }
750
 
751
  /* Set real predecessors in cmov instructions to previous blocks */
752
  for (b = 0; b < f->num_bb; b++)
753
    for (i = 1; i < MAX_REGS - 1; i++) {
754
      int pa, pb;
755
      assert (f->bb[b].insn[i].index ==  II_CMOV);
756
      assert (f->bb[b].insn[i].opt[0] == OPT_REGISTER | OPT_DEST);
757
      assert (f->bb[b].insn[i].op[0] == i);
758
      if (f->bb[b].prev[0] < 0) pa = -1;
759
      else pa = f->bb[f->bb[b].prev[0]].last_used_reg[i];
760
      if (f->bb[b].prev[1] < 0) pb = -1;
761
      else pb = f->bb[f->bb[b].prev[1]].last_used_reg[i];
762
 
763
      /* We do some very simple optimizations right away to make things more readable */
764
      if (pa < 0 && pb < 0) {
765
        /* Was not used at all */
766
        change_insn_type (&f->bb[b].insn[i], II_ADD);
767
        f->bb[b].insn[i].op[2] = 0; f->bb[b].insn[i].opt[2] = OPT_CONST;
768
        f->bb[b].insn[i].opt[3] = OPT_NONE;
769
      } else if (pa < 0) {
770
        change_insn_type (&f->bb[b].insn[i], II_ADD);
771
        assert (f->INSN(pb).opt[0] == (OPT_REGISTER | OPT_DEST));
772
        f->bb[b].insn[i].op[1] = pb; f->bb[b].insn[i].opt[1] = OPT_REF;
773
        f->bb[b].insn[i].op[2] = 0; f->bb[b].insn[i].opt[2] = OPT_CONST;
774
        f->bb[b].insn[i].opt[3] = OPT_NONE;
775
      } else if (pb < 0) {
776
        change_insn_type (&f->bb[b].insn[i], II_ADD);
777
        assert (f->INSN(pa).opt[0] == (OPT_REGISTER | OPT_DEST));
778
        f->bb[b].insn[i].op[1] = pa; f->bb[b].insn[i].opt[1] = OPT_REF;
779
        f->bb[b].insn[i].op[2] = 0; f->bb[b].insn[i].opt[2] = OPT_CONST;
780
        f->bb[b].insn[i].opt[3] = OPT_NONE;
781
      } else {
782
        int t = REF (b, 0); /* lrbb should be first instruction */
783
        assert (f->INSN(t).index == II_LRBB);
784
 
785
        f->bb[b].insn[i].op[1] = pa; f->bb[b].insn[i].opt[1] = OPT_REF;
786
        assert (f->INSN(pa).opt[0] == (OPT_REGISTER | OPT_DEST));
787
 
788
        f->bb[b].insn[i].op[2] = pb; f->bb[b].insn[i].opt[2] = OPT_REF;
789
        assert (f->INSN(pb).opt[0] == (OPT_REGISTER | OPT_DEST));
790
 
791
        /* Update op[3] -- flag register */
792
        assert (f->bb[b].insn[i].opt[3] == OPT_REGISTER);
793
        assert (f->bb[b].insn[i].op[3] == LRBB_REG);
794
        assert (t >= 0);
795
        f->bb[b].insn[i].opt[3] = OPT_REF; /* Convert already used regs to references */
796
        f->bb[b].insn[i].op[3] = t;
797
        assert (f->INSN(t).opt[0] == (OPT_REGISTER | OPT_DEST));
798
      }
799
    }
800
 
801
  /* assign register references */
802
  for (b = 0; b < f->num_bb; b++) {
803
    /* rebuild last used reg array */
804
    f->bb[b].last_used_reg[0] = -1;
805
    if (f->bb[b].insn[0].index == II_LRBB) f->bb[b].last_used_reg[LRBB_REG] = 0;
806
    else f->bb[b].last_used_reg[LRBB_REG] = -1;
807
 
808
    for (i = 1; i < MAX_REGS - 1; i++)
809
      f->bb[b].last_used_reg[i] = -1;
810
 
811
    /* Create references */
812
    for (i = 0; i < f->bb[b].ninsn; i++) {
813
      int k;
814
      /* Check for source operands first */
815
      for (k = 0; k < MAX_OPERANDS; k++) {
816
        if (!(f->bb[b].insn[i].opt[k] & OPT_DEST))
817
        if (f->bb[b].insn[i].opt[k] & OPT_REGISTER) {
818
          int t = f->bb[b].last_used_reg[f->bb[b].insn[i].op[k]];
819
 
820
          if (f->bb[b].insn[i].op[k] == 0) { /* Convert r0 to const0 */
821
            f->bb[b].insn[i].opt[k] = OPT_CONST;
822
            f->bb[b].insn[i].op[k] = 0;
823
          } else if (t >= 0) {
824
            f->bb[b].insn[i].opt[k] = OPT_REF; /* Convert already used regs to references */
825
            f->bb[b].insn[i].op[k] = t;
826
            assert (f->INSN(t).opt[0] == (OPT_REGISTER | OPT_DEST));
827
            //f->INSN(t).op[0] = -1;
828
          }
829
        } else if (f->bb[b].insn[i].opt[k] & OPT_REF) {
830
          //f->INSN(f->bb[b].insn[i].op[k]).op[0] = -1; /* Mark referenced */
831
          f->INSN(f->bb[b].insn[i].op[k]).type &= ~IT_UNUSED;
832
        }
833
      }
834
 
835
      /* Now check for destination operand(s) */
836
      for (k = 0; k < MAX_OPERANDS; k++) if (f->bb[b].insn[i].opt[k] & OPT_DEST)
837
        if ((f->bb[b].insn[i].opt[k] & ~OPT_DEST) == OPT_REGISTER
838
          && (int)f->bb[b].insn[i].op[k] >= 0) {
839
          int t = f->bb[b].last_used_reg[f->bb[b].insn[i].op[k]];
840
          assert (f->bb[b].insn[i].op[k] != 0); /* r0 should never be dest */
841
          f->bb[b].last_used_reg[f->bb[b].insn[i].op[k]] = REF (b, i);
842
        }
843
    }
844
  }
845
 
846
  /* Remove all unused lrbb */
847
  for (b = 0; b < f->num_bb; b++)
848
    for (i = 0; i < f->bb[b].ninsn; i++)
849
      if (f->bb[b].insn[i].type & IT_UNUSED) change_insn_type (&f->bb[b].insn[i], II_NOP);
850
 
851
  /* SSAs with final register value are marked as outputs */
852
  assert (f->bb[f->num_bb - 1].type & BB_END);
853
  for (i = 0; i < MAX_REGS; i++) if (!call_saved[i]) {
854
    int t = f->bb[f->num_bb - 1].last_used_reg[i];
855
    /* Mark them volatile, so optimizer does not remove them */
856
    if (t >= 0) f->bb[REF_BB(t)].insn[REF_I(t)].type |= IT_OUTPUT;
857
  }
858
}
859
 
860 897 markom
/* split the BB, based on the group numbers in .tmp */
861
void expand_bb (cuc_func *f, int b)
862
{
863
  int n = f->num_bb;
864
  int mg = 0;
865
  int b1, i, j;
866
 
867
  for (i = 0; i < f->bb[b].ninsn; i++)
868
    if (f->bb[b].insn[i].tmp > mg) mg = f->bb[b].insn[i].tmp;
869
 
870
  /* Create copies */
871
  for (b1 = 1; b1 <= mg; b1++) {
872
    assert (f->num_bb < MAX_BB);
873
    cpy_bb (&f->bb[f->num_bb], &f->bb[b]);
874
    f->num_bb++;
875
  }
876
 
877
  /* Relocate */
878
  for (b1 = 0; b1 < f->num_bb; b1++)
879
    for (i = 0; i < f->bb[b1].ninsn; i++) {
880
      dep_list *d = f->bb[b1].insn[i].dep;
881
      for (j = 0; j < MAX_OPERANDS; j++)
882
        if (f->bb[b1].insn[i].opt[j] & OPT_REF) {
883
          int t = f->bb[b1].insn[i].op[j];
884
          if (REF_BB(t) == b && f->INSN(t).tmp != 0)
885
            f->bb[b1].insn[i].op[j] = REF (n + f->INSN(t).tmp - 1, REF_I(t));
886
        }
887
      while (d) {
888
        if (REF_BB (d->ref) == b && f->INSN(d->ref).tmp != 0)
889
          d->ref = REF (n + f->INSN(d->ref).tmp - 1, REF_I(d->ref));
890
        d = d->next;
891
      }
892
    }
893
 
894
  /* Delete unused instructions */
895
  for (j = 0; j <= mg; j++) {
896
    if (j == 0) b1 = b;
897
    else b1 = n + j - 1;
898
    for (i = 0; i < f->bb[b1].ninsn; i++) {
899
      if (f->bb[b1].insn[i].tmp != j)
900
        change_insn_type (&f->bb[b1].insn[i], II_NOP);
901
      f->bb[b1].insn[i].tmp = 0;
902
    }
903
    if (j < mg) {
904
      f->bb[b1].next[0] = n + j;
905
      f->bb[b1].next[1] = -1;
906
      f->bb[n + j].prev[0] = b1;
907
      f->bb[n + j].prev[1] = -1;
908
    } else {
909
      i = f->bb[b1].next[0];
910
      f->bb[n + j].prev[0] = j == 1 ? b : b1 - 1;
911
      f->bb[n + j].prev[1] = -1;
912
      if (i >= 0) {
913
        if (f->bb[i].prev[0] == b) f->bb[i].prev[0] = b1;
914
        if (f->bb[i].prev[1] == b) f->bb[i].prev[1] = b1;
915
      }
916
      i = f->bb[b1].next[1];
917
      if (i >= 0) {
918
        if (f->bb[i].prev[0] == b) f->bb[i].prev[0] = b1;
919
        if (f->bb[i].prev[1] == b) f->bb[i].prev[1] = b1;
920
      }
921
    }
922
  }
923
}
924
 
925 879 markom
/* Scans sequence of BBs and set bb[].cnt */
926
void generate_bb_seq (cuc_func *f, char *mp_filename, char *bb_filename)
927
{
928
  FILE *fi, *fo;
929
  struct mprofentry_struct *buf;
930
  const int bufsize = 256;
931
  unsigned long *bb_start;
932
  unsigned long *bb_end;
933
  int b, i, r;
934
  int curbb, prevbb = -1;
935
  unsigned long addr = -1;
936
  unsigned long prevaddr = -1;
937 897 markom
  int mssum = 0;
938
  int mlsum = 0;
939
  int mscnt = 0;
940
  int mlcnt = 0;
941 879 markom
 
942
  assert (fi = fopen (mp_filename, "rb"));
943
  assert (fo = fopen (bb_filename, "wb+"));
944
 
945
  assert (bb_start = (unsigned long *) malloc (sizeof (unsigned long) * f->num_bb));
946
  assert (bb_end = (unsigned long *) malloc (sizeof (unsigned long) * f->num_bb));
947
  for (b = 0; b < f->num_bb; b++) {
948
    bb_start[b] = f->start_addr + f->bb[b].first * 4;
949
    bb_end[b] = f->start_addr + f->bb[b].last * 4;
950
    //printf ("%i %x %x\n", b, bb_start[b], bb_end[b]);
951
    f->bb[0].cnt = 0;
952
  }
953
 
954
  buf = (struct mprofentry_struct *) malloc (sizeof (struct mprofentry_struct) * bufsize);
955
  assert (buf);
956
 
957
  //printf ("BBSEQ:\n");
958
  do {
959
    r = fread (buf, sizeof (struct mprofentry_struct), bufsize, fi);
960
    //printf ("r%i : ", r);
961
    for (i = 0; i < r; i++) {
962
      if (buf[i].type & MPROF_FETCH) {
963
        //printf ("%x, ", buf[i].addr);
964
        if (buf[i].addr >= f->start_addr && buf[i].addr <= f->end_addr) {
965
          assert (buf[i].type & MPROF_32);
966
          prevaddr = addr;
967
          addr = buf[i].addr;
968
          for (b = 0; b < f->num_bb; b++)
969
            if (bb_start[b] <= addr && addr <= bb_end[b]) break;
970
          assert (b < f->num_bb);
971
          curbb = b;
972
          if (prevaddr + 4 != addr) prevbb = -1;
973
        } else curbb = -1;
974
 
975
#warning TODO: do not count interrupts
976
        if (curbb != prevbb && curbb >= 0) {
977
          fwrite (&curbb, sizeof (unsigned long), 1, fo);
978
          //printf (" [%i] ", curbb);
979
          f->bb[curbb].cnt++;
980
          prevbb = curbb;
981
        }
982 897 markom
      } else {
983
        if (verify_memoryarea(buf[i].addr))
984
          if (buf[i].type & MPROF_WRITE) mscnt++, mssum += cur_area->delayw;
985
          else mlcnt++, mlsum += cur_area->delayw;
986 879 markom
      }
987
    }
988
    //printf ("\n");
989
  } while (r == bufsize);
990
  //printf ("\n");
991
 
992 897 markom
  runtime.cuc.mdelay[0] = (1. * mlsum) / mlcnt;
993
  runtime.cuc.mdelay[1] = (1. * mlsum) / mlcnt;
994
  runtime.cuc.mdelay[2] = runtime.cuc.mdelay[3] = 1;
995 883 markom
  f->num_runs = f->bb[0].cnt;
996 879 markom
  fclose (fi);
997
  fclose (fo);
998
  free (buf);
999
  free (bb_end);
1000
  free (bb_start);
1001
 
1002
  /* Initialize basic block relocations */
1003
  f->num_init_bb = f->num_bb;
1004
  //printf ("num_init_bb = %i\n", f->num_init_bb);
1005
  assert (f->init_bb_reloc = (int *)malloc (sizeof (int) * f->num_init_bb));
1006
  for (b = 0; b < f->num_init_bb; b++) f->init_bb_reloc[b] = b;
1007
}
1008
 
1009
/* Scans sequence of BBs and set counts for pre/unrolled loop for BB b */
1010
void count_bb_seq (cuc_func *f, int b, char *bb_filename, int *counts, int preroll, int unroll)
1011
{
1012
  FILE *fi;
1013
  const int bufsize = 256;
1014
  int i, r;
1015
  int *buf;
1016
  int cnt = 0;
1017
  int times = preroll - 1 + unroll;
1018
 
1019
  assert (fi = fopen (bb_filename, "rb"));
1020
  for (i = 0; i < times; i++) counts[i] = 0;
1021
  assert (buf = (int *) malloc (sizeof (int) * bufsize));
1022
 
1023
  do {
1024
    r = fread (buf, sizeof (int), bufsize, fi);
1025
    for (i = 0; i < r; i++) {
1026
      /* count consecutive acesses */
1027
      if (f->init_bb_reloc[buf[i]] == b) {
1028
        counts[cnt]++;
1029
        if (++cnt >= times) cnt = preroll - 1;
1030
      } else cnt = 0;
1031
    }
1032
  } while (r == bufsize);
1033
 
1034
  log ("Counts %i,%i :", preroll, unroll);
1035
  for (i = 0; i < times; i++) log ("%x ", counts[i]);
1036
  log ("\n");
1037
 
1038
  fclose (fi);
1039
  free (buf);
1040
}
1041
 
1042
/* relocate all accesses inside of BB b to back/fwd */
1043
static void relocate_bb (cuc_bb *bb, int b, int back, int fwd)
1044
{
1045
  int i, j;
1046
  for (i = 0; i < bb->ninsn; i++)
1047
    for (j = 0; j < MAX_OPERANDS; j++)
1048
      if (bb->insn[i].opt[j] & OPT_REF
1049
       && REF_BB (bb->insn[i].op[j]) == b) {
1050
        int t = REF_I (bb->insn[i].op[j]);
1051
        if (t < i) bb->insn[i].op[j] = REF (back, t);
1052
        else bb->insn[i].op[j] = REF (fwd, t);
1053
      }
1054
}
1055
 
1056
/* Unroll loop b unroll times and return new function. Original
1057
   function is unmodified. */
1058
static cuc_func *unroll_loop (cuc_func *f, int b, int unroll)
1059
{
1060
  int b1, t, i, j, prevb, prevart_b;
1061
  cuc_func *n = dup_func (f);
1062
  cuc_bb *ob = &f->bb[b];
1063
  cuc_insn *ii;
1064
 
1065
  assert (unroll > 1);
1066
  //printf ("unroll BB%i x %i (num_bb %i)\n", b, unroll, n->num_bb);
1067
  unroll--;
1068
  assert (n->num_bb + unroll * 2 < MAX_BB);
1069
 
1070
  prevb = b;
1071
  prevart_b = b;
1072
  /* Duplicate the BB */
1073
  for (t = 0; t < unroll; t++) {
1074
    cuc_bb *pb = &n->bb[prevart_b];
1075
    /* Add new block and set links */
1076
    b1 = n->num_bb++;
1077
    cpy_bb (&n->bb[b1], ob);
1078
    /* Only one should be in loop, so we remove any INLOOP flags from duplicates */
1079
    n->bb[b1].type &= ~(BB_END | BB_INLOOP);
1080
 
1081
    /* Set predecessor's successor */
1082
    if (n->bb[prevb].next[0] == b) {
1083
      n->bb[prevb].next[0] = b1;
1084
      if (pb->next[0] < 0) pb->next[0] = b1 + 1;
1085
      else pb->next[1] = b1 + 1;
1086
      n->bb[b1].next[1] = b1 + 1;
1087
    } else if (n->bb[prevb].next[1] == b) {
1088
      if (pb->next[0] < 0) pb->next[0] = b1 + 1;
1089
      else pb->next[1] = b1 + 1;
1090
      n->bb[b1].next[0] = b1 + 1;
1091
      n->bb[prevb].next[1] = b1;
1092
    } else assert (0);
1093
 
1094
    /* Set predecessor */
1095
    n->bb[b1].prev[0] = prevb;
1096
    n->bb[b1].prev[1] = -1;
1097
 
1098
    /* Relocate backward references to current instance and forward references
1099
       to previous one */
1100
    relocate_bb (&n->bb[b1], b, b1, prevb);
1101
 
1102
    /* add artificial block, just to join accesses */
1103
    b1 = n->num_bb++;
1104
    cpy_bb (&n->bb[b1], ob);
1105
    n->bb[b1].cnt = 0;
1106
 
1107
    for (i = 0; i < ob->ninsn - 1; i++) {
1108
      ii = &n->bb[b1].insn[i];
1109
      if (ob->insn[i].opt[0] & OPT_DEST) {
1110
        change_insn_type (ii, II_CMOV);
1111
        ii->op[0] = -1; ii->opt[0] = OPT_REGISTER | OPT_DEST;
1112
        ii->op[1] = REF (prevart_b, i); ii->opt[1] = OPT_REF;
1113
        ii->op[2] = REF (b1 - 1, i); ii->opt[2] = OPT_REF;
1114
 
1115
        /* Take left one, if we should have finished the first iteration*/
1116
        if (pb->insn[pb->ninsn - 1].type & IT_BRANCH) {
1117
          ii->op[3] = pb->insn[pb->ninsn - 1].op[1]; ii->opt[3] = pb->insn[pb->ninsn - 1].opt[1];
1118
        } else {
1119
          assert (pb->insn[pb->ninsn - 1].type & IT_COND);
1120
          ii->op[3] = REF (prevart_b, pb->ninsn - 1); ii->opt[3] = OPT_REF;
1121
        }
1122
        ii->dep = NULL;
1123
        ii->type = 0;
1124
      } else {
1125
        change_insn_type (ii, II_NOP);
1126
      }
1127
    }
1128
 
1129
    /* Add sfor instruction at the end, prioritizing flags */
1130
    ii = &n->bb[b1].insn[ob->ninsn - 1];
1131
    change_insn_type (ii, II_SFOR);
1132
    ii->op[0] = FLAG_REG; ii->opt[0] = OPT_REGISTER | OPT_DEST;
1133
    if (pb->insn[pb->ninsn - 1].type & IT_BRANCH) {
1134
      ii->op[1] = pb->insn[pb->ninsn - 1].op[1];
1135
      ii->opt[1] = pb->insn[pb->ninsn - 1].opt[1];
1136
    } else {
1137
      ii->op[1] = REF (prevart_b, pb->ninsn - 1);
1138
      ii->opt[1] = OPT_REF;
1139
    }
1140
    if (n->bb[b1 - 1].insn[pb->ninsn - 1].type & IT_BRANCH) {
1141
      ii->op[2] = n->bb[b1 - 1].insn[pb->ninsn - 1].op[1];
1142
      ii->opt[2] = n->bb[b1 - 1].insn[pb->ninsn - 1].opt[1];
1143
    } else {
1144
      ii->op[2] = REF (b1 - 1, pb->ninsn - 1);
1145
      ii->opt[2] = OPT_REF;
1146
    }
1147
    ii->opt[3] = OPT_NONE;
1148
    ii->type = IT_COND;
1149
 
1150
    /* Only one should be in loop, so we remove any INLOOP flags from duplicates */
1151
    n->bb[b1].type &= ~(BB_END | BB_INLOOP);
1152
    n->bb[b1].prev[0] = prevart_b;
1153
    n->bb[b1].prev[1] = b1 - 1;
1154
    n->bb[b1].next[0] = ob->next[0] == b ? ob->next[1] : ob->next[0];
1155
    n->bb[b1].next[1] = -1;
1156
 
1157
    prevb = b1 - 1;
1158
    prevart_b = b1;
1159
  }
1160
  if (ob->type & BB_END) {
1161
    n->bb[prevart_b].type |= BB_END;
1162
    n->bb[b].type &= ~BB_END;
1163
  }
1164
 
1165
  //print_cuc_bb (n, "unroll1");
1166
  /* repair BB after loop, to point back to latest artificial BB */
1167
  b1 = n->bb[prevart_b].next[0];
1168
  if (b1 >= 0) {
1169 897 markom
    if (n->bb[b1].prev[0] == b) n->bb[b1].prev[0] = prevart_b;
1170
    else if (n->bb[b1].prev[1] == b) n->bb[b1].prev[1] = prevart_b;
1171 879 markom
    else assert (0);
1172
  }
1173
 
1174
  /* Relink back to start of the loop */
1175
  /* Set predecessor's successor */
1176
  if (n->bb[prevb].next[0] == b) n->bb[prevb].next[0] = b;
1177
  else if (n->bb[prevb].next[1] == b) n->bb[prevb].next[1] = b;
1178
  else assert (0);
1179
 
1180
  /* Set predecessor */
1181
  if (n->bb[b].prev[0] == b) n->bb[b].prev[0] = prevb;
1182
  else if (n->bb[b].prev[1] == b) n->bb[b].prev[1] = prevb;
1183
  else assert (0);
1184
 
1185
  //print_cuc_bb (n, "unroll2");
1186
 
1187
  /* Relocate backward references to current instance and forward references
1188
     to previous one */
1189
  relocate_bb (&n->bb[b], b, b, prevb);
1190
 
1191
  /* Relocate all other blocks to point to latest prevart_b */
1192
  for (i = 0; i < f->num_bb; i++)
1193
    if (i != b) relocate_bb (&n->bb[i], b, prevart_b, prevart_b);
1194
 
1195
  return n;
1196
}
1197
 
1198
/* Preroll loop b preroll times and return new function. Original
1199
   function is unmodified. */
1200
static cuc_func *preroll_loop (cuc_func *f, int b, int preroll)
1201
{
1202
  int b1, t, i, j, prevb, prevart_b;
1203
  cuc_func *n = dup_func (f);
1204
  cuc_bb *ob = &f->bb[b];
1205
  cuc_insn *ii;
1206
 
1207
  assert (preroll > 1);
1208
  //printf ("preroll BB%i x %i (num_bb %i)\n", b, preroll, n->num_bb);
1209
  preroll--;
1210
  assert (n->num_bb + preroll * 2 < MAX_BB);
1211
 
1212
  prevb = b;
1213
  prevart_b = b;
1214
  /* Duplicate the BB */
1215
  for (t = 0; t < preroll; t++) {
1216
    cuc_bb *pb = &n->bb[prevart_b];
1217
    /* Add new block and set links */
1218
    b1 = n->num_bb++;
1219
    cpy_bb (&n->bb[b1], ob);
1220
    /* Only one should be in loop, so we remove any INLOOP flags from duplicates */
1221
    n->bb[b1].type &= ~(BB_END | BB_INLOOP);
1222
 
1223
    /* Set predecessor's successor */
1224
    if (n->bb[prevb].next[0] == b) {
1225
      n->bb[prevb].next[0] = b1;
1226
      if (pb->next[0] < 0) pb->next[0] = b1 + 1;
1227
      else pb->next[1] = b1 + 1;
1228
      n->bb[b1].next[1] = b1 + 1;
1229
    } else if (n->bb[prevb].next[1] == b) {
1230
      if (pb->next[0] < 0) pb->next[0] = b1 + 1;
1231
      else pb->next[1] = b1 + 1;
1232
      n->bb[b1].next[0] = b1 + 1;
1233
      n->bb[prevb].next[1] = b1;
1234
    } else assert (0);
1235
 
1236
    /* Set predecessor */
1237
    n->bb[b1].prev[0] = prevb;
1238
    n->bb[b1].prev[1] = -1;
1239
 
1240
    /* Relocate backward references to current instance and forward references
1241
       to previous one */
1242
    relocate_bb (&n->bb[b1], b, b1, prevb);
1243
 
1244
    /* add artificial block, just to join accesses */
1245
    b1 = n->num_bb++;
1246
    cpy_bb (&n->bb[b1], ob);
1247
    n->bb[b1].cnt = 0;
1248
 
1249
    for (i = 0; i < ob->ninsn - 1; i++) {
1250
      ii = &n->bb[b1].insn[i];
1251
      if (ob->insn[i].opt[0] & OPT_DEST) {
1252
        change_insn_type (ii, II_CMOV);
1253
        ii->op[0] = -1; ii->opt[0] = OPT_REGISTER | OPT_DEST;
1254
        ii->op[1] = REF (prevart_b, i); ii->opt[1] = OPT_REF;
1255
        ii->op[2] = REF (b1 - 1, i); ii->opt[2] = OPT_REF;
1256
 
1257
        /* Take left one, if we should have finished the first iteration*/
1258
        if (pb->insn[pb->ninsn - 1].type & IT_BRANCH) {
1259
          ii->op[3] = pb->insn[pb->ninsn - 1].op[1]; ii->opt[3] = pb->insn[pb->ninsn - 1].opt[1];
1260
        } else {
1261
          assert (pb->insn[pb->ninsn - 1].type & IT_COND);
1262
          ii->op[3] = REF (prevart_b, pb->ninsn - 1); ii->opt[3] = OPT_REF;
1263
        }
1264
        ii->dep = NULL;
1265
        ii->type = 0;
1266
      } else {
1267
        change_insn_type (ii, II_NOP);
1268
      }
1269
    }
1270
 
1271
    /* Add sfor instruction at the end, prioritizing flags */
1272
    ii = &n->bb[b1].insn[ob->ninsn - 1];
1273
    change_insn_type (ii, II_SFOR);
1274
    ii->op[0] = FLAG_REG; ii->opt[0] = OPT_REGISTER | OPT_DEST;
1275
    if (pb->insn[pb->ninsn - 1].type & IT_BRANCH) {
1276
      ii->op[1] = pb->insn[pb->ninsn - 1].op[1];
1277
      ii->opt[1] = pb->insn[pb->ninsn - 1].opt[1];
1278
    } else {
1279
      ii->op[1] = REF (prevart_b, pb->ninsn - 1);
1280
      ii->opt[1] = OPT_REF;
1281
    }
1282
    if (n->bb[b1 - 1].insn[pb->ninsn - 1].type & IT_BRANCH) {
1283
      ii->op[2] = n->bb[b1 - 1].insn[pb->ninsn - 1].op[1];
1284
      ii->opt[2] = n->bb[b1 - 1].insn[pb->ninsn - 1].opt[1];
1285
    } else {
1286
      ii->op[2] = REF (b1 - 1, pb->ninsn - 1);
1287
      ii->opt[2] = OPT_REF;
1288
    }
1289
    ii->opt[3] = OPT_NONE;
1290
    ii->type = IT_COND;
1291
 
1292
    /* Only one should be in loop, so we remove any INLOOP flags from duplicates */
1293
    n->bb[b1].type &= ~(BB_END | BB_INLOOP);
1294
    n->bb[b1].prev[0] = prevart_b;
1295
    n->bb[b1].prev[1] = b1 - 1;
1296
    n->bb[b1].next[0] = ob->next[0] == b ? ob->next[1] : ob->next[0];
1297
    n->bb[b1].next[1] = -1;
1298
 
1299
    prevb = b1 - 1;
1300
    prevart_b = b1;
1301
  }
1302
  if (ob->type & BB_END) {
1303
    n->bb[prevart_b].type |= BB_END;
1304
    n->bb[b].type &= ~BB_END;
1305
  }
1306
 
1307
  //print_cuc_bb (n, "preroll1");
1308
  /* repair BB after loop, to point back to latest artificial BB */
1309
  b1 = n->bb[prevart_b].next[0];
1310
  if (b1 >= 0) {
1311 897 markom
    if (n->bb[b1].prev[0] == b) n->bb[b1].prev[0] = prevart_b;
1312
    else if (n->bb[b1].prev[1] == b) n->bb[b1].prev[1] = prevart_b;
1313 879 markom
    else assert (0);
1314
  }
1315
 
1316
  /* Relink to itself */
1317
  /* Set predecessor's successor */
1318
  if (n->bb[prevb].next[0] == b) n->bb[prevb].next[0] = prevb;
1319
  else if (n->bb[prevb].next[1] == b) n->bb[prevb].next[1] = prevb;
1320
  else assert (0);
1321
  n->bb[prevb].prev[1] = prevb;
1322
 
1323
  if (n->bb[b].prev[0] == b) {
1324
    n->bb[b].prev[0] = n->bb[b].prev[1];
1325
    n->bb[b].prev[1] = -1;
1326
  } else if (n->bb[b].prev[1] == b) {
1327
    n->bb[b].prev[1] = -1;
1328
  }
1329
 
1330
  //print_cuc_bb (n, "preroll2");
1331
 
1332
  /* Relocate backward references to current instance and forward references
1333
     to previous one */
1334
  relocate_bb (&n->bb[b], b, b, prevb);
1335
 
1336
  /* Relocate all other blocks to point to latest prevart_b */
1337
  for (i = 0; i < f->num_bb; i++)
1338
    if (i != b) relocate_bb (&n->bb[i], b, prevart_b, prevart_b);
1339
 
1340
  return n;
1341
}
1342
 
1343
/* Unroll loop b unroll times and return new function. Original
1344
   function is unmodified. */
1345
cuc_func *preunroll_loop (cuc_func *f, int b, int preroll, int unroll, char *bb_filename)
1346
{
1347
  int b1, i;
1348
  cuc_func *n, *t;
1349
  int *counts;
1350
  int *bb_reloc;
1351
 
1352
  if (preroll > 1) {
1353
    t = preroll_loop (f, b, preroll);
1354
    b1 = t->num_bb - 2;
1355
    if (unroll > 1) {
1356
      //print_cuc_bb (t, "preunroll1");
1357
      n = unroll_loop (t, b1, unroll);
1358
      free_func (t);
1359
    } else n = t;
1360
  } else {
1361
    b1 = b;
1362 897 markom
    if (unroll > 1) n = unroll_loop (f, b1, unroll);
1363
    else return dup_func (f);
1364 879 markom
  }
1365
 
1366 897 markom
  /* Assign new counts to functions */
1367 879 markom
  assert (counts = (int *)malloc (sizeof (int) * (preroll - 1 + unroll)));
1368
  count_bb_seq (n, b, bb_filename, counts, preroll, unroll);
1369
  for (i = 0; i < preroll - 1 + unroll; i++) {
1370
    if (i == 0) b1 = b;
1371
    else b1 = f->num_bb + (i - 1) * 2;
1372
    n->bb[b1].cnt = counts[i];
1373
  }
1374
 
1375
  //print_cuc_bb (n, "preunroll");
1376
  free (counts);
1377
  return n;
1378
}

powered by: WebSVN 2.1.0

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