-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizations.cpp
More file actions
executable file
·239 lines (172 loc) · 7.87 KB
/
optimizations.cpp
File metadata and controls
executable file
·239 lines (172 loc) · 7.87 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
#include "optimizations.h"
#include "cfg.h"
#include "highlevel.h"
// ConstantPropagation
ConstantPropagation::ConstantPropagation(
const std::shared_ptr<ControlFlowGraph>& cfg)
: ControlFlowGraphTransform(cfg){
}
std::shared_ptr<InstructionSequence> ConstantPropagation::transform_basic_block(const InstructionSequence *orig_bb) {
std::shared_ptr<InstructionSequence> result;
result = constant_propagation(orig_bb->duplicate());
return result;
}
/// Implments constant propagation
/// \param block the wrapped instruction sequence
/// \return a shred_ptr to a Instruction Sequence
std::shared_ptr<InstructionSequence> ConstantPropagation::constant_propagation(InstructionSequence *block) {
std::shared_ptr<InstructionSequence> result(new InstructionSequence());
// empty map of register number to constant value
std::map<int, long> constants;
for (auto i = block->cbegin(); i != block->cend(); i++) {
Instruction *instruction = *i;
auto opcode = static_cast<HighLevelOpcode>(instruction->get_opcode());
// Make sure not doing invalid instruction, enter jmp etc
if (instruction->get_num_operands() < 2) {
result->append(instruction->duplicate());
} else if(match_hl(HighLevelOpcode::HINS_mov_b, opcode) && instruction->get_operand(1).is_imm_ival()) {
// We are moving constant into vreg
constants[instruction->get_operand(0).get_base_reg()] = instruction->get_operand(1).get_imm_ival();
}
else {
Operand origin = instruction->get_operand(0);
if (instruction->get_num_operands() == 2) {
Operand left = instruction->get_operand(1);
if (left.has_base_reg() &&
constants.find(left.get_base_reg()) != constants.end() ) {
// Make new instruction with constant swapped in
Operand copied_constant(Operand::IMM_IVAL, constants[left.get_base_reg()]);
result->append(new Instruction(instruction->get_opcode(), origin, copied_constant));
} else {
result->append(instruction->duplicate());
}
} else if (instruction->get_num_operands() == 3) {
// Make new instruction with constant swapped in, trying either side
Operand left = instruction->get_operand(1);
Operand right = instruction->get_operand(2);
if (left.has_base_reg() &&
constants.find(left.get_base_reg()) != constants.end() ) {
Operand copied_constant(Operand::IMM_IVAL, constants[left.get_base_reg()]);
left = copied_constant;
}
if (right.has_base_reg() &&
constants.find(right.get_base_reg()) != constants.end() ) {
Operand copied_constant(Operand::IMM_IVAL, constants[right.get_base_reg()]);
right = copied_constant;
}
result->append(new Instruction(instruction->get_opcode(), origin, left, right));
} else {
continue;
}
}
}
return result;
}
// Copied from low level because it's useful
bool ConstantPropagation::match_hl(int base, int hl_opcode) {
return hl_opcode >= base && hl_opcode < (base + 4);
}
// CopyPropagation
CopyPropagation::CopyPropagation(
const std::shared_ptr<ControlFlowGraph>& cfg)
: ControlFlowGraphTransform(cfg){
}
bool CopyPropagation::match_hl(int base, int hl_opcode) {
return hl_opcode >= base && hl_opcode < (base + 4);
}
bool CopyPropagation::is_caller_saved(int vreg_num) {
if (vreg_num <= 2) {
return true;
}
return false;
}
std::shared_ptr<InstructionSequence> CopyPropagation::transform_basic_block(const InstructionSequence *orig_bb) {
std::shared_ptr<InstructionSequence> result;
result = copy_propagation(orig_bb->duplicate());
return result;
}
/// Copy propagation, the same as constant propagation but vreg to vreg
/// \param block
/// \return
std::shared_ptr<InstructionSequence> CopyPropagation::copy_propagation(InstructionSequence *block) {
std::shared_ptr<InstructionSequence> result(new InstructionSequence());
// empty map of register number to constant value
constants.clear();
for (auto i = block->cbegin(); i != block->cend(); i++) {
Instruction *instruction = *i;
auto opcode = static_cast<HighLevelOpcode>(instruction->get_opcode());
if (HighLevel::is_def(instruction)) {
if (match_hl(HighLevelOpcode::HINS_mov_b, opcode)) {
if (instruction->get_operand(1).is_imm_ival()) {
constants.erase(instruction->get_operand(0).get_base_reg());
} else {
constants[instruction->get_operand(0).get_base_reg()] = instruction->get_operand(1);
}
}
Operand origin = instruction->get_operand(0);
if (instruction->get_num_operands() == 2) {
Operand left = instruction->get_operand(1);
if (left.has_base_reg() && constants.find(left.get_base_reg()) != constants.end()) {
result->append(new Instruction(instruction->get_opcode(), origin, constants[left.get_base_reg()]));
} else {
result->append(instruction->duplicate());
}
} else if (instruction->get_num_operands() == 3) {
// Make new instruction with constant swapped in, trying either side
Operand left = instruction->get_operand(1);
Operand right = instruction->get_operand(2);
if (left.has_base_reg() && constants.find(left.get_base_reg()) != constants.end()) {
left = constants[left.get_base_reg()];
}
if (right.has_base_reg() && constants.find(right.get_base_reg()) != constants.end()) {
right = constants[right.get_base_reg()];
}
result->append(new Instruction(instruction->get_opcode(), origin, left, right));
}
} else {
result->append(instruction->duplicate());
}
}
return result;
}
std::shared_ptr<ControlFlowGraph> ConstantPropagation::transform_cfg() {
return ControlFlowGraphTransform::transform_cfg();
}
// LIVE ANALYSIS
LiveRegisters::LiveRegisters(const std::shared_ptr<ControlFlowGraph> &cfg)
: ControlFlowGraphTransform(cfg)
, m_live_vregs(cfg) {
m_live_vregs.execute();
}
LiveRegisters::~LiveRegisters() {
}
std::shared_ptr<InstructionSequence>
LiveRegisters::transform_basic_block(const InstructionSequence *orig_bb) {
// LiveVregs needs a pointer to a BasicBlock object to get a dataflow
// fact for that basic block
const auto *orig_bb_as_basic_block =
dynamic_cast<const BasicBlock *>(orig_bb);
std::shared_ptr<InstructionSequence> result_iseq(new InstructionSequence());
for (auto i = orig_bb->cbegin(); i != orig_bb->cend(); ++i) {
Instruction *orig_ins = *i;
bool preserve_instruction = true;
if (HighLevel::is_def(orig_ins)) {
Operand dest = orig_ins->get_operand(0);
LiveVregs::FactType live_after =
m_live_vregs.get_fact_after_instruction(orig_bb_as_basic_block, orig_ins);
if (!live_after.test(dest.get_base_reg()) && !is_caller_saved(dest.get_base_reg()))
// destination register is dead immediately after this instruction,
// so it can be eliminated
preserve_instruction = false;
}
if (preserve_instruction)
result_iseq->append(orig_ins->duplicate());
}
return result_iseq;
}
bool LiveRegisters::is_caller_saved(int vreg_num) {
if (vreg_num <= 2) {
return true;
}
return false;
}