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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cuc/] [bb.c] - Blame information for rev 925

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

powered by: WebSVN 2.1.0

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