Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## [Unreleased]

### Enhancements

* Add `pushDownMov` optimization
* Improve `pullUpMov` optimization
* Add `clr` and `fil` support in incremental evaluator before main loop

## v26.6.7
Expand Down
41 changes: 40 additions & 1 deletion src/eval/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ bool Optimizer::optimize(Program& p) const {
if (pullUpMov(p)) {
changed = true;
}
if (pushDownMov(p)) {
changed = true;
}
if (removeCommutativeDetour(p)) {
changed = true;
}
Expand Down Expand Up @@ -969,7 +972,7 @@ bool canMerge(Operation::Type a, Operation::Type b) {
}

bool Optimizer::pullUpMov(Program& p) const {
// see tests E014 and E015
// see tests E014, E015, E086 and E087
bool changed = false;
for (size_t i = 0; i + 2 < p.ops.size(); i++) {
auto& a = p.ops[i];
Expand Down Expand Up @@ -1005,6 +1008,42 @@ bool Optimizer::pullUpMov(Program& p) const {
return changed;
}

bool Optimizer::pushDownMov(Program& p) const {
// see tests E088, E089 and E090
bool changed = false;
for (size_t i = 0; i + 2 < p.ops.size(); i++) {
auto& a = p.ops[i];
auto& b = p.ops[i + 1];
const auto& c = p.ops[i + 2];
// check operation types
if (a.type != Operation::Type::MOV || c.type != Operation::Type::MOV || (!ProgramUtil::isArithmetic(b.type) && b.type != Operation::Type::SEQ) ) {
continue;
}
// check operand types
if (a.target.type != Operand::Type::DIRECT ||
a.source.type != Operand::Type::DIRECT ||
b.target.type != Operand::Type::DIRECT ||
b.source.type == Operand::Type::INDIRECT ||
c.target.type != Operand::Type::DIRECT ||
c.source.type != Operand::Type::DIRECT) {
continue;
}
// check operand values
if (a.target.value != b.target.value || a.target.value != c.source.value || a.source.value != c.target.value) {
continue;
}
// okay, we are ready to optimize!
b.target.value = a.source.value;
if (b.source.type == Operand::Type::DIRECT && b.source.value == a.target.value) {
b.source.value = a.source.value;
}
std::swap(a, b);
p.ops.erase(p.ops.begin() + i + 2);
changed = true;
}
return changed;
}

bool isDirectMov(const Operation& op) {
return op.type == Operation::Type::MOV &&
op.target.type == Operand::Type::DIRECT &&
Expand Down
2 changes: 2 additions & 0 deletions src/eval/optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Optimizer {

bool pullUpMov(Program &p) const;

bool pushDownMov(Program &p) const;

bool collapseMovChains(Program &p) const;

bool removeCommutativeDetour(Program &p) const;
Expand Down
4 changes: 2 additions & 2 deletions tests/optimizer/E012.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mov $2,$0
add $2,17
mov $0,$2
; out
add $0,17
mov $1,$0
add $1,17
mov $0,$1

7 changes: 7 additions & 0 deletions tests/optimizer/E088.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; in
mov $1,$0
add $1,6
mov $0,$1
; out
add $0,6
mov $1,$0
7 changes: 7 additions & 0 deletions tests/optimizer/E089.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; in
mov $1,$0
pow $1,$0
mov $0,$1
; out
pow $0,$0
mov $1,$0
7 changes: 7 additions & 0 deletions tests/optimizer/E090.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; in
mov $1,$0
pow $1,$1
mov $0,$1
; out
pow $0,$0
mov $1,$0
Loading