Skip to content

Commit 83ca804

Browse files
committed
[Tolk] Peephole optimization: NOT+THROWIFNOT to THROWIF
1 parent eb8cdfb commit 83ca804

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

tolk-tester/tests/logical-operators.tolk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,15 @@ These are moments of future optimizations. For now, it's more than enough.
351351
DUP // x x
352352
IFNOTJMP:<{ // x
353353
DROP //
354-
1 PUSHINT // '5=1
354+
1 PUSHINT // '7=1
355355
}> // x
356356
DUP // x x
357357
IFNOTJMP:<{ // x
358358
DROP //
359-
1 PUSHINT // '6=1
359+
1 PUSHINT // '8=1
360360
}> // x
361361
100 THROWIFNOT
362-
-4 PUSHINT // '9=-4
362+
-4 PUSHINT // '11=-4
363363
}>
364364
"""
365365

tolk/optimize.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,29 @@ bool Optimizer::detect_rewrite_SWAP_STxxxR() {
399399
return false;
400400
}
401401

402+
// pattern `NOT` + `123 THROWIFNOT` -> `123 THROWIF` (and THROWIF -> THROWIFNOT)
403+
bool Optimizer::detect_rewrite_NOT_THROWIF() {
404+
bool first_not = op_[0]->is_custom() && op_[0]->op == "NOT";
405+
if (!first_not || pb_ < 2 || !op_[1]->is_custom()) {
406+
return false;
407+
}
408+
409+
static const char* ends_with[] = {" THROWIF", " THROWIFNOT"};
410+
static const char* repl_with[] = {" THROWIFNOT", " THROWIF"};
411+
412+
std::string_view f = op_[1]->op;
413+
for (size_t i = 0; i < std::size(ends_with); ++i) {
414+
if (f.ends_with(ends_with[i])) {
415+
p_ = 2;
416+
q_ = 1;
417+
oq_[0] = std::make_unique<AsmOp>(AsmOp::Custom(op_[0]->loc, op_[1]->op.substr(0, f.rfind(' ')) + repl_with[i], 1, 0));
418+
return true;
419+
}
420+
}
421+
422+
return false;
423+
}
424+
402425
bool Optimizer::is_push_const(int* i, int* c) const {
403426
return pb_ >= 3 && pb_ <= l2_ && tr_[pb_ - 1].is_push_const(i, c);
404427
}
@@ -821,6 +844,7 @@ bool Optimizer::find_at_least(int pb) {
821844
detect_rewrite_MY_store_int() || detect_rewrite_MY_skip_bits() || detect_rewrite_NEWC_PUSH_STUR() ||
822845
detect_rewrite_LDxx_DROP() ||
823846
detect_rewrite_SWAP_symmetric() || detect_rewrite_SWAP_PUSH_STUR() || detect_rewrite_SWAP_STxxxR() ||
847+
detect_rewrite_NOT_THROWIF() ||
824848
(!(mode_ & 1) &&
825849
((is_rot() && rewrite(AsmOp::Custom(loc, "ROT", 3, 3))) || (is_rotrev() && rewrite(AsmOp::Custom(loc, "-ROT", 3, 3))) ||
826850
(is_2dup() && rewrite(AsmOp::Custom(loc, "2DUP", 2, 4))) ||

tolk/pipe-ast-to-legacy.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,21 +2103,12 @@ static void process_block_statement(V<ast_block_statement> v, CodeBlob& code) {
21032103
}
21042104

21052105
static void process_assert_statement(V<ast_assert_statement> v, CodeBlob& code) {
2106-
std::vector<AnyExprV> args(3);
2107-
if (auto v_not = v->get_cond()->try_as<ast_unary_operator>(); v_not && v_not->tok == tok_logical_not) {
2108-
args[0] = v->get_thrown_code();
2109-
args[1] = v->get_cond()->as<ast_unary_operator>()->get_rhs();
2110-
args[2] = createV<ast_bool_const>(v->loc, true);
2111-
args[2]->mutate()->assign_inferred_type(TypeDataInt::create());
2112-
} else {
2113-
args[0] = v->get_thrown_code();
2114-
args[1] = v->get_cond();
2115-
args[2] = createV<ast_bool_const>(v->loc, false);
2116-
args[2]->mutate()->assign_inferred_type(TypeDataInt::create());
2117-
}
2106+
std::vector ir_thrown_code = pre_compile_expr(v->get_thrown_code(), code);
2107+
std::vector ir_cond = pre_compile_expr(v->get_cond(), code);
2108+
tolk_assert(ir_cond.size() == 1 && ir_thrown_code.size() == 1);
21182109

2110+
std::vector args_vars = { ir_thrown_code[0], ir_cond[0], code.create_int(v->loc, 0, "(assert-0)") };
21192111
FunctionPtr builtin_sym = lookup_function("__throw_if_unless");
2120-
std::vector<var_idx_t> args_vars = pre_compile_tensor(code, args);
21212112
gen_op_call(code, TypeDataVoid::create(), v->loc, std::move(args_vars), builtin_sym, "(throw-call)");
21222113
}
21232114

tolk/tolk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ struct Optimizer {
906906
bool detect_rewrite_SWAP_symmetric();
907907
bool detect_rewrite_SWAP_PUSH_STUR();
908908
bool detect_rewrite_SWAP_STxxxR();
909+
bool detect_rewrite_NOT_THROWIF();
909910

910911
AsmOpConsList extract_code();
911912
};

0 commit comments

Comments
 (0)