/
dead.cc
321 lines (305 loc) · 8.9 KB
/
dead.cc
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*-Mode: c++;-*-
Copyright (c) 2004-2013 John Plevyak, All Rights Reserved
*/
#include "ast.h"
#include "builtin.h"
#include "dom.h"
#include "fa.h"
#include "fail.h"
#include "fun.h"
#include "if1.h"
#include "ifa.h"
#include "ifadefs.h"
#include "pattern.h"
#include "pdb.h"
#include "pnode.h"
#include "prim.h"
#include "var.h"
/*
It is intended that this file contain 2 implementations,
one based on the AVars and another based on Vars (post concretize_program()).
Only the former is currently extent.
*/
static int mark_live_again = 0;
static void print_dead(FA *fa) {
int ndead_pnodes = 0, ndead_vars = 0;
int nlive_pnodes = 0, nlive_vars = 0;
char lfn[512];
strcpy(lfn, fa->fn);
strcat(lfn, ".dead_log");
FILE *fp = fopen(lfn, "w");
fa_dump_types(fa, fp);
forv_Fun(f, fa->funs) {
forv_PNode(p, f->fa_all_PNodes) if (!p->live) {
ndead_pnodes++;
fprintf(fp, "PNode %d %s:%d DEAD\n", p->id, p->code->filename(), p->code->line());
}
else nlive_pnodes++;
forv_Var(v, f->fa_all_Vars) if (!v->live) {
ndead_vars++;
fprintf(fp, "Var %d:%d %s %s:%d DEAD\n", v->sym->id, v->id, v->sym->name ? v->sym->name : "", v->sym->filename(),
v->sym->line());
}
else nlive_vars++;
}
fprintf(fp, "%d PNodes DEAD\n", ndead_pnodes);
fprintf(fp, "%d Vars DEAD\n", ndead_vars);
fprintf(fp, "%d PNodes LIVE\n", nlive_pnodes);
fprintf(fp, "%d Vars LIVE\n", nlive_vars);
fclose(fp);
}
static void mark_live_avar(AVar *av) {
if (av->var->sym->is_fake) return;
av->live = 1;
av->var->live = 1;
mark_live_again = 1;
if (av->var->def) av->var->def->live = 1;
forv_AVar(aav, av->backward) if (aav) {
if (!aav->live && !constant(aav)) mark_live_avar(aav);
}
}
static void mark_live_avars(FA *fa) {
forv_Fun(f, fa->funs) {
forv_PNode(p, f->fa_all_PNodes) {
// if a pnode is live, and it is not a function call, it's inputs (rvals)
// must be live
if (p->live && !f->calls.get(p)) {
forv_Var(v, p->rvals) if (!v->constant) {
form_AVarMapElem(x, v->avars) {
AVar *av = x->value;
if (!av->live) mark_live_avar(av);
}
}
}
}
forv_Var(v, f->fa_all_Vars) {
if (!v->constant) {
form_AVarMapElem(x, v->avars) {
// if an instance variable is live, then the containing object must be
// live
AVar *av = x->value;
if (!av->live) {
forv_CreationSet(cs, *av->out) if (cs) {
forv_AVar(iv, cs->vars) {
if (iv->live) {
mark_live_avar(av);
goto Lbreak2;
}
}
}
}
Lbreak2:;
}
}
}
}
}
static bool forward_live(Var *v, int dist = 2) {
if (dist < 0) return false;
form_AVarMapElem(x, v->avars) forv_AVar(av, x->value->forward) if (av) {
if (av->live) return true;
if (forward_live(av->var, dist - 1)) return true;
}
return false;
}
static void mark_live_pnodes(FA *fa) {
forv_Fun(f, fa->funs) {
forv_PNode(p, f->fa_all_PNodes) {
if (!p->live) {
forv_Var(v, p->lvals) if (v->live) goto Live;
if (p->code) switch (p->code->kind) {
default:
break;
case Code_LABEL:
if (p->cfg_pred.n != 1) forv_PNode(x, p->cfg_pred) if (x->live) goto Live;
break;
case Code_GOTO:
if (p->cfg_succ[0]->cfg_pred.n != 1)
forv_PNode(x, f->fa_all_PNodes) // potentially expensive
if (x->live || x == f->exit) if (p->dom->is_dominated_by(x->dom)) goto Live;
break;
case Code_SEND:
if (p->prim) {
switch (p->prim->index) { // handle side effects
default:
break;
case P_prim_reply:
forv_Var(v, f->rets) if (v->live) goto Live;
break;
case P_prim_setter: {
if (forward_live(p->tvals.v[0])) goto Live;
break;
}
case P_prim_set_index_object: {
if (forward_live(p->tvals.v[0])) goto Live;
break;
}
}
} else {
Vec<Fun *> *calls = f->calls.get(p);
if (calls) forv_Fun(x, *calls) if (x->live) goto Live;
}
break;
case Code_IF:
forv_PNode(x, f->fa_all_PNodes) // potentially expensive
if (x->live) if (x->dom->is_dominated_by(p->dom))
forv_PNode(s, p->cfg_succ) if (!x->dom->is_dominated_by(s->dom)) goto Live;
break;
}
continue; // handle fall through non-live
Live:
p->live = 1;
f->live = 1;
mark_live_again = 1;
} else if (!f->live) {
f->live = 1;
mark_live_again = 1;
}
}
}
}
static void mark_initial_dead_and_alive(FA *fa, int init = 0) {
forv_Fun(f, fa->funs) {
f->live = init;
forv_Var(v, f->fa_all_Vars) {
v->constant = constant(v);
v->live = init;
for (int i = 0; i < v->avars.n; i++)
if (v->avars[i].key) v->avars[i].value->live = false;
if (v->type) { // mark all instance variables not live
forv_CreationSet(cs, v->type->creators) if (cs) { forv_AVar(iv, cs->vars) iv->var->live = init; }
}
}
forv_PNode(p, f->fa_all_PNodes) {
p->live = init;
if (p->code && p->code->kind == Code_SEND && p->prim && p->prim->index == P_prim_primitive) {
cchar *name = p->code->rvals[1]->name;
RegisteredPrim *rp = prim_get(name);
if (rp && rp->is_visible) p->live = 1;
}
}
}
}
void mark_live_types(FA *fa) {
forv_CreationSet(cs, fa->css) cs->type->type_live = 0;
forv_CreationSet(cs, fa->css) if (cs->type && cs->type->creators.n) {
forv_CreationSet(x, cs->type->creators) {
forv_AVar(av, x->defs) if (av) if (av->var->live && !cs->type->type_live) cs->type->type_live = 1;
}
}
}
int mark_live_code(FA *fa) {
mark_initial_dead_and_alive(fa);
do {
mark_live_again = 0;
mark_live_avars(fa);
mark_live_pnodes(fa);
} while (mark_live_again);
mark_live_types(fa);
if (ifa_verbose > 2) print_dead(fa);
return 0;
}
void mark_live_funs(FA *fa) {
forv_Fun(f, fa->funs) f->live = 0;
if1->top->fun->live = 1;
int changed = 1;
while (changed) {
changed = 0;
forv_Fun(f, fa->funs) {
if (f->live) {
forv_PNode(p, f->fa_all_PNodes) if (p->live) {
Vec<Fun *> *fns = f->calls.get(p);
if (fns) forv_Fun(x, *fns) if (!x->live) {
x->live = 1;
changed = 1;
}
}
}
}
}
Vec<Fun *> funs(fa->funs, Vec<Fun *>::MOVE);
forv_Fun(f, funs) if (f->live) fa->funs.add(f);
}
/*
INCOMPLETE VAR BASED VERSION
*/
#if 0
static void
mark_live_vars(FA *fa) {
forv_Fun(f, fa->funs) {
forv_PNode(p, f->fa_all_PNodes) {
// if a pnode is live, it's inputs (rvals) must be live
if (p->live) {
forv_Var(v, p->rvals) {
if (!v->live) {
v->live = 1;
mark_live_again = 1;
}
}
}
forv_Var(v, f->fa_all_Vars) {
// if an instance variable is live, then the containing object must be live
if (!v->live && v->type)
forv_CreationSet(cs, v->type->creators) if (cs) {
forv_AVar(iv, cs->vars) {
if (iv->var->live) {
v->live = 1;
mark_live_again = 1;
}
}
}
}
}
}
}
static void
mark_types_live(FA *fa) {
forv_CreationSet(cs, fa->css)
if (cs->type && cs->type->creators.n) {
forv_CreationSet(x, cs->type->creators) {
forv_AVar(av, x->defs) if (av)
if (av->var->live)
cs->type->type_live = 1;
}
}
}
static void
mark_initial_dead_and_alive(FA *fa, int init = 0) {
forv_Fun(f, fa->funs) {
f->live = init;
forv_Var(v, f->fa_all_Vars) {
v->live = INIT;
if (v->type) { // mark all instance variables not live
forv_CreationSet(cs, v->type->creators) if (cs) {
forv_AVar(iv, cs->vars)
iv->var->live = init;
}
}
}
forv_PNode(p, f->fa_all_PNodes) {
p->live = init;
if (p->code && p->code->kind == Code_SEND && p->prim && p->prim->index == P_prim_primitive) {
cchar *name = p->code->rvals[1]->name;
RegisteredPrim *rp = pdb->fa->primitive_transfer_functions.get(name);
if (rp && rp->is_visible)
p->live = 1;
}
}
}
forv_CreationSet(cs, fa->css)
cs->type->type_live = init;
}
int
mark_dead_code(FA *fa) {
mark_initial_dead_and_alive(fa);
do {
mark_live_again = 0;
mark_live_vars(fa);
mark_live_pnodes(fa);
} while (mark_live_again);
mark_types_live(fa);
if (ifa_verbose > 2)
print_dead(fa);
return 0;
}
#endif