Skip to content

Commit 5767ce0

Browse files
authored
Add more optimizer cases (#612)
1 parent ebd9d9d commit 5767ce0

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

src/eval/optimizer.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,28 @@ bool Optimizer::mergeOps(Program &p) const {
161161
// first sub, the other max?
162162
else if (o1.type == Operation::Type::SUB &&
163163
o2.type == Operation::Type::MAX &&
164-
o2.source.type == Operand::Type::CONSTANT &&
165164
o2.source.value == 0) {
166165
o1.type = Operation::Type::TRN;
167166
do_merge = true;
168167
}
169168

169+
// first trn, second add?
170+
else if (o1.type == Operation::Type::TRN &&
171+
o2.type == Operation::Type::ADD &&
172+
o1.source.value == o2.source.value) {
173+
o1.type = Operation::Type::MAX;
174+
do_merge = true;
175+
}
176+
177+
// first add, second trn?
178+
else if (o1.type == Operation::Type::ADD &&
179+
o2.type == Operation::Type::TRN &&
180+
o1.source.value == o2.source.value) {
181+
o1.type = Operation::Type::MAX;
182+
o1.source = Operand(Operand::Type::CONSTANT, 0);
183+
do_merge = true;
184+
}
185+
170186
// first mul, second div?
171187
else if (o1.type == Operation::Type::MUL &&
172188
o2.type == Operation::Type::DIV &&
@@ -255,6 +271,19 @@ bool Optimizer::mergeOps(Program &p) const {
255271
o1.source = Operand(Operand::Type::CONSTANT, 0);
256272
do_merge = true;
257273
}
274+
275+
// first trn, second add?
276+
else if (o1.type == Operation::Type::TRN && o2.type == Operation::Type::ADD) {
277+
o1.type = Operation::Type::MAX;
278+
do_merge = true;
279+
}
280+
281+
// first add, second trn?
282+
else if (o1.type == Operation::Type::ADD && o2.type == Operation::Type::TRN) {
283+
o1.type = Operation::Type::MAX;
284+
o1.source = Operand(Operand::Type::CONSTANT, 0);
285+
do_merge = true;
286+
}
258287
}
259288

260289
// first operation mov with constant?

tests/optimizer/E078.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; in
2+
sub $0,5
3+
mov $1,$0
4+
div $1,3
5+
add $0,$1
6+
trn $0,$1
7+
; out
8+
sub $0,5
9+
mov $1,$0
10+
div $1,3
11+
max $0,0

tests/optimizer/E079.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; in
2+
sub $0,5
3+
mov $1,$0
4+
div $1,3
5+
trn $0,$1
6+
add $0,$1
7+
; out
8+
sub $0,5
9+
mov $1,$0
10+
div $1,3
11+
max $0,$1

tests/optimizer/E080.asm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; in
2+
trn $0,7
3+
add $0,7
4+
; out
5+
max $0,7

tests/optimizer/E081.asm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; in
2+
add $0,7
3+
trn $0,7
4+
; out
5+
max $0,0

0 commit comments

Comments
 (0)