1 |
879 |
markom |
/* insn.c -- OpenRISC Custom Unit Compiler, instruction support
|
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 "cuc.h"
|
25 |
|
|
#include "insn.h"
|
26 |
|
|
|
27 |
|
|
/* Table of known instructions. Watch out for indexes I_*! */
|
28 |
|
|
const cuc_known_insn known[II_LAST + 1] = {
|
29 |
|
|
{"add", 1, "assign \1 = \2 + \3;"},
|
30 |
|
|
{"sub", 0, "assign \1 = \2 - \3;"},
|
31 |
|
|
{"and", 1, "assign \1 = \2 & \3;"},
|
32 |
|
|
{"or", 1, "assign \1 = \2 | \3;"},
|
33 |
|
|
{"xor", 1, "assign \1 = \2 ^ \3;"},
|
34 |
|
|
{"mul", 1, "assign \1 = \2 * \3;"},
|
35 |
|
|
|
36 |
|
|
{"srl", 0, "assign \1 = \2 >> \3;"},
|
37 |
|
|
{"sll", 0, "assign \1 = \2 << \3;"},
|
38 |
|
|
{"sra", 0, "assign \1 = ({32{\2[31]}} << (6'd32-{1'b0, \3}))\n\
|
39 |
|
|
| \2 >> \3;"},
|
40 |
|
|
|
41 |
|
|
{"lb", 0, "always @(posedge clk or posedge rst)"},
|
42 |
|
|
{"lh", 0, "always @(posedge clk or posedge rst)"},
|
43 |
|
|
{"lw", 0, "always @(posedge clk or posedge rst)"},
|
44 |
|
|
{"sb", 0, "/* mem8[\2] = \1 */"},
|
45 |
|
|
{"sh", 0, "/* mem16[\2] = \1 */"},
|
46 |
|
|
{"sw", 0, "/* mem32[\2] = \1 */"},
|
47 |
|
|
|
48 |
|
|
{"sfeq", 1, "assign \1 = \2 == \3;"},
|
49 |
|
|
{"sfne", 1, "assign \1 = \2 != \3;"},
|
50 |
|
|
{"sfle", 0, "assign \1 = \2 <= \3;"},
|
51 |
|
|
{"sflt", 0, "assign \1 = \2 < \3;"},
|
52 |
|
|
{"sfgt", 0, "assign \1 = \2 > \3;"},
|
53 |
|
|
{"sfge", 0, "assign \1 = \2 >= \3;"},
|
54 |
|
|
{"sfor", 1, "assign \1 = \2 || \3;"},
|
55 |
|
|
{"bf", 0, ""},
|
56 |
|
|
|
57 |
|
|
{"lrbb", 0,"always @(posedge clk or posedge rst)"},
|
58 |
|
|
{"cmov", 0,"assign \1 = \4 ? \2 : \3;"},
|
59 |
|
|
{"reg", 0, "always @(posedge clk or posedge rst)"},
|
60 |
|
|
|
61 |
|
|
{"nop", 0, NULL}};
|
62 |
|
|
|
63 |
|
|
/* Find known instruction and attach them to insn */
|
64 |
|
|
void change_insn_type (cuc_insn *i, int index)
|
65 |
|
|
{
|
66 |
|
|
int j;
|
67 |
|
|
assert (index >= 0 && index <= II_LAST);
|
68 |
|
|
i->index = index;
|
69 |
|
|
if (i->index == II_NOP) {
|
70 |
|
|
for (j = 0; j < MAX_OPERANDS; j++) i->opt[j] = OPT_NONE;
|
71 |
|
|
i->type = 0;
|
72 |
|
|
i->dep = NULL;
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
/* Returns instruction name */
|
77 |
|
|
const char *cuc_insn_name (cuc_insn *ii) {
|
78 |
|
|
if (ii->index < 0 || ii->index > II_LAST) return "???";
|
79 |
|
|
else return known[ii->index].name;
|
80 |
|
|
}
|
81 |
883 |
markom |
|
82 |
|
|
/* CSE -- common subexpression elimination */
|
83 |
|
|
void cse (cuc_func *f)
|
84 |
|
|
{
|
85 |
|
|
int b, i, j, b1, i1, b2, i2, j2;
|
86 |
|
|
for (b1 = 0; b1 < f->num_bb; b1++)
|
87 |
|
|
for (i1 = 0; i1 < f->bb[b1].ninsn; i1++)
|
88 |
|
|
for (b2 = 0; b2 < f->num_bb; b2++)
|
89 |
|
|
for (i2 = 0; i2 < f->bb[b2].ninsn; i2++) {
|
90 |
|
|
cuc_insn *ii1 = &f->bb[b1].insn[i1];
|
91 |
|
|
cuc_insn *ii2 = &f->bb[b2].insn[i2];
|
92 |
|
|
|
93 |
|
|
/* Do we have an exact match? */
|
94 |
|
|
if (ii1->index == ii2->index) continue;
|
95 |
|
|
if (ii1->type & IT_VOLATILE) continue;
|
96 |
|
|
|
97 |
|
|
if (ii1->op[1] != ii2->op[1] || ii1->opt[1] != ii2->opt[1]) continue;
|
98 |
|
|
if (ii1->op[2] != ii2->op[2] || ii1->opt[2] != ii2->opt[2]) continue;
|
99 |
|
|
if (ii1->opt[3] != ii2->opt[3]) continue;
|
100 |
|
|
if (ii1->opt[3] != OPT_NONE && ii1->op[3] != ii2->op[3]) continue;
|
101 |
|
|
|
102 |
|
|
/* Check if we drive outputs? */
|
103 |
|
|
if ((ii1->opt[0] & OPT_REGISTER) && ii1->op[0] >= 0)
|
104 |
|
|
if ((ii2->opt[0] & OPT_REGISTER) && ii2->op[0] >= 0) continue;
|
105 |
|
|
else ii2->op[0] = ii1->op[0];
|
106 |
|
|
|
107 |
|
|
/* remove duplicated instruction and relink the references */
|
108 |
|
|
change_insn_type (ii2, II_NOP);
|
109 |
|
|
for (b = 0; b < f->num_bb; b++)
|
110 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++)
|
111 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
112 |
|
|
if (f->bb[b].insn[i].opt[j] & OPT_REF && f->bb[b].insn[i].op[j] == REF (b2, i2))
|
113 |
|
|
f->bb[b].insn[i].op[j] = REF (b1, i1);
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
static int count_cmovs (cuc_insn *ii, int match)
|
118 |
|
|
{
|
119 |
|
|
int c = 0, j;
|
120 |
|
|
if (match & 2) {
|
121 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
122 |
|
|
if (ii->opt[j] & OPT_DEST) c++;
|
123 |
|
|
}
|
124 |
|
|
if (match & 1) {
|
125 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
126 |
|
|
if (!(ii->opt[j] & OPT_DEST) && ii->opt[j] & OPT_REF) c++;
|
127 |
|
|
} else {
|
128 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
129 |
|
|
if (!(ii->opt[j] & OPT_DEST) && ii->opt[j] != OPT_NONE) c++;
|
130 |
|
|
}
|
131 |
|
|
return c;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
static void search_csm (int iter, cuc_func *f, cuc_shared *list);
|
135 |
|
|
static cuc_shared *main_list;
|
136 |
|
|
static int *iteration;
|
137 |
|
|
|
138 |
|
|
/* CSM -- common subexpression matching -- resource sharing */
|
139 |
|
|
void csm (cuc_func *f)
|
140 |
|
|
{
|
141 |
|
|
int b, i, j;
|
142 |
|
|
int cnt;
|
143 |
|
|
cuc_shared *list;
|
144 |
|
|
cuc_timings timings;
|
145 |
|
|
|
146 |
|
|
analyse_timings (f, &timings);
|
147 |
|
|
main_list = NULL;
|
148 |
|
|
for (b = 0; b < f->num_bb; b++) {
|
149 |
|
|
assert (iteration = (int *)malloc (sizeof (int) * f->bb[b].ninsn));
|
150 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++) {
|
151 |
|
|
int cnt = 0, cntc = 0;
|
152 |
|
|
double size = 0., sizec = 0.;
|
153 |
|
|
int j2 = 0;
|
154 |
|
|
for (j = 0; j < f->bb[b].ninsn; j++)
|
155 |
|
|
if (f->bb[b].insn[i].index == f->bb[b].insn[j].index) {
|
156 |
|
|
int ok = 1;
|
157 |
|
|
for (j2 = 0; j2 < MAX_OPERANDS; j2++) if (!(f->bb[b].insn[j].opt[j2] & OPT_REF))
|
158 |
|
|
if (f->bb[b].insn[j].opt[j2] != f->bb[b].insn[i].opt[j2]
|
159 |
|
|
|| f->bb[b].insn[j].op[j2] != f->bb[b].insn[i].opt[j2]) {
|
160 |
|
|
ok = 0;
|
161 |
|
|
break;
|
162 |
|
|
}
|
163 |
|
|
if (ok) {
|
164 |
|
|
cntc++;
|
165 |
|
|
sizec = sizec + insn_size (&f->bb[b].insn[j]);
|
166 |
|
|
} else {
|
167 |
|
|
cnt++;
|
168 |
|
|
size = size + insn_size (&f->bb[b].insn[j]);
|
169 |
|
|
}
|
170 |
|
|
iteration[j] = 0;
|
171 |
|
|
} else iteration[j] = -1;
|
172 |
|
|
if (cntc > 1) {
|
173 |
|
|
assert (list = (cuc_shared *)malloc (sizeof (cuc_shared)));
|
174 |
|
|
list->next = main_list;
|
175 |
|
|
list->from = NULL;
|
176 |
|
|
list->ref = REF (b, i);
|
177 |
|
|
list->cnt = cnt;
|
178 |
|
|
list->cmatch = 1;
|
179 |
|
|
list->cmovs = count_cmovs (&f->bb[b].insn[i], 3);
|
180 |
|
|
list->osize = sizec;
|
181 |
|
|
list->size = ii_size (f->bb[b].insn[i].index, 1);
|
182 |
|
|
main_list = list;
|
183 |
|
|
search_csm (0, f, list);
|
184 |
|
|
}
|
185 |
|
|
if (cnt > 1) {
|
186 |
|
|
assert (list = (cuc_shared *)malloc (sizeof (cuc_shared)));
|
187 |
|
|
list->next = main_list;
|
188 |
|
|
list->from = NULL;
|
189 |
|
|
list->ref = REF (b, i);
|
190 |
|
|
list->cnt = cnt + cntc;
|
191 |
|
|
list->cmatch = 0;
|
192 |
|
|
list->cmovs = count_cmovs (&f->bb[b].insn[i], 2);
|
193 |
|
|
list->osize = size + sizec;
|
194 |
|
|
list->size = ii_size (f->bb[b].insn[i].index, 0);
|
195 |
|
|
main_list = list;
|
196 |
|
|
search_csm (0, f, list);
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
|
|
free (iteration);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
for (list = main_list; list; list = list->next) list->dead = 0;
|
203 |
|
|
cnt = 0;
|
204 |
|
|
for (list = main_list; list; list = list->next) if (!list->dead) cnt++;
|
205 |
|
|
cucdebug (1, "noptions = %i\n", cnt);
|
206 |
|
|
|
207 |
|
|
/* Now we will check the real size of the 'improvements'; if the size
|
208 |
|
|
actually increases, we abandom the option */
|
209 |
|
|
for (list = main_list; list; list = list->next)
|
210 |
|
|
if (list->cmovs * ii_size (II_CMOV, 0) * (list->cnt - 1) + list->size >= list->osize) list->dead = 1;
|
211 |
|
|
|
212 |
|
|
cnt = 0;
|
213 |
|
|
for (list = main_list; list; list = list->next) if (!list->dead) cnt++;
|
214 |
|
|
cucdebug (1, "noptions = %i\n", cnt);
|
215 |
|
|
|
216 |
|
|
/* Count number of instructions grouped */
|
217 |
|
|
for (list = main_list; list; list = list->next) {
|
218 |
|
|
cuc_shared *l = list;
|
219 |
|
|
int c = 0;
|
220 |
|
|
while (l) {
|
221 |
|
|
c++;
|
222 |
|
|
if (f->INSN(l->ref).type & (IT_VOLATILE | IT_MEMORY | IT_MEMADD)) list->dead = 1;
|
223 |
|
|
l = l->from;
|
224 |
|
|
}
|
225 |
|
|
list->ninsn = c;
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
cnt = 0;
|
229 |
|
|
for (list = main_list; list; list = list->next)
|
230 |
|
|
if (!list->dead) cnt++;
|
231 |
|
|
cucdebug (1, "noptions = %i\n", cnt);
|
232 |
|
|
|
233 |
|
|
#if 1
|
234 |
|
|
/* We can get a lot of options here, so we will delete duplicates */
|
235 |
|
|
for (list = main_list; list; list = list->next) if (!list->dead) {
|
236 |
|
|
cuc_shared *l;
|
237 |
|
|
for (l = list->next; l; l = l->next) if (!l->dead) {
|
238 |
|
|
int ok = 1;
|
239 |
|
|
cuc_shared *t1 = list;
|
240 |
|
|
cuc_shared *t2 = l;
|
241 |
|
|
while (ok && t1 && t2) {
|
242 |
|
|
if (f->INSN(t1->ref).index == f->INSN(t2->ref).index) {
|
243 |
|
|
/* If other operands are matching, we must check for them also */
|
244 |
|
|
if (t1->cmatch) {
|
245 |
|
|
int j;
|
246 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
247 |
|
|
if (!(f->INSN(t1->ref).opt[j] & OPT_REF) || !(f->INSN(t2->ref).opt[j] & OPT_REF)
|
248 |
|
|
|| f->INSN(t1->ref).opt[j] != f->INSN(t2->ref).opt[j]
|
249 |
|
|
|| f->INSN(t1->ref).op[j] != f->INSN(t2->ref).op[j]) {
|
250 |
|
|
ok = 0;
|
251 |
|
|
break;
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
/* This option is duplicate, remove */
|
256 |
|
|
if (ok) t1->dead = 1;
|
257 |
|
|
}
|
258 |
|
|
t1 = t1->from;
|
259 |
|
|
t2 = t2->from;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
cnt = 0;
|
264 |
|
|
for (list = main_list; list; list = list->next) if (!list->dead) cnt++;
|
265 |
|
|
cucdebug (1, "noptions = %i\n", cnt);
|
266 |
|
|
#endif
|
267 |
|
|
/* Print out */
|
268 |
|
|
for (list = main_list; list; list = list->next) if (!list->dead) {
|
269 |
|
|
cuc_shared *l = list;
|
270 |
|
|
cucdebug (1, "%-4s cnt %3i ninsn %3i size %8.1f osize %8.1f cmovs %3i @",
|
271 |
|
|
cuc_insn_name (&f->INSN(list->ref)), list->cnt, list->ninsn,
|
272 |
|
|
list->cmovs * ii_size (II_CMOV, 0) * (list->cnt - 1) + list->size, list->osize, list->cmovs);
|
273 |
|
|
while (l) {
|
274 |
|
|
cucdebug (1, "%c%x,", l->cmatch ? '.' : '!', l->ref);
|
275 |
|
|
l = l->from;
|
276 |
|
|
}
|
277 |
|
|
cucdebug (1, "\n");
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
/* Calculate estimated timings */
|
281 |
|
|
for (b = 0; b < f->num_bb; b++) {
|
282 |
|
|
cnt = 0;
|
283 |
|
|
for (list = main_list; list; list = list->next)
|
284 |
|
|
if (!list->dead && REF_BB(list->ref) == b) cnt++;
|
285 |
|
|
|
286 |
|
|
f->bb[b].ntim = cnt;
|
287 |
|
|
if (!cnt) {
|
288 |
|
|
f->bb[b].tim = NULL;
|
289 |
|
|
continue;
|
290 |
|
|
}
|
291 |
|
|
assert (f->bb[b].tim = (cuc_timings *)malloc (sizeof (cuc_timings) * cnt));
|
292 |
|
|
|
293 |
|
|
cnt = 0;
|
294 |
|
|
for (list = main_list; list; list = list->next) if (!list->dead && REF_BB(list->ref) == b) {
|
295 |
|
|
cuc_shared *l = list;
|
296 |
|
|
f->bb[b].tim[cnt].b = b;
|
297 |
|
|
f->bb[b].tim[cnt].preroll = f->bb[b].tim[cnt].unroll = 1;
|
298 |
|
|
f->bb[b].tim[cnt].nshared = list->ninsn;
|
299 |
|
|
assert (f->bb[b].tim[cnt].shared = (int *) malloc (sizeof(int) * list->ninsn));
|
300 |
|
|
for (i = 0; i < list->ninsn; i++, l = l->from) f->bb[b].tim[cnt].shared[i] = l->ref;
|
301 |
|
|
f->bb[b].tim[cnt].new_time = timings.new_time + f->bb[b].cnt * (list->cnt - 1);
|
302 |
|
|
f->bb[b].tim[cnt].size = timings.size + list->cmovs * ii_size (II_CMOV, 0) * (list->cnt - 1) + list->size - list->osize;
|
303 |
|
|
cnt++;
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
/* Recursive function for searching through instruction graph */
|
309 |
|
|
static void search_csm (int iter, cuc_func *f, cuc_shared *list)
|
310 |
|
|
{
|
311 |
|
|
int b, i, j, i1;
|
312 |
|
|
cuc_shared *l;
|
313 |
|
|
b = REF_BB(list->ref);
|
314 |
|
|
i = REF_I(list->ref);
|
315 |
|
|
|
316 |
|
|
for (j = 0; j < MAX_OPERANDS; j++) if (f->bb[b].insn[i].opt[j] & OPT_REF) {
|
317 |
|
|
int t = f->bb[b].insn[i].op[j];
|
318 |
|
|
int cnt = 0, cntc = 0;
|
319 |
|
|
double size = 0., sizec = 0.;
|
320 |
|
|
|
321 |
|
|
/* Mark neighbours */
|
322 |
|
|
for (i1 = 0; i1 < f->bb[b].ninsn; i1++) {
|
323 |
|
|
if (iteration[i1] == iter && f->bb[b].insn[i1].opt[j] & OPT_REF) {
|
324 |
|
|
int t2 = f->bb[b].insn[i1].op[j];
|
325 |
|
|
if (f->INSN(t).index == f->INSN(t2).index && f->INSN(t2).opt[j] & OPT_REF) {
|
326 |
|
|
int j2;
|
327 |
|
|
int ok = 1;
|
328 |
|
|
iteration[REF_I(t2)] = iter + 1;
|
329 |
|
|
for (j2 = 0; j2 < MAX_OPERANDS; j2++) if (!(f->bb[b].insn[i1].opt[j2] & OPT_REF))
|
330 |
|
|
if (f->bb[b].insn[i1].opt[j2] != f->bb[b].insn[i].opt[j2]
|
331 |
|
|
|| f->bb[b].insn[i1].op[j2] != f->bb[b].insn[i].opt[j2]) {
|
332 |
|
|
ok = 0;
|
333 |
|
|
break;
|
334 |
|
|
}
|
335 |
|
|
if (ok) {
|
336 |
|
|
cntc++;
|
337 |
|
|
sizec = sizec + insn_size (&f->bb[b].insn[i1]);
|
338 |
|
|
} else {
|
339 |
|
|
cnt++;
|
340 |
|
|
size = size + insn_size (&f->bb[b].insn[i1]);
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
}
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
if (cntc > 1) {
|
347 |
|
|
assert (l = (cuc_shared *)malloc (sizeof (cuc_shared)));
|
348 |
|
|
l->next = main_list;
|
349 |
|
|
main_list = l;
|
350 |
|
|
l->from = list;
|
351 |
|
|
l->ref = t;
|
352 |
|
|
l->cnt = cnt;
|
353 |
|
|
l->cmatch = 1;
|
354 |
|
|
l->cmovs = list->cmovs + count_cmovs (&f->bb[b].insn[i], 1) - 1;
|
355 |
|
|
l->size = list->size + ii_size (f->bb[b].insn[i].index, 1);
|
356 |
|
|
l->osize = sizec;
|
357 |
|
|
search_csm (iter + 1, f, l);
|
358 |
|
|
}
|
359 |
|
|
if (cnt > 1) {
|
360 |
|
|
assert (l = (cuc_shared *)malloc (sizeof (cuc_shared)));
|
361 |
|
|
l->next = main_list;
|
362 |
|
|
main_list = l;
|
363 |
|
|
l->from = list;
|
364 |
|
|
l->ref = t;
|
365 |
|
|
l->cnt = cnt + cntc;
|
366 |
|
|
l->cmatch = 0;
|
367 |
|
|
l->osize = size + sizec;
|
368 |
|
|
l->cmovs = list->cmovs + count_cmovs (&f->bb[b].insn[i], 0) - 1;
|
369 |
|
|
l->size = list->size + ii_size (f->bb[b].insn[i].index, 0);
|
370 |
|
|
search_csm (iter + 1, f, l);
|
371 |
|
|
}
|
372 |
|
|
|
373 |
|
|
/* Unmark them back */
|
374 |
|
|
for (i1 = 0; i1 < f->bb[b].ninsn; i1++) if (iteration[i1] > iter) iteration[i1] = -1;
|
375 |
|
|
}
|
376 |
|
|
}
|
377 |
|
|
|