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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [cuc/] [adv.c] - Blame information for rev 938

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

Line No. Rev Author Line
1 937 markom
/* adv.c -- OpenRISC Custom Unit Compiler, Advanced Optimizations
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
#include "sim-config.h"
25
#include "abstract.h"
26
#include "cuc.h"
27
#include "insn.h"
28
#include "support/profile.h"
29
 
30
/* Marks successor of b with mask m */
31
static void mark_successors (cuc_func *f, int b, int m, int stopb)
32
{
33
  if (b < 0 || b == BBID_END) return;
34
  if (f->bb[b].tmp & m) return;
35
  f->bb[b].tmp |= m;
36
  /* mark stopb also; and stop searching -- we will gen new result in stopb */
37
  if (b == stopb) return;
38
  mark_successors (f, f->bb[b].next[0], m, stopb);
39
  mark_successors (f, f->bb[b].next[1], m, stopb);
40
}
41
 
42
static unsigned long mask (unsigned long c)
43
{
44
  if (c) return (1 << (log2 (c) + 1)) - 1;
45
  else return 0;
46
}
47
 
48
/* Calculates facts, that are determined by conditionals */
49
void insert_conditional_facts (cuc_func *f)
50
{
51
  int b, i, j;
52
  int b1, i1, j1;
53
  cuc_insn n[2];
54
  for (b = 0; b < f->num_bb; b++) if (f->bb[b].ninsn > 0) {
55
    cuc_insn *ii = &f->bb[b].insn[f->bb[b].ninsn - 1];
56
    /* We have following situation
57
       x <= ...
58
       sfxx f, x, CONST
59
       bf ..., f */
60
    if (ii->type & IT_BRANCH && ii->opt[1] & OPT_REF && REF_BB(ii->op[1]) == b
61
     && f->INSN(ii->op[1]).opt[2] & OPT_CONST) {
62
      int ok = 0;
63
      unsigned long c = f->INSN(ii->op[1]).op[2];
64
      int rref = f->INSN(ii->op[1]).op[1];
65
      unsigned long r;
66
      if (!(f->INSN(ii->op[1]).opt[1] & OPT_REF)) continue;
67
      r = f->INSN(rref).op[0];
68 938 markom
 
69
      /* Assignment must be in same basic block */
70
      if (REF_BB(rref) != b) continue;
71
 
72 937 markom
      for (j = 0; j < 2; j++) {
73
        change_insn_type (&n[j], II_ADD);
74
        n[j].type = 0;
75
        n[j].dep = NULL;
76
        n[j].op[0] = r; n[j].opt[0] = OPT_REGISTER | OPT_DEST;
77
        n[j].op[1] = 0; n[j].opt[1] = OPT_CONST;
78
        n[j].op[2] = rref; n[j].opt[2] = OPT_REF;
79
        n[j].opt[3] = OPT_NONE;
80
        sprintf (n[j].disasm, "conditional %s fact", j ? "false" : "true");
81
      }
82
 
83
      /* First get the conditional and two instruction to place after the current BB */
84
      switch (f->INSN(ii->op[1]).index) {
85
        case II_SFEQ:
86
          change_insn_type (&n[0], II_ADD);
87
          n[0].op[0] = r; n[0].opt[0] = OPT_REGISTER | OPT_DEST;
88
          n[0].op[1] = 0; n[0].opt[1] = OPT_CONST;
89
          n[0].op[2] = c; n[0].opt[2] = OPT_CONST;
90
          ok = 1;
91
          break;
92
        case II_SFNE:
93
          change_insn_type (&n[1], II_ADD);
94
          n[1].op[0] = r; n[1].opt[0] = OPT_REGISTER | OPT_DEST;
95
          n[1].op[1] = 0; n[1].opt[1] = OPT_CONST;
96
          n[1].op[2] = c; n[1].opt[2] = OPT_CONST;
97
          ok = 2;
98
          break;
99
        case II_SFLT:
100
          change_insn_type (&n[0], II_AND);
101
          n[0].op[0] = r; n[0].opt[0] = OPT_REGISTER | OPT_DEST;
102
          n[0].op[1] = rref; n[0].opt[1] = OPT_REF;
103
          n[0].op[2] = mask (c); n[0].opt[2] = OPT_CONST;
104
          ok = 1;
105
          break;
106
        case II_SFGT:
107
          change_insn_type (&n[1], II_ADD);
108
          n[1].op[0] = r; n[1].opt[0] = OPT_REGISTER | OPT_DEST;
109
          n[1].op[1] = rref; n[1].opt[1] = OPT_REF;
110
          n[1].op[2] = mask (c + 1); n[1].opt[2] = OPT_CONST;
111
          ok = 2;
112
          break;
113
        case II_SFLE:
114
          change_insn_type (&n[0], II_AND);
115
          n[0].op[0] = r; n[0].opt[0] = OPT_REGISTER | OPT_DEST;
116
          n[0].op[1] = rref; n[0].opt[1] = OPT_REF;
117
          n[0].op[2] = mask (c); n[0].opt[2] = OPT_CONST;
118
          ok = 1;
119
          break;
120
        case II_SFGE:
121
          change_insn_type (&n[1], II_ADD);
122
          n[1].op[0] = r; n[1].opt[0] = OPT_REGISTER | OPT_DEST;
123
          n[1].op[1] = rref; n[1].opt[1] = OPT_REF;
124
          n[1].op[2] = mask (c + 1); n[1].opt[2] = OPT_CONST;
125
          ok = 2;
126
          break;
127
        default:
128
          ok = 0;
129
          break;
130
      }
131
 
132
      /* Now add two BBs at the end and relink */
133
      if (ok) {
134
        int cnt = 0;
135 938 markom
        cucdebug (1, "%x rref %x cnt %i\n", b, rref, cnt);
136 937 markom
        fflush (stdout);
137
        for (j = 0; j < 2; j++) {
138
          int nb = f->num_bb++;
139
          int sb;
140
          int k;
141
          assert (nb < MAX_BB);
142
          f->bb[nb].type = 0;
143
          f->bb[nb].first = -1; f->bb[nb].last = -1;
144
          f->bb[nb].prev[0] = b; f->bb[nb].prev[1] = -1;
145
          sb = f->bb[nb].next[0] = f->bb[b].next[j]; f->bb[nb].next[1] = -1;
146
          assert (cnt >= 0);
147 938 markom
          cucdebug (2, "%x %x %x rref %x cnt %i\n", b, sb, nb, rref, cnt);
148 937 markom
          fflush (stdout);
149
          assert (sb >= 0);
150
          f->bb[b].next[j] = nb;
151
          if (sb != BBID_END) {
152
            if (f->bb[sb].prev[0] == b) f->bb[sb].prev[0] = nb;
153
            else if (f->bb[sb].prev[1] == b) f->bb[sb].prev[1] = nb;
154
            else assert (0);
155
          }
156
          f->bb[nb].insn = (cuc_insn *) malloc (sizeof (cuc_insn) * (cnt + 1));
157
          assert (f->bb[nb].insn);
158
          f->bb[nb].insn[0] = n[j];
159
          f->bb[nb].ninsn = cnt + 1;
160
          f->bb[nb].mdep = NULL;
161
          f->bb[nb].nmemory = 0;
162
          f->bb[nb].cnt = 0;
163
          f->bb[nb].unrolled = 0;
164
          f->bb[nb].ntim = 0;
165
          f->bb[nb].selected_tim = -1;
166
        }
167
        for (b1 = 0; b1 < f->num_bb; b1++) f->bb[b1].tmp = 0;
168
 
169
        /* Find successor blocks and change links accordingly */
170
        mark_successors (f, f->num_bb - 2, 2, b);
171
        mark_successors (f, f->num_bb - 1, 1, b);
172
        for (b1 = 0; b1 < f->num_bb - 2; b1++) if (f->bb[b1].tmp == 1 || f->bb[b1].tmp == 2) {
173
          int end;
174
          if (REF_BB (rref) == b1) end = REF_I (rref) + 1;
175
          else end = f->bb[b1].ninsn;
176
          for (i1 = 0; i1 < end; i1++)
177
            for (j1 = 0; j1 < MAX_OPERANDS; j1++)
178
              if (f->bb[b1].insn[i1].opt[j1] & OPT_REF && f->bb[b1].insn[i1].op[j1] == rref)
179
                f->bb[b1].insn[i1].op[j1] = REF (f->num_bb - f->bb[b1].tmp, 0);
180
        }
181 938 markom
        if (cuc_debug >= 3) print_cuc_bb (f, "FACT");
182 937 markom
      }
183
    }
184
  }
185
}
186
 
187
static unsigned long max_op (cuc_func *f, int ref, int o)
188
{
189
  if (f->INSN(ref).opt[o] & OPT_REF) return f->INSN(f->INSN(ref).op[o]).max;
190
  else if (f->INSN(ref).opt[o] & OPT_CONST) return f->INSN(ref).op[o];
191
  else if (f->INSN(ref).opt[o] & OPT_REGISTER) return 0xffffffff;
192
  else assert (0);
193
}
194
 
195
/* Returns maximum value, based on inputs */
196
static unsigned long calc_max (cuc_func *f, int ref)
197
{
198
  cuc_insn *ii = &f->INSN(ref);
199
  if (ii->type & IT_COND) return 1;
200
  switch (ii->index) {
201
    case II_ADD : return MIN ((unsigned long long) max_op (f, ref, 1)
202
                            + (unsigned long long)max_op (f, ref, 2), 0xffffffff);
203
    case II_SUB : return 0xffffffff;
204
    case II_AND : return MIN (max_op (f, ref, 1), max_op (f, ref, 2));
205
    case II_OR  : return max_op (f, ref, 1) | max_op (f, ref, 2);
206
    case II_XOR : return max_op (f, ref, 1) | max_op (f, ref, 2);
207
    case II_MUL : return MIN ((unsigned long long) max_op (f, ref, 1)
208
                            * (unsigned long long)max_op (f, ref, 2), 0xffffffff);
209
    case II_SLL : if (ii->opt[2] & OPT_CONST) return max_op (f, ref, 1) << ii->op[2];
210
                  else return max_op (f, ref, 1);
211
    case II_SRA : return max_op (f, ref, 1);
212
    case II_SRL : if (ii->opt[2] & OPT_CONST) return max_op (f, ref, 1) >> ii->op[2];
213
                  else return max_op (f, ref, 1);
214
    case II_LB  : return 0xff;
215
    case II_LH  : return 0xffff;
216
    case II_LW  : return 0xffffffff;
217
    case II_SB  :
218
    case II_SH  :
219
    case II_SW  : return 0;
220
    case II_SFEQ:
221
    case II_SFNE:
222
    case II_SFLE:
223
    case II_SFLT:
224
    case II_SFGE:
225
    case II_SFGT: return 1;
226
    case II_BF  : return 0;
227
    case II_LRBB: return 1;
228
    case II_CMOV: return MAX (max_op (f, ref, 1), max_op (f, ref, 2));
229
    case II_REG : return max_op (f, ref, 1);
230
    case II_NOP : assert (0);
231
    case II_CALL: assert (0);
232
    default:  assert (0);
233
  }
234
  return -1;
235
}
236
 
237
/* Width optimization -- detect maximum values;
238
   these values are actually estimates, since the problem
239
   is to hard otherwise...
240
   We calculate these maximums iteratively -- we are slowly
241
   approaching final solution. This algorithm is surely finite,
242
   but can be very slow; so we stop after some iterations;
243
   normal loops should be in this range */
244
void detect_max_values (cuc_func *f)
245
{
246
  int b, i, j;
247
  int modified = 0;
248
  int iteration = 0;
249
 
250
  for (b = 0; b < f->num_bb; b++) {
251
    for (i = 0; i < f->bb[b].ninsn; i++) f->bb[b].insn[i].max = 0;
252
    f->bb[b].tmp = 1;
253
  }
254
 
255
  /* Repeat until something is changing */
256
  do {
257
    modified = 0;
258
    for (b = 0; b < f->num_bb; b++) {
259
      if (f->bb[b].tmp) {
260
        for (i = 0; i < f->bb[b].ninsn; i++) {
261
          unsigned long m = calc_max (f, REF (b, i));
262
          if (m > f->bb[b].insn[i].max) {
263
            f->bb[b].insn[i].max = m;
264
            modified = 1;
265
          }
266
        }
267
      }
268
    }
269
    if (iteration++ > CUC_WIDTH_ITERATIONS) break;
270
  } while (modified);
271
 
272
  /* Something bad has happened; now we will assign 0xffffffff to all unsatisfied
273
     instructions; this one is stoppable in O(n ^ 2) */
274
  if (iteration > CUC_WIDTH_ITERATIONS) {
275
    do {
276
      modified = 0;
277
      for (b = 0; b < f->num_bb; b++)
278
        for (i = 0; i < f->bb[b].ninsn; i++) {
279
          unsigned long m = calc_max (f, REF (b, i));
280
          if (m > f->bb[b].insn[i].max) {
281
            f->bb[b].insn[i].max = 0xffffffff;
282
            modified = 1;
283
          }
284
        }
285
    } while (modified);
286
  }
287
  cucdebug (1, "detect_max_values %i iterations\n", iteration);
288
}
289
 

powered by: WebSVN 2.1.0

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