Skip to content
Prev Previous commit
Next Next commit
replace lir find call with 2 lir::store
  • Loading branch information
ValKmjolnir committed Feb 10, 2025
commit 1a5c931ae8e0cae7256f71e10ed1cc00b7bc0eb1
65 changes: 65 additions & 0 deletions godel-script/godel-frontend/src/ir/inst_combine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,69 @@ void inst_elimination_worker::copy(souffle_rule_impl* impl) {
delete impl_blk;
}

void replace_find_call::visit_block(lir::block* node) {
bool has_find_call = false;
for (auto i : node->get_content()) {
if (i->get_kind() != lir::inst_kind::inst_call) {
continue;
}
auto call = reinterpret_cast<lir::call*>(i);
if (call->get_func_kind() == lir::call::kind::find &&
call->get_function_name() == "find") {
has_find_call = true;
break;
}
}

if (has_find_call) {
std::vector<lir::inst*> new_content;
for (auto i : node->get_content()) {
if (i->get_kind() != lir::inst_kind::inst_call) {
new_content.push_back(i);
continue;
}

auto call = reinterpret_cast<lir::call*>(i);
if (call->get_func_kind() != lir::call::kind::find ||
call->get_function_name() != "find") {
new_content.push_back(i);
continue;
}

auto dst = call->get_return();
auto arg0 = call->get_arguments()[0];
auto arg1 = call->get_arguments()[1];
auto new_block = new lir::block(call->get_location());
new_block->set_use_comma();
new_content.push_back(new_block);

new_block->add_new_content(new lir::store(arg0, dst, call->get_location()));
new_block->add_new_content(new lir::store(arg1, arg0, call->get_location()));

delete i;
}
node->get_mutable_content().swap(new_content);
} else {
for (auto i : node->get_content()) {
i->accept(this);
}
}
}

bool replace_find_call::run() {
for (auto impl : ctx->rule_impls) {
impl->get_block()->accept(this);
}
for (auto impl : ctx->database_get_table) {
impl->get_block()->accept(this);
}
for (auto impl : ctx->schema_get_field) {
impl->get_block()->accept(this);
}
for (auto impl : ctx->schema_data_constraint_impls) {
impl->get_block()->accept(this);
}
return true;
}

}
12 changes: 12 additions & 0 deletions godel-script/godel-frontend/src/ir/inst_combine.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,16 @@ class inst_elimination_worker: public lir::inst_visitor {
}
};

class replace_find_call: public pass {
private:
void visit_block(lir::block*) override;

public:
replace_find_call(ir_context& c): pass(pass_kind::ps_replace_find_call, c) {}
const char* get_name() const override {
return "[Transform] Replace Find Call";
}
bool run() override;
};

}
1 change: 1 addition & 0 deletions godel-script/godel-frontend/src/ir/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum class pass_kind {
ps_remove_unused,
ps_remove_unused_type,
ps_inst_combine,
ps_replace_find_call,
ps_flatten_nested_block,
ps_aggregator_inline_remark,
ps_ungrounded_check,
Expand Down
1 change: 1 addition & 0 deletions godel-script/godel-frontend/src/ir/pass_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void pass_manager::run(ir_context& ctx, const cli::configure& conf) {
ordered_pass_list.push_back(new unused_type_alias_remove_pass(ctx));
}
if (!conf.count(cli::option::cli_disable_inst_combine)) {
ordered_pass_list.push_back(new replace_find_call(ctx));
ordered_pass_list.push_back(new inst_combine_pass(ctx));
}
ordered_pass_list.push_back(new flatten_nested_block(ctx));
Expand Down