-
Notifications
You must be signed in to change notification settings - Fork 32
/
reg.c
288 lines (270 loc) · 6.29 KB
/
reg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* neatcc global register allocation */
#include <stdio.h>
#include <stdlib.h>
#include "ncc.h"
#define IC_LLD(ic, i) (O_C((ic)[i].op) == (O_LD | O_LOC) ? (ic)[i].a1 : -1)
#define IC_LST(ic, i) (O_C((ic)[i].op) == (O_ST | O_LOC) ? (ic)[i].a2 : -1)
static int ic_loc(struct ic *ic, long iv, long *loc, long *off)
{
long oc = O_C(ic[iv].op);
if (oc == (O_LD | O_LOC) || oc == (O_MOV | O_LOC)) {
*loc = ic[iv].a1;
*off = ic[iv].a2;
return 0;
}
if (oc == (O_ST | O_LOC)) {
*loc = ic[iv].a2;
*off = ic[iv].a3;
return 0;
}
return 1;
}
/* local live region */
struct rgn {
long loc; /* local number */
long beg; /* region start (instruction number) */
long end; /* region end */
long cnt; /* number of accesses */
int reg; /* register allocated to this region */
};
static struct rgn *rgn; /* live regions */
static int rgn_n; /* number of entries in rgn[] */
static int rgn_sz; /* size of rgn[] */
static int *loc_ptr; /* if the address of locals is accessed */
static int loc_n; /* number of locals */
static long *dst_head; /* lists of jumps to each instruction */
static long *dst_next; /* next entries in dst_head[] lists */
static void rgn_add(long loc, long beg, long end, long cnt)
{
int i;
for (i = 0; i < rgn_n; i++) {
if (rgn[i].loc == loc && rgn[i].beg < end && rgn[i].end > beg) {
if (beg > rgn[i].beg)
beg = rgn[i].beg;
if (end < rgn[i].end)
end = rgn[i].end;
cnt += rgn[i].cnt;
rgn[i].loc = -1;
}
}
for (i = 0; i < rgn_n; i++)
if (rgn[i].loc < 0)
break;
if (i == rgn_n) {
if (rgn_n >= rgn_sz) {
rgn_sz = MAX(16, rgn_sz * 2);
rgn = mextend(rgn, rgn_n, rgn_sz, sizeof(rgn[0]));
}
rgn_n++;
}
rgn[i].loc = loc;
rgn[i].beg = beg;
rgn[i].end = end;
rgn[i].cnt = cnt;
rgn[i].reg = -1;
}
/* return nonzero if register reg is free from beg till end */
static int rgn_available(long beg, long end, int reg)
{
int i;
for (i = 0; i < rgn_n; i++)
if (rgn[i].reg == reg)
if (rgn[i].beg < end && rgn[i].end > beg)
return 0;
return 1;
}
static long reg_region(struct ic *ic, long ic_n, long loc, long pos,
long *beg, long *end, char *mark)
{
long cnt = 0;
long dst;
for (; pos >= 0; pos--) {
if (pos < *beg)
*beg = pos;
if (pos + 1 > *end)
*end = pos + 1;
if (mark[pos])
break;
mark[pos] = 1;
if (IC_LST(ic, pos) == loc)
break;
if (IC_LLD(ic, pos) == loc)
cnt++;
dst = dst_head[pos];
while (dst >= 0) {
cnt += reg_region(ic, ic_n, loc, dst, beg, end, mark);
dst = dst_next[dst];
}
if (pos > 0 && ic[pos - 1].op & O_JMP)
break;
}
return cnt;
}
/* compute local's live regions */
static void reg_regions(struct ic *ic, long ic_n, long loc)
{
char *mark;
long beg, end;
long cnt;
long i;
mark = calloc(ic_n, sizeof(mark[0]));
for (i = 0; i < ic_n; i++) {
if (IC_LLD(ic, i) == loc && !mark[i]) {
beg = i;
end = i + 1;
cnt = reg_region(ic, ic_n, loc, i, &beg, &end, mark);
rgn_add(loc, beg, end, cnt);
}
}
for (i = 0; i < ic_n; i++)
if (IC_LST(ic, i) == loc && !mark[i])
rgn_add(loc, i, i + 1, 1);
free(mark);
}
/* number of times a local is accessed */
static long reg_loccnt(struct ic *ic, long ic_n, long loc)
{
long cnt = 0;
long i;
for (i = 0; i < ic_n; i++)
if (IC_LLD(ic, i) == loc || IC_LST(ic, i) == loc)
cnt++;
return cnt;
}
/* perform global register allocation */
static void reg_glob(int leaf)
{
int *srt;
int regs[N_REGS];
int i, j;
int regs_max = MIN(N_TMPS >> 1, 4);
long regs_mask = leaf ? R_TMPS : R_PERM;
int regs_n = 0;
for (i = leaf ? 1 : 3; i < N_TMPS && regs_n < regs_max; i++)
if ((1 << i) & regs_mask)
regs[regs_n++] = i;
srt = malloc(rgn_n * sizeof(srt[0]));
/* sorting locals */
for (i = 0; i < rgn_n; i++) {
for (j = i - 1; j >= 0 && rgn[i].cnt > rgn[srt[j]].cnt; j--)
srt[j + 1] = srt[j];
srt[j + 1] = i;
}
/* allocating registers */
for (i = 0; i < rgn_n; i++) {
int r = srt[i];
long loc = rgn[r].loc;
long beg = rgn[r].beg;
long end = rgn[r].end;
if (loc < 0 || loc_ptr[loc])
continue;
if (leaf && loc < N_ARGS && beg == 0 &&
rgn_available(beg, end, argregs[loc])) {
rgn[r].reg = argregs[loc];
continue;
}
for (j = 0; j < regs_n; j++)
if (rgn_available(beg, end, regs[j]))
break;
if (j < regs_n)
rgn[r].reg = regs[j];
}
free(srt);
}
void reg_init(struct ic *ic, long ic_n)
{
long loc, off;
int *loc_sz;
int leaf = 1;
long i;
for (i = 0; i < ic_n; i++)
if (ic[i].op & O_LOC && !ic_loc(ic, i, &loc, &off))
if (loc + 1 >= loc_n)
loc_n = loc + 1;
loc_ptr = calloc(loc_n, sizeof(loc_ptr[0]));
loc_sz = calloc(loc_n, sizeof(loc_sz[0]));
for (i = 0; i < loc_n; i++)
loc_ptr[i] = !opt(1);
for (i = 0; i < ic_n; i++) {
long oc = O_C(ic[i].op);
if (ic_loc(ic, i, &loc, &off))
continue;
if (oc == (O_LD | O_LOC) || oc == (O_ST | O_LOC)) {
int sz = T_SZ(O_T(ic[i].op));
if (!loc_sz[loc])
loc_sz[loc] = sz;
if (off || sz < 2 || sz != loc_sz[loc])
loc_ptr[loc]++;
}
if (oc == (O_MOV | O_LOC))
loc_ptr[loc]++;
}
free(loc_sz);
for (i = 0; i < ic_n; i++)
if (ic[i].op & O_CALL)
leaf = 0;
dst_head = malloc(ic_n * sizeof(dst_head[0]));
dst_next = malloc(ic_n * sizeof(dst_next[0]));
for (i = 0; i < ic_n; i++)
dst_head[i] = -1;
for (i = 0; i < ic_n; i++)
dst_next[i] = -1;
for (i = 0; i < ic_n; i++) {
if (ic[i].op & O_JXX) {
dst_next[i] = dst_head[ic[i].a3];
dst_head[ic[i].a3] = i;
}
}
for (i = 0; i < loc_n; i++) {
if (!loc_ptr[i] && opt(2))
reg_regions(ic, ic_n, i);
if (!loc_ptr[i] && !opt(2))
rgn_add(i, 0, ic_n, reg_loccnt(ic, ic_n, i));
}
reg_glob(leaf);
}
long reg_mask(void)
{
long ret = 0;
int i;
for (i = 0; i < rgn_n; i++)
if (rgn[i].reg >= 0)
ret |= 1 << rgn[i].reg;
return ret;
}
/* return the allocated register of local loc */
int reg_lmap(long c, long loc)
{
int i;
for (i = 0; i < rgn_n; i++)
if (rgn[i].loc == loc)
if (rgn[i].beg <= c && rgn[i].end > c)
return rgn[i].reg;
return -1;
}
/* return the local to which register reg is allocated */
int reg_rmap(long c, long reg)
{
int i;
for (i = 0; i < rgn_n; i++)
if (rgn[i].reg == reg)
if (rgn[i].beg <= c && rgn[i].end > c)
return rgn[i].loc;
return -1;
}
void reg_done(void)
{
free(dst_head);
free(dst_next);
free(loc_ptr);
free(rgn);
loc_ptr = NULL;
rgn = NULL;
rgn_sz = 0;
rgn_n = 0;
loc_n = 0;
}
int reg_safe(long loc)
{
return loc < loc_n && !loc_ptr[loc];
}