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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_69/] [or1ksim/] [cuc/] [bb.c] - Blame information for rev 1044

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

powered by: WebSVN 2.1.0

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