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