2
This file is part of TON Blockchain Library.
4
TON Blockchain Library is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as published by
6
the Free Software Foundation, either version 2 of the License, or
7
(at your option) any later version.
9
TON Blockchain Library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public License
15
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
17
Copyright 2017-2020 Telegram Systems LLP
29
void Optimizer::set_code(AsmOpConsList code) {
30
code_ = std::move(code);
34
void Optimizer::unpack() {
36
for (AsmOpCons *p = code_.get(); p && i < n; p = p->cdr.get(), ++j) {
37
if (p->car->is_very_custom()) {
40
if (p->car->is_comment()) {
44
op_[i] = std::move(p->car);
49
indent_ = (i ? op_[0]->indent : 0);
52
void Optimizer::pack() {
53
for (int i = 0; i < l_; i++) {
54
op_cons_[i]->car = std::move(op_[i]);
55
op_cons_[i] = nullptr;
60
void Optimizer::apply() {
64
func_assert(p_ > 0 && p_ <= l_ && q_ >= 0 && q_ <= n && l_ <= n);
65
for (int i = p_; i < l_; i++) {
67
op_cons_[i]->car = std::move(op_[i]);
68
op_cons_[i] = nullptr;
70
for (int c = offs_[p_ - 1]; c >= 0; --c) {
71
code_ = std::move(code_->cdr);
73
for (int j = q_ - 1; j >= 0; j--) {
75
oq_[j]->indent = indent_;
76
code_ = AsmOpCons::cons(std::move(oq_[j]), std::move(code_));
81
AsmOpConsList Optimizer::extract_code() {
83
return std::move(code_);
86
void Optimizer::show_head() const {
90
std::cerr << "optimizing";
91
for (int i = 0; i < l_; i++) {
93
std::cerr << ' ' << *op_[i] << ' ';
95
std::cerr << " (null) ";
98
std::cerr << std::endl;
101
void Optimizer::show_left() const {
105
std::cerr << "// *** rewriting";
106
for (int i = 0; i < p_; i++) {
108
std::cerr << ' ' << *op_[i] << ' ';
110
std::cerr << " (null) ";
115
void Optimizer::show_right() const {
120
for (int i = 0; i < q_; i++) {
122
std::cerr << ' ' << *oq_[i] << ' ';
124
std::cerr << " (null) ";
127
std::cerr << std::endl;
130
bool Optimizer::say(std::string str) const {
132
std::cerr << str << std::endl;
137
bool Optimizer::find_const_op(int* op_idx, int cst) {
138
for (int i = 0; i < l2_; i++) {
139
if (op_[i]->is_gconst() && tr_[i].get(0) == cst) {
147
bool Optimizer::is_push_const(int* i, int* c) const {
148
return pb_ >= 3 && pb_ <= l2_ && tr_[pb_ - 1].is_push_const(i, c);
151
// PUSHCONST c ; PUSH s(i+1) ; SWAP -> PUSH s(i) ; PUSHCONST c
152
bool Optimizer::rewrite_push_const(int i, int c) {
156
if (!(p_ >= 2 && find_const_op(&idx, c) && idx < p_)) {
160
oq_[1] = std::move(op_[idx]);
161
oq_[0] = std::move(op_[!idx]);
162
*oq_[0] = AsmOp::Push(i);
167
bool Optimizer::is_const_rot(int* c) const {
168
return pb_ >= 3 && pb_ <= l2_ && tr_[pb_ - 1].is_const_rot(c);
171
bool Optimizer::rewrite_const_rot(int c) {
175
if (!(p_ >= 2 && find_const_op(&idx, c) && idx < p_)) {
179
oq_[0] = std::move(op_[idx]);
180
oq_[1] = std::move(op_[!idx]);
181
*oq_[1] = AsmOp::Custom("ROT", 3, 3);
186
bool Optimizer::is_const_pop(int* c, int* i) const {
187
return pb_ >= 3 && pb_ <= l2_ && tr_[pb_ - 1].is_const_pop(c, i);
190
bool Optimizer::rewrite_const_pop(int c, int i) {
194
if (!(p_ >= 2 && find_const_op(&idx, c) && idx < p_)) {
198
oq_[0] = std::move(op_[idx]);
199
oq_[1] = std::move(op_[!idx]);
200
*oq_[1] = AsmOp::Pop(i);
205
bool Optimizer::is_const_push_xchgs() {
206
if (!(pb_ >= 2 && pb_ <= l2_ && op_[0]->is_gconst())) {
211
for (i = 1; i < pb_; i++) {
213
if (op_[i]->is_xchg(&a, &b)) {
216
} else if (pos == b) {
219
t.apply_xchg(a - (a > pos), b - (b > pos));
221
} else if (op_[i]->is_push(&a)) {
225
t.apply_push(a - (a > pos));
234
t.apply_push_newconst();
235
if (t <= tr_[i - 1]) {
243
bool Optimizer::rewrite_const_push_xchgs() {
248
auto c_op = std::move(op_[0]);
249
func_assert(c_op->is_gconst());
253
for (int i = 1; i < p_; i++) {
255
if (op_[i]->is_xchg(&a, &b)) {
258
} else if (b == pos) {
261
oq_[q_] = std::move(op_[i]);
268
func_assert(apply_op(t, *oq_[q_]));
272
func_assert(op_[i]->is_push(&a));
273
func_assert(a != pos);
274
oq_[q_] = std::move(op_[i]);
278
func_assert(apply_op(t, *oq_[q_]));
284
t.apply_push_newconst();
285
func_assert(t <= tr_[p_ - 1]);
286
oq_[q_++] = std::move(c_op);
291
bool Optimizer::rewrite(int p, AsmOp&& new_op) {
292
func_assert(p > 0 && p <= l_);
296
oq_[0] = std::move(op_[0]);
302
bool Optimizer::rewrite(int p, AsmOp&& new_op1, AsmOp&& new_op2) {
303
func_assert(p > 1 && p <= l_);
307
oq_[0] = std::move(op_[0]);
309
oq_[1] = std::move(op_[1]);
315
bool Optimizer::rewrite(int p, AsmOp&& new_op1, AsmOp&& new_op2, AsmOp&& new_op3) {
316
func_assert(p > 2 && p <= l_);
320
oq_[0] = std::move(op_[0]);
322
oq_[1] = std::move(op_[1]);
324
oq_[2] = std::move(op_[2]);
330
bool Optimizer::rewrite_nop() {
331
func_assert(p_ > 0 && p_ <= l_);
338
bool Optimizer::is_pred(const std::function<bool(const StackTransform&)>& pred, int min_p) {
339
min_p = std::max(min_p, pb_);
340
for (int p = l2_; p >= min_p; p--) {
341
if (pred(tr_[p - 1])) {
349
bool Optimizer::is_same_as(const StackTransform& trans, int min_p) {
350
return is_pred([&trans](const auto& t) { return t >= trans; }, min_p);
353
// s1 s3 XCHG ; s0 s2 XCHG -> 2SWAP
354
bool Optimizer::is_2swap() {
355
static const StackTransform t_2swap{2, 3, 0, 1, 4};
356
return is_same_as(t_2swap);
359
// s3 PUSH ; s3 PUSH -> 2OVER
360
bool Optimizer::is_2over() {
361
static const StackTransform t_2over{2, 3, 0};
362
return is_same_as(t_2over);
365
bool Optimizer::is_2dup() {
366
static const StackTransform t_2dup{0, 1, 0};
367
return is_same_as(t_2dup);
370
bool Optimizer::is_tuck() {
371
static const StackTransform t_tuck{0, 1, 0, 2};
372
return is_same_as(t_tuck);
375
bool Optimizer::is_2drop() {
376
static const StackTransform t_2drop{2};
377
return is_same_as(t_2drop);
380
bool Optimizer::is_rot() {
381
return is_pred([](const auto& t) { return t.is_rot(); });
384
bool Optimizer::is_rotrev() {
385
return is_pred([](const auto& t) { return t.is_rotrev(); });
388
bool Optimizer::is_nop() {
389
return is_pred([](const auto& t) { return t.is_id(); }, 1);
392
bool Optimizer::is_xchg(int* i, int* j) {
393
return is_pred([i, j](const auto& t) { return t.is_xchg(i, j) && ((*i < 16 && *j < 16) || (!*i && *j < 256)); });
396
bool Optimizer::is_xchg_xchg(int* i, int* j, int* k, int* l) {
397
return is_pred([i, j, k, l](const auto& t) {
398
return t.is_xchg_xchg(i, j, k, l) && (*i < 2 && *j < (*i ? 16 : 256) && *k < 2 && *l < (*k ? 16 : 256));
400
(!(p_ == 2 && op_[0]->is_xchg(*i, *j) && op_[1]->is_xchg(*k, *l)));
403
bool Optimizer::is_push(int* i) {
404
return is_pred([i](const auto& t) { return t.is_push(i) && *i < 256; });
407
bool Optimizer::is_pop(int* i) {
408
return is_pred([i](const auto& t) { return t.is_pop(i) && *i < 256; });
411
bool Optimizer::is_pop_pop(int* i, int* j) {
412
return is_pred([i, j](const auto& t) { return t.is_pop_pop(i, j) && *i < 256 && *j < 256; }, 3);
415
bool Optimizer::is_push_rot(int* i) {
416
return is_pred([i](const auto& t) { return t.is_push_rot(i) && *i < 16; }, 3);
419
bool Optimizer::is_push_rotrev(int* i) {
420
return is_pred([i](const auto& t) { return t.is_push_rotrev(i) && *i < 16; }, 3);
423
bool Optimizer::is_push_xchg(int* i, int* j, int* k) {
424
return is_pred([i, j, k](const auto& t) { return t.is_push_xchg(i, j, k) && *i < 16 && *j < 16 && *k < 16; }) &&
425
!(p_ == 2 && op_[0]->is_push() && op_[1]->is_xchg());
428
bool Optimizer::is_xchg2(int* i, int* j) {
429
return is_pred([i, j](const auto& t) { return t.is_xchg2(i, j) && *i < 16 && *j < 16; });
432
bool Optimizer::is_xcpu(int* i, int* j) {
433
return is_pred([i, j](const auto& t) { return t.is_xcpu(i, j) && *i < 16 && *j < 16; });
436
bool Optimizer::is_puxc(int* i, int* j) {
437
return is_pred([i, j](const auto& t) { return t.is_puxc(i, j) && *i < 16 && *j < 15; });
440
bool Optimizer::is_push2(int* i, int* j) {
441
return is_pred([i, j](const auto& t) { return t.is_push2(i, j) && *i < 16 && *j < 16; });
444
bool Optimizer::is_xchg3(int* i, int* j, int* k) {
445
return is_pred([i, j, k](const auto& t) { return t.is_xchg3(i, j, k) && *i < 16 && *j < 16 && *k < 16; });
448
bool Optimizer::is_xc2pu(int* i, int* j, int* k) {
449
return is_pred([i, j, k](const auto& t) { return t.is_xc2pu(i, j, k) && *i < 16 && *j < 16 && *k < 16; });
452
bool Optimizer::is_xcpuxc(int* i, int* j, int* k) {
453
return is_pred([i, j, k](const auto& t) { return t.is_xcpuxc(i, j, k) && *i < 16 && *j < 16 && *k < 15; });
456
bool Optimizer::is_xcpu2(int* i, int* j, int* k) {
457
return is_pred([i, j, k](const auto& t) { return t.is_xcpu2(i, j, k) && *i < 16 && *j < 16 && *k < 16; });
460
bool Optimizer::is_puxc2(int* i, int* j, int* k) {
462
[i, j, k](const auto& t) { return t.is_puxc2(i, j, k) && *i < 16 && *j < 15 && *k < 15 && *j + *k != -1; });
465
bool Optimizer::is_puxcpu(int* i, int* j, int* k) {
466
return is_pred([i, j, k](const auto& t) { return t.is_puxcpu(i, j, k) && *i < 16 && *j < 15 && *k < 15; });
469
bool Optimizer::is_pu2xc(int* i, int* j, int* k) {
470
return is_pred([i, j, k](const auto& t) { return t.is_pu2xc(i, j, k) && *i < 16 && *j < 15 && *k < 14; });
473
bool Optimizer::is_push3(int* i, int* j, int* k) {
474
return is_pred([i, j, k](const auto& t) { return t.is_push3(i, j, k) && *i < 16 && *j < 16 && *k < 16; });
477
bool Optimizer::is_blkswap(int* i, int* j) {
478
return is_pred([i, j](const auto& t) { return t.is_blkswap(i, j) && *i > 0 && *j > 0 && *i <= 16 && *j <= 16; });
481
bool Optimizer::is_blkpush(int* i, int* j) {
482
return is_pred([i, j](const auto& t) { return t.is_blkpush(i, j) && *i > 0 && *i < 16 && *j < 16; });
485
bool Optimizer::is_blkdrop(int* i) {
486
return is_pred([i](const auto& t) { return t.is_blkdrop(i) && *i > 0 && *i < 16; });
489
bool Optimizer::is_blkdrop2(int* i, int* j) {
490
return is_pred([i, j](const auto& t) { return t.is_blkdrop2(i, j) && *i > 0 && *i < 16 && *j > 0 && *j < 16; });
493
bool Optimizer::is_reverse(int* i, int* j) {
494
return is_pred([i, j](const auto& t) { return t.is_reverse(i, j) && *i >= 2 && *i <= 17 && *j < 16; });
497
bool Optimizer::is_nip_seq(int* i, int* j) {
498
return is_pred([i, j](const auto& t) { return t.is_nip_seq(i, j) && *i >= 3 && *i <= 15; });
501
bool Optimizer::is_pop_blkdrop(int* i, int* k) {
502
return is_pred([i, k](const auto& t) { return t.is_pop_blkdrop(i, k) && *i >= *k && *k >= 2 && *k <= 15; }, 3);
505
bool Optimizer::is_2pop_blkdrop(int* i, int* j, int* k) {
507
[i, j, k](const auto& t) { return t.is_2pop_blkdrop(i, j, k) && *i >= *k && *j >= *k && *k >= 2 && *k <= 15; },
511
bool Optimizer::compute_stack_transforms() {
512
StackTransform trans;
513
for (int i = 0; i < l_; i++) {
514
if (!apply_op(trans, *op_[i])) {
524
bool Optimizer::show_stack_transforms() const {
528
StackTransform trans2;
529
std::cerr << "id = " << trans2 << std::endl;
530
for (int i = 0; i < l_; i++) {
532
if (!apply_op(op, *op_[i])) {
533
std::cerr << "* (" << *op_[i] << " = invalid)\n";
537
std::cerr << "* " << *op_[i] << " = " << op << " -> " << trans2 << std::endl;
541
StackTransform trans;
542
for (int i = 0; i < l_; i++) {
543
std::cerr << trans << std::endl << *op_[i] << " -> ";
544
if (!apply_op(trans, *op_[i])) {
545
std::cerr << " <not-applicable>" << std::endl;
549
std::cerr << trans << std::endl;
553
bool Optimizer::find_at_least(int pb) {
556
// show_stack_transforms();
558
return (is_push_const(&i, &c) && rewrite_push_const(i, c)) || (is_nop() && rewrite_nop()) ||
559
(!(mode_ & 1) && is_const_rot(&c) && rewrite_const_rot(c)) ||
560
(is_const_push_xchgs() && rewrite_const_push_xchgs()) || (is_const_pop(&c, &i) && rewrite_const_pop(c, i)) ||
561
(is_xchg(&i, &j) && rewrite(AsmOp::Xchg(i, j))) || (is_push(&i) && rewrite(AsmOp::Push(i))) ||
562
(is_pop(&i) && rewrite(AsmOp::Pop(i))) || (is_pop_pop(&i, &j) && rewrite(AsmOp::Pop(i), AsmOp::Pop(j))) ||
563
(is_xchg_xchg(&i, &j, &k, &l) && rewrite(AsmOp::Xchg(i, j), AsmOp::Xchg(k, l))) ||
565
((is_rot() && rewrite(AsmOp::Custom("ROT", 3, 3))) || (is_rotrev() && rewrite(AsmOp::Custom("-ROT", 3, 3))) ||
566
(is_2dup() && rewrite(AsmOp::Custom("2DUP", 2, 4))) ||
567
(is_2swap() && rewrite(AsmOp::Custom("2SWAP", 2, 4))) ||
568
(is_2over() && rewrite(AsmOp::Custom("2OVER", 2, 4))) ||
569
(is_tuck() && rewrite(AsmOp::Custom("TUCK", 2, 3))) ||
570
(is_2drop() && rewrite(AsmOp::Custom("2DROP", 2, 0))) || (is_xchg2(&i, &j) && rewrite(AsmOp::Xchg2(i, j))) ||
571
(is_xcpu(&i, &j) && rewrite(AsmOp::XcPu(i, j))) || (is_puxc(&i, &j) && rewrite(AsmOp::PuXc(i, j))) ||
572
(is_push2(&i, &j) && rewrite(AsmOp::Push2(i, j))) || (is_blkswap(&i, &j) && rewrite(AsmOp::BlkSwap(i, j))) ||
573
(is_blkpush(&i, &j) && rewrite(AsmOp::BlkPush(i, j))) || (is_blkdrop(&i) && rewrite(AsmOp::BlkDrop(i))) ||
574
(is_push_rot(&i) && rewrite(AsmOp::Push(i), AsmOp::Custom("ROT"))) ||
575
(is_push_rotrev(&i) && rewrite(AsmOp::Push(i), AsmOp::Custom("-ROT"))) ||
576
(is_push_xchg(&i, &j, &k) && rewrite(AsmOp::Push(i), AsmOp::Xchg(j, k))) ||
577
(is_reverse(&i, &j) && rewrite(AsmOp::BlkReverse(i, j))) ||
578
(is_blkdrop2(&i, &j) && rewrite(AsmOp::BlkDrop2(i, j))) ||
579
(is_nip_seq(&i, &j) && rewrite(AsmOp::Xchg(i, j), AsmOp::BlkDrop(i))) ||
580
(is_pop_blkdrop(&i, &k) && rewrite(AsmOp::Pop(i), AsmOp::BlkDrop(k))) ||
581
(is_2pop_blkdrop(&i, &j, &k) && (k >= 3 && k <= 13 && i != j + 1 && i <= 15 && j <= 14
582
? rewrite(AsmOp::Xchg2(j + 1, i), AsmOp::BlkDrop(k + 2))
583
: rewrite(AsmOp::Pop(i), AsmOp::Pop(j), AsmOp::BlkDrop(k)))) ||
584
(is_xchg3(&i, &j, &k) && rewrite(AsmOp::Xchg3(i, j, k))) ||
585
(is_xc2pu(&i, &j, &k) && rewrite(AsmOp::Xc2Pu(i, j, k))) ||
586
(is_xcpuxc(&i, &j, &k) && rewrite(AsmOp::XcPuXc(i, j, k))) ||
587
(is_xcpu2(&i, &j, &k) && rewrite(AsmOp::XcPu2(i, j, k))) ||
588
(is_puxc2(&i, &j, &k) && rewrite(AsmOp::PuXc2(i, j, k))) ||
589
(is_puxcpu(&i, &j, &k) && rewrite(AsmOp::PuXcPu(i, j, k))) ||
590
(is_pu2xc(&i, &j, &k) && rewrite(AsmOp::Pu2Xc(i, j, k))) ||
591
(is_push3(&i, &j, &k) && rewrite(AsmOp::Push3(i, j, k)))));
594
bool Optimizer::find() {
595
if (!compute_stack_transforms()) {
598
for (int pb = l_; pb > 0; --pb) {
599
if (find_at_least(pb)) {
606
bool Optimizer::optimize() {
616
AsmOpConsList optimize_code_head(AsmOpConsList op_list, int mode) {
617
Optimizer opt(std::move(op_list), op_rewrite_comments, mode);
619
return opt.extract_code();
622
AsmOpConsList optimize_code(AsmOpConsList op_list, int mode) {
623
std::vector<std::unique_ptr<AsmOp>> v;
625
if (!op_list->car->is_comment()) {
626
op_list = optimize_code_head(std::move(op_list), mode);
629
v.push_back(std::move(op_list->car));
630
op_list = std::move(op_list->cdr);
633
for (auto it = v.rbegin(); it < v.rend(); ++it) {
634
op_list = AsmOpCons::cons(std::move(*it), std::move(op_list));
636
return std::move(op_list);
639
void optimize_code(AsmOpList& ops) {
640
AsmOpConsList op_list;
641
for (auto it = ops.list_.rbegin(); it < ops.list_.rend(); ++it) {
642
op_list = AsmOpCons::cons(std::make_unique<AsmOp>(std::move(*it)), std::move(op_list));
644
for (int mode : {1, 1, 1, 1, 0, 0, 0, 0}) {
645
op_list = optimize_code(std::move(op_list), mode);
649
ops.list_.push_back(std::move(*(op_list->car)));
650
op_list = std::move(op_list->cdr);