Skip to content

Commit a045893

Browse files
committed
[Tolk] Peephole optimization: SDSKIPFIRST to LDU+NIP
It's a bit better for gas usage, always
1 parent 06e0544 commit a045893

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

tolk-tester/tests/cells-slices.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ We test that consequtive storeInt/storeUint with constants are joined into a sin
401401
@fif_codegen
402402
"""
403403
test21() PROC:<{
404-
14 PUSHINT
405-
SDSKIPFIRST
404+
14 LDU
405+
NIP
406406
}>
407407
"""
408408

tolk-tester/tests/lazy-load-tests.tolk

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,8 +1741,8 @@ fun main() {
17411741
"""
17421742
demo103() PROC:<{ //
17431743
x{0102} PUSHSLICE // lazyS
1744-
8 PUSHINT
1745-
SDSKIPFIRST // lazyS
1744+
8 LDU
1745+
NIP // lazyS
17461746
8 LDI // '10 lazyS
17471747
DROP // p.y
17481748
"""
@@ -1753,17 +1753,17 @@ fun main() {
17531753
// lazyS
17541754
PUSHNULL // lazyS x
17551755
SWAP // x lazyS
1756-
8 PUSHINT
1757-
SDSKIPFIRST // x lazyS
1756+
8 LDU
1757+
NIP // x lazyS
17581758
1 LDU // x '18 lazyS
17591759
"""
17601760

17611761
@fif_codegen
17621762
"""
17631763
demo109() PROC:<{ //
17641764
x{8000003d800000e4000000000000000000000000000000000000000000000000000000000000000020_} PUSHSLICE // lazyS
1765-
65 PUSHINT
1766-
SDSKIPFIRST // lazyS
1765+
65 LDU
1766+
NIP // lazyS
17671767
256 LDU // '13 lazyS
17681768
DROP // st.publicKey
17691769
}>
@@ -1821,8 +1821,8 @@ fun main() {
18211821
DROP
18221822
-1 PUSHINT
18231823
}>
1824-
8 PUSHINT
1825-
SDSKIPFIRST
1824+
8 LDU
1825+
NIP
18261826
8 LDI
18271827
DROP
18281828
}>
@@ -1962,8 +1962,8 @@ fun main() {
19621962
THROWANY
19631963
}>
19641964
32 LDU
1965-
8 PUSHINT
1966-
SDSKIPFIRST
1965+
8 LDU
1966+
NIP
19671967
8 LDI
19681968
DROP
19691969
OVER

tolk-tester/tests/pack-unpack-1.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ fun main(c: cell) {
112112
64 STI // b
113113
ENDC // c
114114
CTOS // s
115-
64 PUSHINT
116-
SDSKIPFIRST // s
115+
64 LDU
116+
NIP // s
117117
SBITS // '17
118118
}>
119119
"""

tolk/optimize.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,13 @@ bool Optimizer::detect_rewrite_MY_skip_bits() {
231231

232232
p_ = n_merged;
233233
q_ = 2;
234-
oq_[0] = std::make_unique<AsmOp>(AsmOp::IntConst(op_[0]->loc, td::make_refint(total_skip_bits)));
235-
oq_[1] = std::make_unique<AsmOp>(AsmOp::Custom(op_[0]->loc, "SDSKIPFIRST"));
234+
if (total_skip_bits <= 256) {
235+
oq_[0] = std::make_unique<AsmOp>(AsmOp::Custom(op_[0]->loc, std::to_string(total_skip_bits) + " LDU"));
236+
oq_[1] = std::make_unique<AsmOp>(AsmOp::Pop(op_[0]->loc, 1));
237+
} else {
238+
oq_[0] = std::make_unique<AsmOp>(AsmOp::IntConst(op_[0]->loc, td::make_refint(total_skip_bits)));
239+
oq_[1] = std::make_unique<AsmOp>(AsmOp::Custom(op_[0]->loc, "SDSKIPFIRST"));
240+
}
236241
return true;
237242
}
238243

0 commit comments

Comments
 (0)