-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_data_flow_analysis.cpp
288 lines (199 loc) · 5.84 KB
/
opt_data_flow_analysis.cpp
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
/* data flow analysis */
#include "types.h"
#include "common.h"
#include "Expression.h"
#include "cxx/deque.h"
template<class UnaryFunction>
void for_each(dp_register r, unsigned reg_count, UnaryFunction fx) {
for (unsigned i = 0; i < reg_count; ++i) {
fx(r);
r.number += 2;
}
}
template<class UnaryPredicate>
bool all_of(dp_register r, unsigned reg_count, UnaryPredicate fx) {
for (unsigned i = 0; i < reg_count; ++i) {
if (!fx(r)) return false;
r.number += 2;
}
return true;
}
template<class UnaryPredicate>
bool none_of(dp_register r, unsigned reg_count, UnaryPredicate fx) {
for (unsigned i = 0; i < reg_count; ++i) {
if (fx(r)) return false;
r.number += 2;
}
return true;
}
template<class UnaryPredicate>
bool any_of(dp_register r, unsigned reg_count, UnaryPredicate fx) {
for (unsigned i = 0; i < reg_count; ++i) {
if (fx(r)) return true;
r.number += 2;
}
return false;
}
void add_imports(BasicBlockPtr block, dp_register_set imports) {
imports -= block->dp_reg_export;
if (!imports) return;
if (block->dp_reg_import.includes(imports)) return;
block->dp_reg_import += imports;
for (auto prev : block->prev_set)
add_imports(prev, block->dp_reg_import);
}
void add_imports(BasicBlockPtr block, register_set imports) {
imports -= block->reg_export;
if (!imports) return;
if (block->reg_import.includes(imports)) return;
block->reg_import += imports;
for (auto prev : block->prev_set)
add_imports(prev, block->reg_import);
}
void import_export(BasicBlockPtr block) {
dp_register_set dp_reg_import;
dp_register_set dp_reg_export;
register_set reg_import;
register_set reg_export;
// if (block->label && *block->label == ".404") {
// printf("here\n");
// }
// exit nodes also need to export a/x (if cdecl and not void...)
#if 0
if (block->exit_node) {
reg_export += register_set(0x07); // magic!
reg_import += register_set(0x07); // magic!
}
#endif
for (BasicLinePtr line : block->lines) {
OpCode opcode = line->opcode;
line->calc_registers();
auto rs = line->read_registers();
auto ws = line->write_registers();
reg_import += (rs - reg_export);
reg_export += ws;
if (!rs.zp() && !ws.zp()) continue;
dp_register reg = line->reg;
// if it reads a dp register, mark it as imported...
// unless it was previously written by this block.
// (in which case it will be in the export set.
if (rs.zp()) {
for_each(line->reg, line->reg_count, [&dp_reg_import, &dp_reg_export](dp_register r){
if (!dp_reg_export.includes(r)) dp_reg_import += r;
});
//dp_reg_import += (registers - dp_reg_export)
}
// if it writes to a dp register, mark it as exported.
if (ws.zp()) {
for_each(line->reg, line->reg_count, [&dp_reg_export](dp_register r) { dp_reg_export += r; });
//dp_reg_export += registers;
}
}
// also need to add in the branch block.
BasicLinePtr &line = block->exit_branch;
if (line) {
OpCode opcode = line->opcode;
line->calc_registers();
auto rs = line->read_registers();
auto ws = line->write_registers();
reg_import += (rs - reg_export);
reg_export += ws;
// currently, branches don't read dp_registers.
}
block->dp_reg_import = dp_reg_import;
block->dp_reg_export = dp_reg_export;
block->reg_import = reg_import;
block->reg_export = reg_export;
// if (block->label && *block->label == ".404") {
// printf("imports: ");
// block->dp_reg_import.dump();
// printf("exports: ");
// block->dp_reg_import.dump();
// }
}
void build_import_export_set(BlockQueue &bq) {
for (auto block : bq) {
import_export(block);
if (block->exit_node) {
block->reg_import += register_set(0x07); // export a, x, y
block->reg_export += register_set(0x07); // export a, x, y
}
}
// now propogate the imports.
for (auto block : bq) {
const auto &dp_imports = block->dp_reg_import;
auto imports = block->reg_import;
if (!dp_imports && !imports) continue;
for (auto prev : block->prev_set) {
add_imports(prev, dp_imports);
add_imports(prev, imports);
}
}
// now remove unnecessary exports!
for (auto block : bq) {
dp_register_set dp_exports;
register_set exports;
for (auto next : block->next_set) {
dp_exports += next->dp_reg_import;
exports += next->reg_import;
}
if (block->exit_node) {
exports += register_set(0x07);
}
block->dp_reg_export &= dp_exports;
block->reg_export &= exports;
}
}
//struct register_pair { dp_register base, unsigned size = 0};
bool dataflow_analysis(BasicBlockPtr block) {
auto size = block->lines.size();
LineQueue tmp;
dp_register_set dp_live = block->dp_reg_export;
std::copy_if(block->lines.rbegin(), block->lines.rend(), std::front_inserter(tmp), [&dp_live, block](BasicLinePtr line){
OpCode opcode = line->opcode;
if (!opcode) return true;
auto rs = line->read_registers();
auto ws = line->write_registers();
dp_register reg = line->reg;
//if (block->label && *block->label == ".404" && opcode.mnemonic() == STA) {
// printf(".\n");
//}
if (ws.zp()) {
if (ws.p() == 0 && reg.is_temporary()) {
if (none_of(line->reg, line->reg_count, [&dp_live](dp_register r){
return dp_live.includes(r);
})) return false;
//if (!dp_live.includes(rp)) return false;
}
for_each(line->reg, line->reg_count, [&dp_live](dp_register r){
dp_live -= r;
});
//dp_live -= rp;
}
if (rs.zp()) {
// reading from a zp -- mark as live.
for_each(line->reg, line->reg_count, [&dp_live](dp_register r){
dp_live += r;
});
//dp_live += rp;
}
return true;
});
if (tmp.size() == size) return false;
block->lines = std::move(tmp);
block->dirty = true;
return true;
}
bool dataflow_analysis(BlockQueue &bq) {
unsigned any_delta = 0;
for(;;) {
unsigned delta = 0;
build_import_export_set(bq);
for (auto block : bq) {
delta += dataflow_analysis(block);
}
any_delta += delta;
if (!delta) break;
}
return any_delta;
}