OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [or1ksim/] [cuc/] [bb.c] - Blame information for rev 22

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

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

powered by: WebSVN 2.1.0

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