Skip to content

Commit 25fffae

Browse files
committed
optimizer: collapse mov loops
1 parent 64d827e commit 25fffae

File tree

7 files changed

+77
-7
lines changed

7 files changed

+77
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ To install or update LODA, please follow the [installation instructions](https:/
22

33
## [Unreleased]
44

5+
## v24.9.7
6+
57
### Bugfixes
68

7-
* Fix optimizer `prg`-handling
9+
* Fix optimizer `prg` handling
810
* Fix max memory option `-m`
911
* Fix program folding
1012

@@ -13,6 +15,7 @@ To install or update LODA, please follow the [installation instructions](https:/
1315
* Support `prg` in `unfold` command
1416
* Internal `replace` command
1517
* Remove deprecated dot export
18+
* Collapse `mov` loops in optimizer
1619

1720
## v24.9.2
1821

src/lang/optimizer.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ bool Optimizer::optimize(Program &p) const {
5252
if (mergeLoops(p)) {
5353
changed = true;
5454
}
55-
if (collapseLoops(p)) {
55+
if (collapseMovLoops(p)) {
56+
changed = true;
57+
}
58+
if (collapseArithmeticLoops(p)) {
5659
changed = true;
5760
}
5861
if (pullUpMov(p)) {
@@ -768,7 +771,42 @@ bool Optimizer::mergeLoops(Program &p) const {
768771
return false;
769772
}
770773

771-
bool Optimizer::collapseLoops(Program &p) const {
774+
bool Optimizer::collapseMovLoops(Program &p) const {
775+
bool changed = false;
776+
for (size_t i = 0; i + 2 < p.ops.size(); i++) {
777+
if (p.ops[i].type != Operation::Type::LPB ||
778+
p.ops[i + 1].type != Operation::Type::MOV ||
779+
p.ops[i + 2].type != Operation::Type::LPE) {
780+
continue;
781+
}
782+
const auto &lpb = p.ops[i];
783+
const auto &mov = p.ops[i + 1];
784+
if (lpb.source != Operand(Operand::Type::CONSTANT, 1) ||
785+
lpb.target.type != Operand::Type::DIRECT ||
786+
mov.source.type != Operand::Type::CONSTANT ||
787+
mov.target != lpb.target) {
788+
continue;
789+
}
790+
auto val = mov.source.value;
791+
if (val < Number::ZERO) {
792+
p.ops.erase(p.ops.begin() + i, p.ops.begin() + i + 3);
793+
changed = true;
794+
} else if (val == Number::ZERO) {
795+
p.ops.erase(p.ops.begin() + i + 1, p.ops.begin() + i + 3);
796+
p.ops[i] = Operation(Operation::Type::MOV, lpb.target,
797+
Operand(Operand::Type::CONSTANT, 0));
798+
changed = true;
799+
} else {
800+
p.ops.erase(p.ops.begin() + i + 1, p.ops.begin() + i + 3);
801+
p.ops[i] = Operation(Operation::Type::MIN, lpb.target,
802+
Operand(Operand::Type::CONSTANT, val));
803+
changed = true;
804+
}
805+
}
806+
return changed;
807+
}
808+
809+
bool Optimizer::collapseArithmeticLoops(Program &p) const {
772810
if (ProgramUtil::hasIndirectOperand(p)) {
773811
return false;
774812
}

src/lang/optimizer.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class Optimizer {
3131

3232
bool mergeLoops(Program &p) const;
3333

34-
bool collapseLoops(Program &p) const;
34+
bool collapseMovLoops(Program &p) const;
35+
36+
bool collapseArithmeticLoops(Program &p) const;
3537

3638
bool pullUpMov(Program &p) const;
3739

src/lang/program_util.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ bool ProgramUtil::getUsedMemoryCells(const Program &p,
217217
for (const auto &op : p.ops) {
218218
int64_t region_length = 1;
219219
if (op.source.type == Operand::Type::INDIRECT ||
220-
op.target.type == Operand::Type::INDIRECT) {
220+
op.target.type == Operand::Type::INDIRECT ||
221+
op.type == Operation::Type::PRG) {
221222
return false;
222223
}
223-
if (op.type == Operation::Type::LPB ||
224-
ProgramUtil::isWritingRegion(op.type)) {
224+
if (op.type == Operation::Type::LPB || op.type == Operation::Type::CLR ||
225+
op.type == Operation::Type::SRT) {
225226
if (op.source.type == Operand::Type::CONSTANT) {
226227
region_length = op.source.value.asInt();
227228
} else {

tests/optimizer/E046.asm

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

tests/optimizer/E047.asm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; in
2+
add $0,3
3+
lpb $0
4+
mov $0,0
5+
lpe
6+
add $0,7
7+
; out
8+
mov $0,7

tests/optimizer/E048.asm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; in
2+
add $0,3
3+
lpb $0
4+
mov $0,-3
5+
lpe
6+
add $0,7
7+
; out
8+
add $0,10

0 commit comments

Comments
 (0)