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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc2/] [or1ksim/] [cuc/] [bb.c] - Blame information for rev 1042

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

powered by: WebSVN 2.1.0

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