-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathoptimizer.cpp
More file actions
278 lines (255 loc) · 7.68 KB
/
optimizer.cpp
File metadata and controls
278 lines (255 loc) · 7.68 KB
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
#include "optimizer.h"
using namespace febcode;
Optimizer::Optimizer(Program& program) : Modifier(program) {}
void Optimizer::optimize()
{
AST& ast = *prg.ast;
for (int i = (int)ast.root.statements.size() - 1; i >= 0; --i)
{
Statement* stmt = ast.root.statements[i].get();
if (shouldRemove(stmt))
{
ast.root.statements.erase(ast.root.statements.begin() + i);
}
}
}
bool Optimizer::shouldRemove(Statement* stmt)
{
if (auto retStmt = dynamic_cast<ReturnStmt* >(stmt)) return shouldRemoveReturn (retStmt);
if (auto varDecl = dynamic_cast<VarDeclStmt* >(stmt)) return shouldRemoveVarDecl (varDecl);
if (auto exprStmt = dynamic_cast<ExpressionStmt*>(stmt)) return shouldRemoveExprStmt (exprStmt);
if (auto blckStmt = dynamic_cast<BlockStmt *>(stmt)) return shouldRemoveBlockStmt(blckStmt);
if (auto ifStmt = dynamic_cast<IfStmt *>(stmt)) return shouldRemoveIfStmt (ifStmt );
throw std::runtime_error("Unsupported statement type in optimizer");
}
bool Optimizer::shouldRemoveReturn(ReturnStmt* stmt)
{
updateLiveness(stmt->value.get());
// don't remove return statements.
return false;
}
bool Optimizer::shouldRemoveVarDecl(VarDeclStmt* stmt)
{
// remove unused variables.
for (int i = (int)stmt->vars.size() - 1; i >= 0; i--)
{
auto& var = stmt->vars[i];
if (live.find(var.name) == live.end())
{
if (assignedLater.find(var.name) != assignedLater.end())
{
// the variable is assigned later, so we can't remove the declaration,
// but we can remove the initializer if it exists.
assignedLater.erase(var.name);
var.initializer.reset();
}
else
stmt->vars.erase(stmt->vars.begin() + i);
}
else
{
live.erase(var.name);
updateLiveness(var.initializer.get());
}
}
return stmt->vars.empty();
}
bool Optimizer::shouldRemoveExprStmt(ExpressionStmt* stmt)
{
ExprPtr& expr = stmt->expr;
if (expr->exprType == ExpressionType::Assignment)
{
const auto* assignExpr = static_cast<const AssignExpr*>(expr.get());
if (assignExpr->target->exprType == ExpressionType::Variable)
{
const auto* varExpr = static_cast<const VariableExpr*>(assignExpr->target.get());
if (live.find(varExpr->name) == live.end())
{
// the variable is not live, so we can remove the assignment, but
// only if the value being assigned doesn't have side effects, otherwise we need to keep the assignment for the side effects.
if (hasSideEffects(assignExpr->value.get()))
{
assignedLater.insert(varExpr->name);
updateLiveness(assignExpr->value.get());
return false; // keep the assignment for the side effects, even though the variable is not live.
}
updateLiveness(assignExpr->value.get());
return !hasSideEffects(assignExpr->value.get()); // remove this statement if it doesn't have side effects.
}
else
{
assignedLater.insert(varExpr->name);
live.erase(varExpr->name);
updateLiveness(assignExpr->value.get());
return false;
}
}
if (assignExpr->target->exprType == ExpressionType::Member)
{
// for member assignments, we need to check if the object is live, since the assignment has side effects on the object.
const auto* memberExpr = static_cast<const MemberExpr*>(assignExpr->target.get());
updateLiveness(memberExpr->object.get());
updateLiveness(assignExpr->value.get());
return false; // don't remove member assignments, since they have side effects on the object.
}
}
updateLiveness(expr.get());
return !hasSideEffects(expr.get());
}
bool Optimizer::shouldRemoveBlockStmt(BlockStmt* blckStmt)
{
for (int i = (int)blckStmt->statements.size() - 1; i >= 0; --i)
{
Statement* stmt = blckStmt->statements[i].get();
if (shouldRemove(stmt))
{
blckStmt->statements.erase(blckStmt->statements.begin() + i);
}
}
return blckStmt->statements.empty();
}
bool Optimizer::shouldRemoveIfStmt(IfStmt* stmt)
{
auto liveBefore = live; // save live variables before processing branches
bool removeThen = shouldRemove(stmt->thenBranch.get());
auto liveThen = live;
bool removeElse = true;
if (stmt->elseBranch)
{
live = liveBefore; // reset live variables before processing else branch
bool removeElse = shouldRemove(stmt->elseBranch.get());
}
if (removeThen && removeElse && !hasSideEffects(stmt->condition.get()))
{
live = liveBefore; // restore live variables before processing branches
return true; // remove the entire if statement
}
else
{
// merge live variables from both branches
for (const auto& var : liveThen)
live.insert(var);
updateLiveness(stmt->condition.get());
// remove else branch if not needed
if (removeElse)
stmt->elseBranch.reset();
return false;
}
}
void Optimizer::updateLiveness(Expression* expr)
{
if (!expr) return;
switch (expr->exprType)
{
case ExpressionType::Literal:
break;
case ExpressionType::Variable:
{
const auto* varExpr = static_cast<const VariableExpr*>(expr);
live.insert(varExpr->name);
break;
}
case ExpressionType::Member:
{
const auto* memberExpr = static_cast<const MemberExpr*>(expr);
updateLiveness(memberExpr->object.get());
break;
}
case ExpressionType::Index:
{
const auto* indexExpr = static_cast<const IndexExpr*>(expr);
updateLiveness(indexExpr->object.get());
updateLiveness(indexExpr->index.get());
break;
}
case ExpressionType::Binary:
{
const auto* binaryExpr = static_cast<const BinaryExpr*>(expr);
updateLiveness(binaryExpr->left.get());
updateLiveness(binaryExpr->right.get());
break;
}
case ExpressionType::Unary:
{
const auto* unaryExpr = static_cast<const UnaryExpr*>(expr);
updateLiveness(unaryExpr->right.get());
break;
}
case ExpressionType::Call:
{
const auto* callExpr = static_cast<const CallExpr*>(expr);
for (const auto& arg : callExpr->arguments)
updateLiveness(arg.get());
break;
}
case ExpressionType::Constructor:
{
const auto* ctorExpr = static_cast<const ConstructorExpr*>(expr);
for (const auto& arg : ctorExpr->args)
updateLiveness(arg.get());
break;
}
case ExpressionType::Initializer:
{
const auto* initExpr = static_cast<const InitExpr*>(expr);
for (const auto& element : initExpr->elements)
updateLiveness(element.get());
break;
}
default:
throw std::runtime_error("Unsupported expression type in optimizer");
break; // For now, we won't handle these expression types
}
}
bool Optimizer::hasSideEffects(Expression* expr)
{
if (!expr) return false;
switch (expr->exprType)
{
case ExpressionType::Literal:
case ExpressionType::Variable:
case ExpressionType::Member:
return false;
case ExpressionType::Index:
{
const auto* indexExpr = static_cast<const IndexExpr*>(expr);
return hasSideEffects(indexExpr->object.get()) || hasSideEffects(indexExpr->index.get());
}
case ExpressionType::Binary:
{
const auto* binaryExpr = static_cast<const BinaryExpr*>(expr);
return hasSideEffects(binaryExpr->left.get()) || hasSideEffects(binaryExpr->right.get());
}
case ExpressionType::Unary:
{
const auto* unaryExpr = static_cast<const UnaryExpr*>(expr);
return hasSideEffects(unaryExpr->right.get());
}
case ExpressionType::Initializer:
{
const auto* initExpr = static_cast<const InitExpr*>(expr);
for (const auto& element : initExpr->elements)
{
if (hasSideEffects(element.get()))
return true;
}
return false;
}
case ExpressionType::Constructor:
{
const auto* ctorExpr = static_cast<const ConstructorExpr*>(expr);
for (const auto& arg : ctorExpr->args)
{
if (hasSideEffects(arg.get()))
return true;
}
return false;
}
case ExpressionType::Assignment:
return true; // assignments have side effects
case ExpressionType::Call:
return true; // assume all function calls have side effects for simplicity
default:
throw std::runtime_error("Unsupported expression type in optimizer");
}
}