From 5c1278280ab75aafe3a411b2ca0d9e8be598d8ca Mon Sep 17 00:00:00 2001 From: ridethepig Date: Thu, 8 Jun 2023 17:13:53 +0800 Subject: [PATCH] optimize local array init --- .vscode/launch.json | 2 +- include/common.h | 1 + include/llir_value.h | 1 + src/visitor.cpp | 30 ++++++++++++++---------------- src/visitor_llir_gen.cpp | 4 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 6b467a7..68c7082 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "request": "launch", "name": "Debug", "program": "${workspaceFolder}/build/sysy", - "args": ["../sysytests/functional_2022/21_if_test2.sy", "-S", "-o", "build/21_my.ll", "-O1", "-emit-llvm"], + "args": ["../sysytests/functional_2022/34_arr_expr_len.sy", "-S", "-o", "build/21_my.ll", "-O1", "-emit-llvm"], "cwd": "${workspaceFolder}" } ] diff --git a/include/common.h b/include/common.h index 7426049..5d5c1cd 100644 --- a/include/common.h +++ b/include/common.h @@ -1,5 +1,6 @@ #pragma once #include "3rdparty/easylogging++.h" +#include "fmt/core.h" #include #include #include diff --git a/include/llir_value.h b/include/llir_value.h index 2c63b7f..f1032c4 100644 --- a/include/llir_value.h +++ b/include/llir_value.h @@ -199,6 +199,7 @@ public: class ConstantArr : public Constant { public: + //! By default, this value_list is flattened and with size equal to declared size std::vector value_list; ConstantArr(const std::string &name, const std::vector &value_list, std::shared_ptr type) : Constant(name, type), value_list(value_list) {} diff --git a/src/visitor.cpp b/src/visitor.cpp index a1e9fa0..9cc060f 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -67,7 +67,6 @@ void _sysy_stoptime(int lineno); */ Visitor::Visitor(SysyLexer &) { -#pragma region RegisterLibFunc auto fparam_i32 = std::make_shared("", TypeHelper::TYPE_I32); auto pointer_type = std::make_shared(TypeHelper::TYPE_I32); auto fparam_ptr_i32 = std::make_shared("", pointer_type); @@ -107,7 +106,9 @@ Visitor::Visitor(SysyLexer &) { func_stoptime->fparam_list = {fparam_i32}; _func_tab.push_name("stoptime", func_stoptime); -#pragma endregion + auto func_memset = std::make_shared("memset", TypeHelper::TYPE_VOID); + func_memset->fparam_list = {fparam_ptr_i32, fparam_i32, fparam_i32}; + _func_tab.push_name("memset", func_memset); } std::any Visitor::visitConstDecl(SysyParser::ConstDeclContext *ctx) { @@ -235,11 +236,17 @@ std::any Visitor::visitVarDef(SysyParser::VarDefContext *ctx) { for (int i = 1; i < dim_list.size(); ++i) { base_ptr = build_InstGEP(base_ptr, {CONST0, CONST0}, _state.current_bb); } + auto inst_call = build_InstCall( + _func_tab.get_name("memset").value(), + {base_ptr, CONST0, ConstantInt::make_shared(array_value.size() * 4)}, + _state.current_bb + ); // TODO: BAAU-2021 calls memset in `libc` (not sysylib) to optimize this process - build_InstStore(array_value[0] ? array_value[0] : CONST0, base_ptr, _state.current_bb); + if (array_value[0]) build_InstStore(array_value[0], base_ptr, _state.current_bb); for (int i = 1; i < array_value.size(); ++i) { + if (!array_value[i]) continue; auto ptr = build_InstGEP(base_ptr, {ConstantInt::make_shared(i)}, _state.current_bb); - build_InstStore(array_value[i] ? array_value[i] : CONST0, ptr, _state.current_bb); + build_InstStore(array_value[i], ptr, _state.current_bb); } } // If there is no init expr, let it be @@ -288,17 +295,9 @@ std::any Visitor::visitInitVal(SysyParser::InitValContext *ctx) { } for (auto init_val : ctx->initVal()) { if (init_val->exp()) { - if (_state.isGlobalIint) { - auto const_value = any_to_Value(visitInitVal(init_val)); - // should evaluate to const int - sysy_assert(Value::is(const_value)); - cur_arr.push_back(const_value); - } - else { - // God knows what it evaluates to - auto exp_value = any_to_Value(visitInitVal(init_val)); - cur_arr.push_back(exp_value); - } + auto exp_value = any_to_Value(visitInitVal(init_val)); + if (_state.isGlobalIint) assert(Value::is(exp_value)); + cur_arr.push_back(exp_value); } else { // evaluate sub-array @@ -614,7 +613,6 @@ std::any Visitor::visitUnaryExp(SysyParser::UnaryExpContext *ctx) { } else if (ctx->IDENT()) { // Fucntion call - // TODO: buildCall/isRealParam // TODO: Handle string & putf() auto func_name = ctx->IDENT()->getText(); VLOG(5) << "Calling Func: " << func_name; diff --git a/src/visitor_llir_gen.cpp b/src/visitor_llir_gen.cpp index 51f2cf6..76738fe 100644 --- a/src/visitor_llir_gen.cpp +++ b/src/visitor_llir_gen.cpp @@ -11,8 +11,8 @@ namespace CompSysY { -const std::array libfunc_list = - {"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime"}; +const std::array libfunc_list = + {"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime", "memset"}; // On generation, the array is flattened, thus we have to calculate them std::shared_ptr gen_arr_hierarchy(