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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [cuc/] [bb.c] - Blame information for rev 897

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

powered by: WebSVN 2.1.0

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