-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize.h
301 lines (269 loc) · 9.82 KB
/
optimize.h
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
#pragma once
#include "instructions.h"
#include "analysis.h"
struct TempConstraints {
bool canBeNothing = false;
TempConstraints accumulateOr(TempConstraints t) {
return TempConstraints{canBeNothing || t.canBeNothing};
}
};
struct OptimizationCtx {
std::map<TempId, TempConstraints> constraints;
};
size_t findDefinition(CompilationResult* r, TempId id) {
size_t off = 0;
for (auto& instr : r->instructions) {
auto dest = getDest(instr);
bool isHere = dest && *dest == id;
if (isHere) {
return off;
}
++off;
}
return -1;
}
void replaceTemp(CompilationResult* r, TempId oldTemp, TempId newTemp) {
for (auto& instr : r->instructions) {
std::visit(
Overloaded{
[&](LInstrLoadConst& lc) {
if (lc.dst == oldTemp) {
lc.dst = newTemp;
}
},
[&](LInstrLoadSlot& lc) {
if (lc.dst == oldTemp) {
lc.dst = newTemp;
}
},
[&](LInstrAdd& a) {
if (a.dst == oldTemp) {
a.dst = newTemp;
}
if (a.left == oldTemp) {
a.left = newTemp;
}
if (a.right == oldTemp) {
a.right = newTemp;
}
},
[&](LInstrFillEmpty& a) {
if (a.dst == oldTemp) {
a.dst = newTemp;
}
if (a.left == oldTemp) {
a.left = newTemp;
}
if (a.right == oldTemp) {
a.right = newTemp;
}
},
[&](LInstrJmp& j) {
},
[&](LInstrMove& m) {
if (m.dst == oldTemp) {
m.dst = newTemp;
}
if (m.src == oldTemp) {
m.src = newTemp;
}
},
[&](LInstrMovePhi& m) {
assert(0);
},
[&](LInstrLabel& l) {
},
[&](LInstrTestTruthy& t) {
if (t.reg == oldTemp) {
t.reg = newTemp;
}
},
[&](LInstrTestFalsey& t) {
if (t.reg == oldTemp) {
t.reg = newTemp;
}
},
[&](LInstrTestNothing& t) {
if (t.reg == oldTemp) {
t.reg = newTemp;
}
}
},
instr);
}
}
void removePhi(CompilationResult* r) {
auto it = r->instructions.begin();
while (it != r->instructions.end()) {
auto instr = *it;
if (std::holds_alternative<LInstrMovePhi>(instr)) {
// Remove the phi instruction.
r->instructions.erase(it);
auto phiInstr = std::get<LInstrMovePhi>(instr);
for (auto src : phiInstr.sources) {
auto srcOffset = findDefinition(r, src);
r->instructions.insert(r->instructions.begin() + srcOffset + 1,
LInstrMove{phiInstr.dst, src});
// auto srcBOffset = findDefinition(r, phiInstr.srcB);
// r->instructions.insert(r->instructions.begin() + srcBOffset + 1,
// LInstrMove{phiInstr.dst, phiInstr.srcB});
}
// Iterator is invalidated now, just go to the beginning. Not efficient,
// but who cares?
it = r->instructions.begin();
} else {
++it;
}
}
}
void computeConstraints(OptimizationCtx* ctx, const CompilationResult* r) {
for (const auto& instr : r->instructions) {
std::visit(
Overloaded{
[&](LInstrLoadConst lc) {
ctx->constraints[lc.dst] = TempConstraints{lc.constVal.tag == kTagNothing};
},
[&](LInstrLoadSlot lc) {
ctx->constraints[lc.dst] = TempConstraints{true};
},
[&](LInstrAdd a) {
// Could be smarter about this.
ctx->constraints[a.dst] = TempConstraints{true};
},
[&](LInstrFillEmpty a) {
ctx->constraints[a.dst] = TempConstraints{
// If you do fillEmpty(a, Nothing), then you still get nothing.
ctx->constraints[a.right].canBeNothing
};
},
[&](LInstrJmp j) {
},
[&](LInstrMove m) {
assert(ctx->constraints.count(m.src));
ctx->constraints[m.dst] = ctx->constraints[m.src];
},
[&](LInstrMovePhi m) {
TempConstraints constraint;
for (auto& src : m.sources) {
assert(ctx->constraints.count(src));
constraint = constraint.accumulateOr(ctx->constraints[src]);
}
ctx->constraints[m.dst] = constraint;
},
[&](LInstrLabel l) {
},
[&](LInstrTestTruthy t) {
},
[&](LInstrTestFalsey t) {
},
[&](LInstrTestNothing t) {
}
},
instr);
}
}
struct OptimizationPass {
virtual bool run(OptimizationCtx* ctx, CompilationResult* r) = 0;
virtual ~OptimizationPass() = default;
};
struct DeadStorePass : public OptimizationPass {
bool run(OptimizationCtx* ctx, CompilationResult* r) {
bool didAnything = false;
auto it = r->instructions.begin();
while (it != r->instructions.end()) {
auto dest = getDest(*it);
// The temp representing the whole output is an exception.
if (dest && *dest != r->tempId && !isTempRead(r, *dest)) {
it = r->instructions.erase(it);
didAnything = true;
} else {
++it;
}
}
return didAnything;
}
};
struct RemoveRedundantNothingPass : public OptimizationPass {
bool run(OptimizationCtx* ctx, CompilationResult* r) {
bool didAnything = false;
auto it = r->instructions.begin();
while (it != r->instructions.end()) {
auto instr = *it;
if (auto testNothing = getAlternative<LInstrTestNothing>(instr)) {
if (!ctx->constraints[testNothing->reg].canBeNothing) {
// Erase this instruction and the following jump.
it = r->instructions.erase(it);
it = r->instructions.erase(it);
continue;
}
}
if (auto fillEmpty = getAlternative<LInstrFillEmpty>(instr)) {
if (!ctx->constraints[fillEmpty->left].canBeNothing) {
// Replace the fill empty with a simple move.
*it = LInstrMove{fillEmpty->dst, fillEmpty->left};
continue;
}
}
++it;
}
return didAnything;
}
};
void optimizePreSSA(OptimizationCtx* ctx, CompilationResult* r) {
computeConstraints(ctx, r);
std::cout << "Constraints generated:\n";
for (auto& [tempId, con] : ctx->constraints) {
std::cout << tmpStr(tempId) << " " <<
(con.canBeNothing ? "maybe-nothing" : "not-nothing") << std::endl;
}
std::vector<std::unique_ptr<OptimizationPass>> passes;
passes.push_back(std::make_unique<RemoveRedundantNothingPass>());
passes.push_back(std::make_unique<DeadStorePass>());
for (auto& p : passes) {
p->run(ctx, r);
}
// find move TA, TB instruction. If the definition of TB is available in this block then get rid
// of the move and use TA everywhere TB is used.
}
struct BasicCopyPropPass : public OptimizationPass {
bool run(OptimizationCtx* ctx, CompilationResult* r) {
bool didAnything = false;
auto it = r->instructions.begin();
while (it != r->instructions.end()) {
auto instr = *it;
if (std::holds_alternative<LInstrMove>(instr)) {
auto moveInstr = std::get<LInstrMove>(instr);
auto srcDefinition = findDefinition(r, moveInstr.src);
// Is there a jump between the definition and here?
auto it2 = r->instructions.begin() + srcDefinition;
bool foundJump = false;
while (it2 != it) {
if (std::holds_alternative<LInstrJmp>(*it2)) {
foundJump = true;
break;
}
++it2;
}
if (!foundJump) {
replaceTemp(r, moveInstr.src, moveInstr.dst);
it = r->instructions.erase(it);
didAnything = true;
} else {
++it;
}
} else {
++it;
}
}
return didAnything;
}
};
void optimizePostSSA(OptimizationCtx* ctx, CompilationResult* r) {
std::vector<std::unique_ptr<OptimizationPass>> passes;
passes.push_back(std::make_unique<BasicCopyPropPass>());
for (auto& p : passes) {
p->run(ctx, r);
}
// find move TA, TB instruction. If the definition of TB is available in this block then get rid
// of the move and use TA everywhere TB is used.
}