-
Notifications
You must be signed in to change notification settings - Fork 3
/
optimize.h
95 lines (63 loc) · 1.92 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
#ifndef __TAB_OPTIMIZE_H
#define __TAB_OPTIMIZE_H
namespace tab {
namespace {
struct var_access {
size_t reads;
size_t writes;
size_t reads_after_write;
};
void unneeded_variable(std::vector<Command>& commands, size_t var, var_access& ret) {
if (commands.empty())
return;
// Find unnecessary variables and remove them.
// 'Unnecessary' means variables that are accessed only once, immediately after assignment.
size_t i = commands.size();
while (i > 0) {
--i;
auto& cmd = commands[i];
for (auto& j : cmd.closure) {
unneeded_variable(j.code, var, ret);
}
if (cmd.cmd == Command::VAR && cmd.arg.uint == var) {
ret.reads++;
if (i > 0) {
auto& cmdprev = commands[i - 1];
if (cmdprev.cmd == Command::VAW && cmdprev.arg.uint == var) {
ret.reads_after_write++;
}
}
} else if (cmd.cmd == Command::VAW && cmd.arg.uint == var) {
ret.writes++;
}
}
}
void remove_variable(std::vector<Command>& commands, size_t var) {
std::vector<Command> ret;
ret.reserve(commands.size());
for (auto& cmd : commands) {
for (auto& j : cmd.closure) {
remove_variable(j.code, var);
}
bool bad = ((cmd.cmd == Command::VAR || cmd.cmd == Command::VAW) && cmd.arg.uint == var);
if (!bad) {
ret.push_back(cmd);
}
}
commands.swap(ret);
}
}
#include <iostream>
void optimize(std::vector<Command>& commands, const TypeRuntime& typer) {
if (commands.empty())
return;
for (size_t var = 0; var < typer.num_vars(); ++var) {
var_access va{0, 0, 0};
unneeded_variable(commands, var, va);
if (va.reads == 1 && va.writes == 1 && va.reads_after_write == 1) {
remove_variable(commands, var);
}
}
}
}
#endif