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 1061

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

powered by: WebSVN 2.1.0

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