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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [cuc/] [adv.c] - Blame information for rev 937

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

powered by: WebSVN 2.1.0

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