-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.c
186 lines (172 loc) · 3.2 KB
/
copy.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
#include "all.h"
typedef struct RList RList;
struct RList {
int t;
RList *l;
};
static Ref
copyof(Ref r, Ref *cp)
{
if (rtype(r) == RTmp)
return cp[r.val];
else
return r;
}
static void
update(Ref r, Ref rcp, Ref *cp, RList ***pw)
{
RList *l;
if (!req(cp[r.val], rcp)) {
cp[r.val] = rcp;
l = emalloc(sizeof *l);
l->t = r.val;
l->l = 0;
**pw = l;
*pw = &l->l;
}
}
static void
visitphi(Phi *p, Ref *cp, RList ***pw)
{
uint a;
Ref r, r1;
r = R;
for (a=0; a<p->narg; a++) {
r1 = copyof(p->arg[a], cp);
if (req(r1, R) || req(r1, p->to))
continue;
if (req(r, R) || req(r, r1))
r = r1;
else {
r = p->to;
break;
}
}
update(p->to, r, cp, pw);
}
static int
iscopy(Ins *i, Ref r, Fn *fn)
{
static bits extcpy[] = {
[WFull] = 0,
[Wsb] = BIT(Wsb) | BIT(Wsh) | BIT(Wsw),
[Wub] = BIT(Wub) | BIT(Wuh) | BIT(Wuw),
[Wsh] = BIT(Wsh) | BIT(Wsw),
[Wuh] = BIT(Wuh) | BIT(Wuw),
[Wsw] = BIT(Wsw),
[Wuw] = BIT(Wuw),
};
bits b;
Tmp *t;
if (i->op == Ocopy)
return 1;
if (!isext(i->op) || rtype(r) != RTmp)
return 0;
if (i->op == Oextsw || i->op == Oextuw)
if (i->cls == Kw)
return 1;
t = &fn->tmp[r.val];
assert(KBASE(t->cls) == 0);
if (i->cls == Kl && t->cls == Kw)
return 0;
b = extcpy[t->width];
return (BIT(Wsb + (i->op-Oextsb)) & b) != 0;
}
static void
visitins(Ins *i, Ref *cp, RList ***pw, Fn *fn)
{
Ref r;
r = copyof(i->arg[0], cp);
if (iscopy(i, r, fn)) {
update(i->to, r, cp, pw);
} else if (!req(i->to, R)) {
assert(rtype(i->to) == RTmp);
update(i->to, i->to, cp, pw);
}
}
static void
subst(Ref *r, Ref *cp)
{
assert((rtype(*r) != RTmp || !req(copyof(*r, cp), R)) && "ssa invariant broken");
*r = copyof(*r, cp);
}
void
copy(Fn *fn)
{
Blk *b;
Ref *cp, r;
RList *w, *w1, **pw;
Use *u, *u1;
Ins *i;
Phi *p, **pp;
uint a;
int t;
w = 0;
pw = &w;
cp = emalloc(fn->ntmp * sizeof cp[0]);
for (b=fn->start; b; b=b->link) {
for (p=b->phi; p; p=p->link)
visitphi(p, cp, &pw);
for (i=b->ins; i-b->ins < b->nins; i++)
visitins(i, cp, &pw, fn);
}
while ((w1=w)) {
t = w->t;
u = fn->tmp[t].use;
u1 = u + fn->tmp[t].nuse;
for (; u<u1; u++)
switch (u->type) {
case UPhi:
visitphi(u->u.phi, cp, &pw);
break;
case UIns:
visitins(u->u.ins, cp, &pw, fn);
break;
case UJmp:
break;
default:
die("invalid use %d", u->type);
}
w = w->l;
free(w1);
}
for (b=fn->start; b; b=b->link) {
for (pp=&b->phi; (p=*pp);) {
r = cp[p->to.val];
if (!req(r, p->to)) {
*pp = p->link;
continue;
}
for (a=0; a<p->narg; a++)
subst(&p->arg[a], cp);
pp=&p->link;
}
for (i=b->ins; i-b->ins < b->nins; i++) {
r = copyof(i->to, cp);
if (!req(r, i->to)) {
*i = (Ins){.op = Onop};
continue;
}
for (a=0; a<2; a++)
subst(&i->arg[a], cp);
}
subst(&b->jmp.arg, cp);
}
if (debug['C']) {
fprintf(stderr, "\n> Copy information:");
for (t=Tmp0; t<fn->ntmp; t++) {
if (req(cp[t], R)) {
fprintf(stderr, "\n%10s not seen!",
fn->tmp[t].name);
}
else if (!req(cp[t], TMP(t))) {
fprintf(stderr, "\n%10s copy of ",
fn->tmp[t].name);
printref(cp[t], fn, stderr);
}
}
fprintf(stderr, "\n\n> After copy elimination:\n");
printfn(fn, stderr);
}
free(cp);
}