-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.cpp
230 lines (218 loc) · 6.67 KB
/
opt.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
#include "stdafx.h"
int calculate(int a, int b, int op)
{
switch(op)
{
case qi::ADD: return (a+b);
case qi::SUB: return (a-b);
case qi::DIV: return (a/b);
case qi::MUL: return (a*b);
default: assert(0);
}
}
void do_substitution(qoperand &qo, map<qoperand, qoperand> &sbsttt)
{
auto iter = sbsttt.find(qo);
if(iter != sbsttt.end())
{
qo.type = iter->second.type;
qo.value = iter->second.value;
}
}
void eraseqi(qi &q)
{
q.op = qi::NOP;
q.A = BLANKOP;
q.B = BLANKOP;
q.D = BLANKOP;
}
//bblock level optimization
void opt_bblk_level(vector<qi> &qilist, vector<bblk> &bblklist, int bidx, actvartbl &avflow, int ct)
{
bblk &b = bblklist[bidx];
//variable substitution and constant combination
map<qoperand, qoperand> sbsttt;//used for variable substitution
//for each qi
for(int qidx = b.entrance; qidx<=b.exit; ++qidx)
{
qi &q = qilist[qidx];
//use of qo, do substitution
switch(q.op)
{
case qi::ASSIGN: case qi::NEG:
case qi::JZ: case qi::RET:
case qi::PUSH: case qi::WR:
do_substitution(q.A, sbsttt);
}
switch(q.op)
{
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmatic
case qi::JEQ: case qi::JGE: case qi::JGT: case qi::JLE:
case qi::JLT: case qi::JNE: //binary cmp
do_substitution(q.A, sbsttt);
do_substitution(q.B, sbsttt);
}
switch(q.op)
{
case qi::ARRAYASS:
do_substitution(q.D, sbsttt);//intent to cross case
case qi::ARRAYLOAD:
do_substitution(q.B, sbsttt);
}
//constant combination
switch(q.op)
{
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL:
if(q.A.type == qoperand::IMD && q.B.type == qoperand::IMD)
{
q.A = qoperand{qoperand::IMD, calculate(q.A.value, q.B.value, q.op), int_t};
q.B = BLANKOP;
q.op = qi::ASSIGN;
}
break;
case qi::NEG:
if(q.A.type == qoperand::IMD)
{
q.A = qoperand{qoperand::IMD, -q.A.value, int_t};
q.op = qi::ASSIGN;
}
}
//update substitution map
switch(q.op)//if there is a define point, delete relate substitution in map
{
case qi::NEG://unary arithmetic
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmetic
case qi::ARRAYLOAD: case qi::CALL:
for(auto it = sbsttt.begin(); it != sbsttt.end(); )
if(it->first == q.D || it->second == q.D)
it = sbsttt.erase(it);
else
++it;
break;
case qi::RD:
for(auto it = sbsttt.begin(); it != sbsttt.end(); )
if(it->first == q.A || it->second == q.A)
it = sbsttt.erase(it);
else
++it;
}
if(q.op == qi::ASSIGN)
{
//stop the spread of q.D
for(auto it = sbsttt.begin(); it != sbsttt.end(); )
if(it->second == q.D)
it = sbsttt.erase(it);
else
++it;
sbsttt[q.D] = q.A;//and spread the new q.D
}
else if(q.op == qi::CALL)
{
//function call, must assume every global variable may be change
for(auto it = sbsttt.begin(); it != sbsttt.end(); )
if(it->first.type == qoperand::GLB_OBJ)
it = sbsttt.erase(it);
else
++it;
}
}
//do reduce
set<qoperand> use;
for(int i :avflow.out[bidx])
use.insert(qoperand{qoperand::LCL_OBJ, i, int_t});
for(int qidx = b.exit; qidx>=b.entrance; --qidx)
{
qi &q = qilist[qidx];
//delete silly define point
if(q.op == qi::ASSIGN && q.A == q.D)
{
eraseqi(q);
continue;
}
//delete some define point not in set `use`
switch(q.op)
{
case qi::ASSIGN: case qi::NEG://unary arithmetic
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmetic
if(q.D.type!=qoperand::GLB_OBJ && use.find(q.D)==use.end())
{
eraseqi(q);
continue;
}
}
//take away variables in set `use` at define point
switch(q.op)
{
case qi::ASSIGN: case qi::NEG://unary arithmetic
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmetic
case qi::ARRAYLOAD: case qi::CALL:
use.erase(q.D);
break;
case qi::RD:
use.erase(q.A);
}
//add non-global variables in set `use` at use point
switch(q.op)
{
case qi::ASSIGN: case qi::NEG:
case qi::JZ: case qi::RET:
case qi::PUSH: case qi::WR:
if(q.A.type==qoperand::LCL_OBJ || q.A.type==qoperand::TMP)
use.insert(q.A);
}
switch(q.op)
{
case qi::ADD: case qi::SUB:
case qi::DIV: case qi::MUL: //binary arithmatic
case qi::JEQ: case qi::JGE: case qi::JGT: case qi::JLE:
case qi::JLT: case qi::JNE: //binary cmp
if(q.A.type==qoperand::LCL_OBJ || q.A.type==qoperand::TMP)
use.insert(q.A);
if(q.B.type==qoperand::LCL_OBJ || q.B.type==qoperand::TMP)
use.insert(q.B);
}
switch(q.op)
{
case qi::ARRAYASS:
if(q.D.type==qoperand::LCL_OBJ || q.D.type==qoperand::TMP)
use.insert(q.D);
case qi::ARRAYLOAD:
if(q.B.type==qoperand::LCL_OBJ || q.B.type==qoperand::TMP)
use.insert(q.B);
}
}
}
//do some small but efficient optimization
//such as copy-spread, dead-code deletion, and other thing
void general_optmize()
{
buildbblks();
//for each function
for(size_t ct=0; ct<functbl.size(); ++ct)
{
//do active variable analysis
vector<qi> &qilist = functbl[ct].qilist;
vector<bblk> &bblklist = bblktbl[ct];
actvartbl avflow;//function level
actvar(functbl[ct], bblklist, avflow);
//do some optimization to each bblock
for(size_t bidx=0; bidx<bblklist.size(); ++bidx)
{
opt_bblk_level(qilist, bblklist, bidx, avflow, ct);
}
}
}
void optimize()
{
if(!flg_no_dag)
opt_DAG();
if(!flg_no_reduce)
general_optmize();
if(flg_glbreg)
;
}