-
Notifications
You must be signed in to change notification settings - Fork 0
/
peephole.hh
203 lines (176 loc) · 6.33 KB
/
peephole.hh
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
#pragma once
#include <cassert>
#include <cstdint>
#include <memory>
#include <unordered_map>
#include "pass_manager.hh"
#include "pass_visitor.hh"
//
namespace jj_vm::passes {
class Peephole : Pass, PassVisitor {
public:
using GraphTy = jj_vm::graph::BBGraph;
using node_pointer = typename GraphTy::node_pointer;
using node_iterator = typename GraphTy::node_iterator;
private:
std::vector<std::reference_wrapper<jj_vm::ir::Instr>> m_instrs{};
public:
void run(jj_vm::ir::Function* func) override {
visit_func(func);
optimize();
}
void visit_instr(jj_vm::ir::Instr& instr) override {
switch (instr.opcode()) {
case jj_vm::ir::Opcode::MUL:
case jj_vm::ir::Opcode::SHR:
case jj_vm::ir::Opcode::XOR: {
m_instrs.push_back(instr);
break;
}
default: {
break;
}
}
}
bool is_really_need_peephole(jj_vm::ir::Instr& instr) {
return !instr.users().empty();
}
void optimize() {
for (auto ref_instr : m_instrs) {
auto& instr = ref_instr.get();
if (!is_really_need_peephole(instr)) continue;
switch (instr.opcode()) {
case jj_vm::ir::Opcode::MUL: {
process_mul(instr);
break;
}
case jj_vm::ir::Opcode::SHR: {
process_shr(instr);
break;
}
case jj_vm::ir::Opcode::XOR: {
process_xor(instr);
break;
}
default: {
break;
}
}
}
}
template <typename ConstTy, ConstTy const_value>
bool compare_val(const jj_vm::ir::Instr* instr) {
return static_cast<const jj_vm::ir::Constant<ConstTy>*>(instr)->val() ==
const_value;
}
template <typename ConstTy>
bool compare_val(const jj_vm::ir::Instr* lhs, const jj_vm::ir::Instr* rhs) {
return static_cast<const jj_vm::ir::Constant<ConstTy>*>(lhs)->val() ==
static_cast<const jj_vm::ir::Constant<ConstTy>*>(rhs)->val();
}
template <int const_value>
bool check_const_val(const jj_vm::ir::Instr* instr) {
switch (instr->type()) {
case jj_vm::ir::TypeId::I1:
return compare_val<bool, static_cast<bool>(const_value)>(instr);
case jj_vm::ir::TypeId::I8:
return compare_val<int8_t, int8_t{const_value}>(instr);
case jj_vm::ir::TypeId::I16:
return compare_val<int16_t, int16_t{const_value}>(instr);
case jj_vm::ir::TypeId::I32:
return compare_val<int32_t, int32_t{const_value}>(instr);
case jj_vm::ir::TypeId::I64:
return compare_val<int64_t, int64_t{const_value}>(instr);
default: {
assert(false && "Error: unsupported constant type in peephole");
return false;
}
}
}
bool compare_const_val(const jj_vm::ir::Instr* lhs,
const jj_vm::ir::Instr* rhs) {
bool same_type = lhs->type() == rhs->type();
if (!same_type) return false;
switch (lhs->type()) {
case jj_vm::ir::TypeId::I1:
return compare_val<bool>(lhs, rhs);
case jj_vm::ir::TypeId::I8:
return compare_val<int8_t>(lhs, rhs);
case jj_vm::ir::TypeId::I16:
return compare_val<int16_t>(lhs, rhs);
case jj_vm::ir::TypeId::I32:
return compare_val<int32_t>(lhs, rhs);
case jj_vm::ir::TypeId::I64:
return compare_val<int64_t>(lhs, rhs);
default: {
assert(false && "Error: unsupported constant type in peephole");
return false;
}
}
}
void process_mul(jj_vm::ir::Instr& instr) {
auto* lval = instr.get_input(0);
auto* rval = instr.get_input(1);
// Pattern 1:
// MUL v0, 1 -> v0
auto* const_instr = static_cast<jj_vm::ir::Instr*>(rval);
if (const_instr->opcode() == jj_vm::ir::Opcode::CONST &&
check_const_val<1>(const_instr)) {
lval->replace_users(instr);
jj_vm::ir::erase(&instr);
}
}
//
void process_shr(jj_vm::ir::Instr& instr) {
auto* lval = instr.get_input(0);
auto* rval = instr.get_input(1);
// Pattern 1, zero shift
// SHR v0, 1 -> v0
auto* const_instr = static_cast<jj_vm::ir::Instr*>(rval);
if (const_instr->opcode() == jj_vm::ir::Opcode::CONST &&
check_const_val<0>(const_instr)) {
lval->replace_users(instr);
jj_vm::ir::erase(&instr);
}
// Pattern 2, commulative two sequantially shify
// v1 = SHR v0, 1
// v2 = SHR v1, 2
// -->
// const = add 1, 2
// v2 = SHR v0, const
//! TODO: implementation in progress
}
//
void process_xor(jj_vm::ir::Instr& instr) {
auto* lval = instr.get_input(0);
auto* rval = instr.get_input(1);
// Pattern 1:
// XOR v0, 0 -> v0
auto* const_instr = static_cast<jj_vm::ir::Instr*>(rval);
if (const_instr->opcode() == jj_vm::ir::Opcode::CONST &&
check_const_val<0>(const_instr)) {
lval->replace_users(instr);
jj_vm::ir::erase(&instr);
}
// Pattern 2, same registers
// v0 = i64 1
// XOR v0, v0 -> 0
// Pattern 3, same constant values
// v0 = i64 1
// v1 = i64 1
// XOR v0, v1 -> 0
auto* lval_instr = static_cast<jj_vm::ir::Instr*>(lval);
auto* rval_instr = static_cast<jj_vm::ir::Instr*>(rval);
bool both_const = lval_instr->opcode() == jj_vm::ir::Opcode::CONST &&
rval_instr->opcode() == jj_vm::ir::Opcode::CONST;
//
bool is_equal_vals =
both_const && compare_const_val(lval_instr, rval_instr);
if (lval == rval || is_equal_vals) {
std::unique_ptr<jj_vm::ir::Instr> const_zero{
new jj_vm::ir::Constant<int64_t>{0}};
instr.parent()->replace_instr(&instr, const_zero.release());
}
}
};
} // namespace jj_vm::passes