-
Notifications
You must be signed in to change notification settings - Fork 1
/
SCCP.cpp
260 lines (237 loc) · 8.66 KB
/
SCCP.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
#include "SCCP.h"
#define CONST_INT(num) ConstantInt::get(num, module)
#define CONST_FLOAT(num) ConstantFloat::get(num, module)
Constant *SCCP::constFold(CmpInst *inst, Constant *v1, Constant *v2) {
switch (inst->get_cmp_op()) {
case CmpInst::EQ:
return CONST_INT(get_const_int(v1) == get_const_int(v2));
case CmpInst::NE:
return CONST_INT(get_const_int(v1) != get_const_int(v2));
case CmpInst::GT:
return CONST_INT(get_const_int(v1) > get_const_int(v2));
case CmpInst::GE:
return CONST_INT(get_const_int(v1) >= get_const_int(v2));
case CmpInst::LT:
return CONST_INT(get_const_int(v1) < get_const_int(v2));
case CmpInst::LE:
return CONST_INT(get_const_int(v1) <= get_const_int(v2));
default:
return nullptr;
}
}
Constant *SCCP::constFold(FCmpInst *inst, Constant *v1, Constant *v2) {
switch (inst->get_cmp_op()) {
case FCmpInst::EQ:
return CONST_INT(get_const_float(v1) == get_const_float(v2));
case FCmpInst::NE:
return CONST_INT(get_const_float(v1) != get_const_float(v2));
case FCmpInst::GT:
return CONST_INT(get_const_float(v1) > get_const_float(v2));
case FCmpInst::GE:
return CONST_INT(get_const_float(v1) >= get_const_float(v2));
case FCmpInst::LT:
return CONST_INT(get_const_float(v1) < get_const_float(v2));
case FCmpInst::LE:
return CONST_INT(get_const_float(v1) <= get_const_float(v2));
default:
return nullptr;
}
}
Constant *SCCP::constFold(Instruction *inst, Constant *v1, Constant *v2) {
auto op = inst->get_instr_type();
switch (op) {
case Instruction::add:
return CONST_INT(get_const_int(v1) + get_const_int(v2));
case Instruction::sub:
return CONST_INT(get_const_int(v1) - get_const_int(v2));
case Instruction::mul:
return CONST_INT(get_const_int(v1) * get_const_int(v2));
case Instruction::sdiv:
return CONST_INT(get_const_int(v1) / get_const_int(v2));
case Instruction::srem:
return CONST_INT(get_const_int(v1) % get_const_int(v2));
case Instruction::fadd:
return CONST_FLOAT(get_const_float(v1) + get_const_float(v2));
case Instruction::fsub:
return CONST_FLOAT(get_const_float(v1) - get_const_float(v2));
case Instruction::fmul:
return CONST_FLOAT(get_const_float(v1) * get_const_float(v2));
case Instruction::fdiv:
return CONST_FLOAT(get_const_float(v1) / get_const_float(v2));
case Instruction::cmp:
return constFold(static_cast<CmpInst *>(inst), v1, v2);
case Instruction::fcmp:
return constFold(static_cast<FCmpInst *>(inst), v1, v2);
default:
return nullptr;
}
}
Constant *SCCP::constFold(Instruction *inst, Constant *v) {
auto op = inst->get_instr_type();
switch (op) {
case Instruction::zext:
return CONST_INT(get_const_int(v));
case Instruction::sitofp:
return CONST_FLOAT((float)get_const_int(v));
case Instruction::fptosi:
return CONST_INT((int)get_const_float(v));
default:
return nullptr;
}
}
Constant *SCCP::constFold(Instruction *inst) {
if (inst->is_binary()) {
auto *const_v1 = getMapped(inst->get_operand(0)).value;
auto *const_v2 = getMapped(inst->get_operand(1)).value;
if (const_v1 != nullptr && const_v2 != nullptr)
return constFold(inst, const_v1, const_v2);
} else if (inst->is_unary()) {
auto *const_v = getMapped(inst->get_operand(0)).value;
if (const_v != nullptr)
return constFold(inst, const_v);
}
return nullptr;
}
void SCCP::execute() {
for (auto *f : module->get_functions())
sccp(f);
}
void SCCP::sccp(Function *f) {
if (f->is_declaration())
return;
value_map.clear();
marked.clear();
cfg_worklist.clear();
ssa_worklist.clear();
cfg_worklist.emplace_back(nullptr, f->get_entry_block());
for (auto *bb : f->get_basic_blocks())
for (auto *expr : bb->get_instructions())
value_map.set(expr, {ValueStatus::TOP});
auto i = 0U;
auto j = 0U;
while (i < cfg_worklist.size() || j < ssa_worklist.size()) {
while (i < cfg_worklist.size()) {
auto [pre_bb, bb] = cfg_worklist[i++];
if (marked.count({pre_bb, bb}) != 0)
continue;
marked.insert({pre_bb, bb});
for (auto *inst : bb->get_instructions())
instruction_visitor->visit(inst);
}
while (j < ssa_worklist.size()) {
auto *inst = ssa_worklist[j++];
auto *bb = inst->get_parent();
for (auto *pre_bb : bb->get_pre_basic_blocks()) {
if (marked.count({pre_bb, bb}) != 0) {
instruction_visitor->visit(inst);
break;
}
}
}
}
replaceConstant(f);
}
void SCCP::replaceConstant(Function *f) {
std::vector<Instruction *> delete_list;
for (auto *bb : f->get_basic_blocks()) {
for (auto *inst : bb->get_instructions()) {
if (auto *constant = getMapped(inst).value) {
inst->replace_all_use_with(constant);
delete_list.push_back(inst);
}
}
}
for (auto *inst : delete_list)
inst->get_parent()->delete_instr(inst);
// cond_br with const cond
for (auto *bb : f->get_basic_blocks()) {
auto *branch_inst = dynamic_cast<BranchInst *>(bb->get_terminator());
if (branch_inst == nullptr || !branch_inst->is_cond_br())
continue;
auto *const_cond =
dynamic_cast<ConstantInt *>(branch_inst->get_operand(0));
if (const_cond == nullptr)
continue;
auto *true_bb = static_cast<BasicBlock *>(branch_inst->get_operand(1));
auto *false_bb = static_cast<BasicBlock *>(branch_inst->get_operand(2));
if (const_cond->get_value() != 0)
condBrToJmp(branch_inst, true_bb, false_bb);
else
condBrToJmp(branch_inst, false_bb, true_bb);
}
}
void SCCP::condBrToJmp(Instruction *inst, BasicBlock *jmp_bb,
BasicBlock *invalid_bb) {
auto *bb = inst->get_parent();
inst->remove_operands(0, 2);
inst->add_operand(jmp_bb);
if (jmp_bb == invalid_bb)
return;
bb->remove_succ_basic_block(invalid_bb);
invalid_bb->remove_pre_basic_block(bb);
}
void InstructionVisitor::visit(Instruction *inst) {
inst_ = inst;
bb = inst->get_parent();
prev_status = value_map.get(inst);
cur_status = prev_status;
if (inst->is_phi()) {
visit_phi(static_cast<PhiInst *>(inst));
} else if (inst->is_br()) {
visit_br(static_cast<BranchInst *>(inst));
} else if (inst->is_binary() || inst->is_unary()) {
visit_foldable(inst);
} else {
cur_status = {ValueStatus::BOT};
}
if (cur_status != prev_status) {
value_map.set(inst, cur_status);
for (auto use : inst->get_use_list()) {
auto *use_inst = dynamic_cast<Instruction *>(use.val_);
ssa_worklist.push_back(use_inst);
}
}
}
void InstructionVisitor::visit_phi(PhiInst *inst) {
const int phi_size = inst->get_num_operand() / 2;
for (int i = 0; i < phi_size; i++) {
auto *pre_bb = static_cast<BasicBlock *>(inst->get_operand(2 * i + 1));
if (sccp.get_marked().count({pre_bb, bb}) != 0) {
auto *op = inst->get_operand(2 * i);
auto op_status = value_map.get(op);
cur_status ^= op_status;
}
}
}
void InstructionVisitor::visit_br(BranchInst *inst) {
if (!static_cast<BranchInst *>(inst)->is_cond_br()) {
auto *jmp_bb = static_cast<BasicBlock *>(inst->get_operand(0));
cfg_worklist.emplace_back(bb, jmp_bb);
return;
}
auto *true_bb = static_cast<BasicBlock *>(inst->get_operand(1));
auto *false_bb = static_cast<BasicBlock *>(inst->get_operand(2));
auto *const_cond =
static_cast<ConstantInt *>(value_map.get(inst->get_operand(0)).value);
if (const_cond != nullptr) {
const_cond->get_value() != 0 ? cfg_worklist.emplace_back(bb, true_bb)
: cfg_worklist.emplace_back(bb, false_bb);
} else {
cfg_worklist.emplace_back(bb, true_bb);
cfg_worklist.emplace_back(bb, false_bb);
}
}
void InstructionVisitor::visit_foldable(Instruction *inst) {
auto *folded = sccp.constFold(inst);
if (folded != nullptr) {
cur_status = {ValueStatus::CONST, folded};
return;
}
cur_status = {ValueStatus::TOP};
for (auto *op : inst->get_operands()) {
if (value_map.get(op).is_bot()) {
cur_status = {ValueStatus::BOT};
return;
}
}
}