Skip to content

Commit b297b6e

Browse files
committed
[Tolk] Peephole optimization: LD+DROP to PLD
Peephole optimizations: - `N LDU` + `DROP` -> `N PLDU` - same for LDI -> PLDI, LDREF -> PLDREF, etc.
1 parent a045893 commit b297b6e

File tree

10 files changed

+61
-48
lines changed

10 files changed

+61
-48
lines changed

tolk-tester/tests/asm-arg-order.tolk

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,11 @@ fun main() {
269269
test34() PROC:<{
270270
x{020a} PUSHSLICE
271271
8 LDU
272-
8 LDU
273-
DROP
272+
8 PLDU
274273
SWAP
275274
1 ADDCONST MUL
276275
}>
277276
"""
278277

279-
@code_hash 88343225778124778743440967192570181022747908733235053033508198051414837052492
278+
@code_hash 38717859169035491454494966803813683158800226121601078320213761069394202390827
280279
*/

tolk-tester/tests/handle-msg-3.tolk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ fun onInternalMessage(in: InMessage) {
2323
IFJMP:<{
2424
32 LDU
2525
NIP
26-
32 LDU
27-
DROP
26+
32 PLDU
2827
THROWANY
2928
}> // in.body
3029
}>

tolk-tester/tests/lazy-algo-tests.tolk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,7 @@ fun main() {
14981498
x{0102} PUSHSLICE
14991499
8 LDI
15001500
NIP
1501-
8 LDI
1502-
DROP
1501+
8 PLDI
15031502
}>
15041503
"""
15051504
*/

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

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,8 +1732,7 @@ fun main() {
17321732
"""
17331733
demo101() PROC:<{ //
17341734
x{0102} PUSHSLICE // lazyS
1735-
8 LDI // '9 lazyS
1736-
DROP // p.x
1735+
8 PLDI // p.x
17371736
}>
17381737
"""
17391738

@@ -1743,8 +1742,7 @@ fun main() {
17431742
x{0102} PUSHSLICE // lazyS
17441743
8 LDU
17451744
NIP // lazyS
1746-
8 LDI // '10 lazyS
1747-
DROP // p.y
1745+
8 PLDI // p.y
17481746
"""
17491747

17501748
@fif_codegen
@@ -1764,8 +1762,7 @@ fun main() {
17641762
x{8000003d800000e4000000000000000000000000000000000000000000000000000000000000000020_} PUSHSLICE // lazyS
17651763
65 LDU
17661764
NIP // lazyS
1767-
256 LDU // '13 lazyS
1768-
DROP // st.publicKey
1765+
256 PLDU // st.publicKey
17691766
}>
17701767
"""
17711768

@@ -1775,13 +1772,11 @@ fun main() {
17751772
// lazyS
17761773
x{01} SDBEGINSQ
17771774
IF:<{
1778-
8 LDI
1779-
DROP
1775+
8 PLDI
17801776
}>ELSE<{
17811777
x{02} SDBEGINSQ
17821778
IF:<{
1783-
8 LDI
1784-
DROP
1779+
8 PLDI
17851780
NEGATE
17861781
}>ELSE<{
17871782
x{03} SDBEGINSQ
@@ -1793,8 +1788,7 @@ fun main() {
17931788
IFNOTJMP:<{
17941789
63 THROW
17951790
}>
1796-
32 LDI
1797-
DROP
1791+
32 PLDI
17981792
}>
17991793
}>
18001794
}>
@@ -1807,8 +1801,7 @@ fun main() {
18071801
x{010f} PUSHSLICE // lazyS
18081802
x{01} SDBEGINSQ // lazyS '6
18091803
134 THROWIFNOT // lazyS
1810-
8 LDI // '11 lazyS
1811-
DROP // cc
1804+
8 PLDI // cc
18121805
}>
18131806
"""
18141807

@@ -1823,17 +1816,15 @@ fun main() {
18231816
}>
18241817
8 LDU
18251818
NIP
1826-
8 LDI
1827-
DROP
1819+
8 PLDI
18281820
}>
18291821
"""
18301822

18311823
@fif_codegen
18321824
"""
18331825
demo146() PROC:<{
18341826
x{0000000} PUSHSLICE
1835-
LDOPTREF
1836-
DROP
1827+
PLDOPTREF
18371828
ISNULL
18381829
}>
18391830
"""
@@ -1844,8 +1835,7 @@ fun main() {
18441835
x{0102} PUSHSLICE
18451836
DUP
18461837
8 LDI
1847-
8 LDI
1848-
DROP
1838+
8 PLDI
18491839
s2 PUSH
18501840
NEWC
18511841
STSLICE
@@ -1866,8 +1856,7 @@ fun main() {
18661856
demo203() PROC:<{ //
18671857
x{010203040506070809} PUSHSLICE // lazyS
18681858
DUP // '14 lazyS
1869-
8 LDU // '14 '16 lazyS
1870-
DROP // '14 o.f1
1859+
8 PLDU // '14 o.f1
18711860
1 EQINT // '14 '18
18721861
IFJMP:<{ // '14
18731862
NEWC // '14 b
@@ -1889,17 +1878,15 @@ fun main() {
18891878
8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 lazyS
18901879
8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 lazyS
18911880
8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 lazyS
1892-
8 LDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 '32 lazyS
1893-
DROP // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 o.f9
1881+
8 PLDU // '14 o.f1 o.f2 o.f3 o.f4 o.f5 o.f6 o.f7 o.f8 o.f9
18941882
"""
18951883

18961884
@fif_codegen
18971885
"""
18981886
demo215() PROC:<{
18991887
x{0102} PUSHSLICE
19001888
8 LDI
1901-
8 LDI
1902-
DROP
1889+
8 PLDI
19031890
SWAP
19041891
NEWC
19051892
8 STI
@@ -1928,8 +1915,7 @@ fun main() {
19281915
1 LDI // '13 lazyS
19291916
NIP // lazyS
19301917
320 PUSHINT // lazyS '14=320
1931-
LDSLICEX // '15 lazyS
1932-
DROP // '15
1918+
PLDSLICEX // '15
19331919
PUSHNULL // '15 st.extensions
19341920
0 PUSHINT
19351921
NEWC
@@ -1947,8 +1933,7 @@ fun main() {
19471933
// lazyS
19481934
x{12345678} SDBEGINSQ
19491935
IF:<{
1950-
32 LDI
1951-
DROP
1936+
32 PLDI
19521937
10 ADDCONST
19531938
}>ELSE<{
19541939
x{23456789} SDBEGINSQ
@@ -1964,8 +1949,7 @@ fun main() {
19641949
32 LDU
19651950
8 LDU
19661951
NIP
1967-
8 LDI
1968-
DROP
1952+
8 PLDI
19691953
OVER
19701954
100 GTINT
19711955
IF:<{
@@ -2011,8 +1995,7 @@ fun main() {
20111995
"""
20121996
demo411() PROC:<{ // s
20131997
// lazyS
2014-
1 LDU // '10 lazyS
2015-
DROP // '10
1998+
1 PLDU // '10
20161999
IFJMP:<{ //
20172000
DUMPSTK //
20182001
}> //

tolk-tester/tests/null-keyword.tolk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ fun main() {
122122
"""
123123
test7() PROC:<{
124124
...
125-
LDOPTREF // b '9 '8
126-
DROP // b c
125+
PLDOPTREF // b c
127126
ISNULL // b '11
128127
10 MULCONST // b '13
129128
SWAP // '13 b

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ RETALT
138138
CTOS
139139
x{01} SDBEGINSQ
140140
63 THROWIFNOT
141-
8 LDI
142-
DROP
141+
8 PLDI
143142
}>
144143
"""
145144

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ fun main() {
175175
test3() PROC:<{
176176
x{0102} PUSHSLICE
177177
8 LDU
178-
8 LDI
179-
DROP
178+
8 PLDI
180179
}>
181180
"""
182181
*/

tolk-tester/tests/some-tests-3.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ fun f(cs: slice) {
3131
@testcase | 101 | x{000102030405060708090a0b0c0d0e0f10111213} | 6
3232
@testcase | 0 | x{000102030405060708090a0b0c0d0e0f10111213} | 0 1 2 3
3333

34-
@code_hash 58474889199998908444151060994149070836199913191952040273624197630531731101157
34+
@code_hash 110094925152266492480367791654603701827254965953641340034522027582587727126150
3535
*/

tolk/optimize.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,40 @@ bool Optimizer::detect_rewrite_NEWC_PUSH_STUR() {
264264
return true;
265265
}
266266

267+
// pattern `N LDU` + `DROP` -> `N PLDU` (common after loading the last field manually or by `lazy`);
268+
// the same for LDI -> PLDI, LDREF -> PLDREF, etc.
269+
bool Optimizer::detect_rewrite_LDxx_DROP() {
270+
bool second_drop = pb_ > 1 && op_[1]->is_pop() && op_[1]->a == 0;
271+
if (!second_drop || !op_[0]->is_custom()) {
272+
return false;
273+
}
274+
275+
static const char* ends_with[] = { " LDI", " LDU", " LDBITS"};
276+
static const char* repl_with[] = {" PLDI", " PLDU", " PLDBITS"};
277+
static const char* equl_to[] = { "LDREF", "LDDICT", "LDOPTREF", "LDSLICEX"};
278+
static const char* repl_to[] = {"PLDREF", "PLDDICT", "PLDOPTREF", "PLDSLICEX"};
279+
280+
std::string_view f = op_[0]->op;
281+
for (size_t i = 0; i < std::size(ends_with); ++i) {
282+
if (f.ends_with(ends_with[i])) {
283+
p_ = 2;
284+
q_ = 1;
285+
oq_[0] = std::make_unique<AsmOp>(AsmOp::Custom(op_[0]->loc, op_[0]->op.substr(0, f.rfind(' ')) + repl_with[i], 0, 1));
286+
return true;
287+
}
288+
}
289+
for (size_t i = 0; i < std::size(equl_to); ++i) {
290+
if (f == equl_to[i]) {
291+
p_ = 2;
292+
q_ = 1;
293+
oq_[0] = std::make_unique<AsmOp>(AsmOp::Custom(op_[0]->loc, repl_to[i], 0, 1));
294+
return true;
295+
}
296+
}
297+
298+
return false;
299+
}
300+
267301

268302
bool Optimizer::is_push_const(int* i, int* c) const {
269303
return pb_ >= 3 && pb_ <= l2_ && tr_[pb_ - 1].is_push_const(i, c);
@@ -685,6 +719,7 @@ bool Optimizer::find_at_least(int pb) {
685719
(is_xchg_xchg(&i, &j, &k, &l) && rewrite(AsmOp::Xchg(loc, i, j), AsmOp::Xchg(loc, k, l))) ||
686720
detect_rewrite_big_THROW() ||
687721
detect_rewrite_MY_store_int() || detect_rewrite_MY_skip_bits() || detect_rewrite_NEWC_PUSH_STUR() ||
722+
detect_rewrite_LDxx_DROP() ||
688723
(!(mode_ & 1) &&
689724
((is_rot() && rewrite(AsmOp::Custom(loc, "ROT", 3, 3))) || (is_rotrev() && rewrite(AsmOp::Custom(loc, "-ROT", 3, 3))) ||
690725
(is_2dup() && rewrite(AsmOp::Custom(loc, "2DUP", 2, 4))) ||

tolk/tolk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ struct Optimizer {
902902
bool detect_rewrite_MY_store_int();
903903
bool detect_rewrite_MY_skip_bits();
904904
bool detect_rewrite_NEWC_PUSH_STUR();
905+
bool detect_rewrite_LDxx_DROP();
905906

906907
AsmOpConsList extract_code();
907908
};

0 commit comments

Comments
 (0)