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