From 71d51ec94c6dd7c939351e191e68f0cb12e02e1c Mon Sep 17 00:00:00 2001 From: nefanov Date: Thu, 21 Apr 2022 09:17:17 +0300 Subject: [PATCH] [FEIR, MAPLEALL] Required FEIR / MAPLEALL changes for libc mem*,str* intrinsiccall support --- .../clang/include/ast_builtin_func.def | 1 + src/hir2mpl/common/src/feir_stmt.cpp | 20 ++++++++++++++++--- .../include/cg/aarch64/aarch64_cgfunc.h | 3 ++- src/mapleall/maple_be/include/cg/cgfunc.h | 3 ++- .../maple_be/include/cg/x86_64/x64_cgfunc.h | 3 ++- src/mapleall/maple_be/src/be/lower.cpp | 6 ++++++ .../src/cg/aarch64/aarch64_cgfunc.cpp | 3 ++- src/mapleall/maple_be/src/cg/cgfunc.cpp | 13 ++++++++++++ .../maple_be/src/cg/x86_64/x64_cgfunc.cpp | 3 ++- src/mapleall/maple_ir/include/intrinsic_c.def | 6 +++++- 10 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/hir2mpl/ast_input/clang/include/ast_builtin_func.def b/src/hir2mpl/ast_input/clang/include/ast_builtin_func.def index 08747d4fbf..1ecffd079c 100644 --- a/src/hir2mpl/ast_input/clang/include/ast_builtin_func.def +++ b/src/hir2mpl/ast_input/clang/include/ast_builtin_func.def @@ -6,6 +6,7 @@ BUILTIN_FUNC(strcpy) BUILTIN_FUNC(strcmp) BUILTIN_FUNC(strlen) BUILTIN_FUNC(strchr) +BUILTIN_FUNC(strrchr) BUILTIN_FUNC(memcmp) BUILTIN_FUNC(memcpy) BUILTIN_FUNC(memset) diff --git a/src/hir2mpl/common/src/feir_stmt.cpp b/src/hir2mpl/common/src/feir_stmt.cpp index d638b5847a..9d83f63c1c 100755 --- a/src/hir2mpl/common/src/feir_stmt.cpp +++ b/src/hir2mpl/common/src/feir_stmt.cpp @@ -2164,8 +2164,16 @@ std::list FEIRStmtIntrinsicCallAssign::GenMIRStmtsImpl(MIRBuilder &mi args.push_back(node); } } - stmtCall = mirBuilder.CreateStmtIntrinsicCall(intrinsicId, std::move(args), TyIdx(0)); - } else if (intrinsicId == INTRN_C_memset) { + MIRSymbol *retVarSym = nullptr; + if (var != nullptr) { + retVarSym = var->GenerateLocalMIRSymbol(mirBuilder); + stmtCall = mirBuilder.CreateStmtIntrinsicCallAssigned(intrinsicId, std::move(args), retVarSym); + } else { + stmtCall = mirBuilder.CreateStmtIntrinsicCall(intrinsicId, std::move(args), TyIdx(0)); + } + } else if (intrinsicId == INTRN_C_memset || intrinsicId == INTRN_C_memmove || + intrinsicId == INTRN_C_strcpy || intrinsicId == INTRN_C_strncpy || + intrinsicId == INTRN_C_memcpy) { MapleVector args(mirBuilder.GetCurrentFuncCodeMpAllocator()->Adapter()); if (exprList != nullptr) { for (const auto &expr : *exprList) { @@ -2173,7 +2181,13 @@ std::list FEIRStmtIntrinsicCallAssign::GenMIRStmtsImpl(MIRBuilder &mi args.push_back(node); } } - stmtCall = mirBuilder.CreateStmtIntrinsicCall(INTRN_C_memset, std::move(args), TyIdx(0)); + MIRSymbol *retVarSym = nullptr; + if (var != nullptr) { + retVarSym = var->GenerateLocalMIRSymbol(mirBuilder); + stmtCall = mirBuilder.CreateStmtIntrinsicCallAssigned(intrinsicId, std::move(args), retVarSym); + } else { + stmtCall = mirBuilder.CreateStmtIntrinsicCall(intrinsicId, std::move(args), TyIdx(0)); + } } else if (intrinsicId >= INTRN_vector_zip_v2i32 && intrinsicId <= INTRN_vector_zip_v2f32) { MapleVector args(mirBuilder.GetCurrentFuncCodeMpAllocator()->Adapter()); if (exprList != nullptr) { diff --git a/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h b/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h index c3bedb8a39..6f104a9105 100644 --- a/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h +++ b/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h @@ -125,7 +125,8 @@ class AArch64CGFunc : public CGFunc { void SelectIcall(IcallNode &icallNode, Operand &fptrOpnd) override; void SelectIntrinCall(IntrinsiccallNode &intrinsicCallNode) override; Operand *SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrinsicopNode, std::string name) override; - Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinsicopNode, PrimType retType, std::string &name) override; + Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinsicopNode, PrimType retType, + const std::string &name) override; Operand *SelectCclz(IntrinsicopNode &intrinsicopNode) override; Operand *SelectCctz(IntrinsicopNode &intrinsicopNode) override; Operand *SelectCpopcount(IntrinsicopNode &intrinsicopNode) override; diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index fae5350906..47aaf524dc 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -194,7 +194,8 @@ class CGFunc { virtual void SelectIcall(IcallNode &icallNode, Operand &fptrOpnd) = 0; virtual void SelectIntrinCall(IntrinsiccallNode &intrinsiccallNode) = 0; virtual Operand *SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrinsicopNode, std::string name) = 0; - virtual Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinsicopNode, PrimType retType, std::string &name) = 0; + virtual Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinsicopNode, PrimType retType, + const std::string &name) = 0; virtual Operand *SelectCclz(IntrinsicopNode &intrinsicopNode) = 0; virtual Operand *SelectCctz(IntrinsicopNode &intrinsicopNode) = 0; virtual Operand *SelectCpopcount(IntrinsicopNode &intrinsicopNode) = 0; diff --git a/src/mapleall/maple_be/include/cg/x86_64/x64_cgfunc.h b/src/mapleall/maple_be/include/cg/x86_64/x64_cgfunc.h index df262b22d7..ec2ebf29bb 100644 --- a/src/mapleall/maple_be/include/cg/x86_64/x64_cgfunc.h +++ b/src/mapleall/maple_be/include/cg/x86_64/x64_cgfunc.h @@ -209,7 +209,8 @@ class X64CGFunc : public CGFunc { RegOperand *SelectVectorSum(PrimType rtype, Operand *o1, PrimType oType) override; RegOperand *SelectVectorTableLookup(PrimType rType, Operand *o1, Operand *o2) override; RegOperand *SelectVectorWiden(PrimType rType, Operand *o1, PrimType otyp, bool isLow) override; - Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinopNode, PrimType retType, std::string &name) override; + Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinopNode, PrimType retType, + const std::string &name) override; Operand &CreateFPImmZero(PrimType primType) override; void ProcessLazyBinding() override; void DBGFixCallFrameLocationOffsets() override; diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 6a00d972ce..c5f2333920 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -3749,6 +3749,12 @@ bool CGLowerer::IsIntrinsicOpHandledAtLowerLevel(MIRIntrinsicID intrinsic) const case INTRN_C___sync_synchronize: case INTRN_C__builtin_return_address: case INTRN_C__builtin_extract_return_addr: + case INTRN_C_memcmp: + case INTRN_C_strlen: + case INTRN_C_strcmp: + case INTRN_C_strncmp: + case INTRN_C_strchr: + case INTRN_C_strrchr: return true; #endif default: diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp index f88b63df39..42a6aa3266 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -4865,7 +4865,8 @@ Operand *AArch64CGFunc::SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrnNode return dst; } -Operand *AArch64CGFunc::SelectIntrinsicOpWithNParams(IntrinsicopNode &intrnNode, PrimType retType, std::string &name) { +Operand *AArch64CGFunc::SelectIntrinsicOpWithNParams(IntrinsicopNode &intrnNode, PrimType retType, + const std::string &name) { MapleVector argNodes = intrnNode.GetNopnd(); std::vector opndVec; std::vector opndTypes; diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index 469c48d900..311b550ac6 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -626,6 +626,19 @@ Operand *HandleIntrinOp(const BaseNode &parent, BaseNode &expr, CGFunc &cgFunc) // int case INTRN_C_ffs: return cgFunc.SelectIntrinsicOpWithOneParam(intrinsicopNode, "ffs"); + // libc mem* and str* functions as intrinsicops + case INTRN_C_memcmp: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "memcmp"); + case INTRN_C_strlen: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_u64, "strlen"); + case INTRN_C_strcmp: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "strcmp"); + case INTRN_C_strncmp: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "strncmp"); + case INTRN_C_strchr: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strchr"); + case INTRN_C_strrchr: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strrchr"); case INTRN_C_clz32: case INTRN_C_clz64: diff --git a/src/mapleall/maple_be/src/cg/x86_64/x64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/x86_64/x64_cgfunc.cpp index a204a5c99a..979d7e5e9e 100644 --- a/src/mapleall/maple_be/src/cg/x86_64/x64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/x86_64/x64_cgfunc.cpp @@ -665,7 +665,8 @@ Operand &X64CGFunc::CreateFPImmZero(PrimType primType) { Operand *a; return *a; } -Operand *X64CGFunc::SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinopNode, PrimType retType, std::string &name) { +Operand *X64CGFunc::SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinopNode, PrimType retType, + const std::string &name) { CHECK_FATAL(false, "NIY"); return nullptr; } diff --git a/src/mapleall/maple_ir/include/intrinsic_c.def b/src/mapleall/maple_ir/include/intrinsic_c.def index e6771edb94..a91560d8c4 100644 --- a/src/mapleall/maple_ir/include/intrinsic_c.def +++ b/src/mapleall/maple_ir/include/intrinsic_c.def @@ -25,6 +25,10 @@ DEF_MIR_INTRINSIC(C_strncpy,\ "strncpy", 0, kArgTyVoid, kArgTyPtr, kArgTyPtr, kArgTyU64) DEF_MIR_INTRINSIC(C_strlen,\ "strlen", INTRNNOSIDEEFFECT | INTRNISPURE, kArgTyU64, kArgTyPtr) +DEF_MIR_INTRINSIC(C_strchr,\ + "strchr", INTRNNOSIDEEFFECT | INTRNISPURE, kArgTyPtr, kArgTyPtr, kArgTyU32) +DEF_MIR_INTRINSIC(C_strrchr,\ + "strrchr", INTRNNOSIDEEFFECT | INTRNISPURE, kArgTyPtr, kArgTyPtr, kArgTyU32) DEF_MIR_INTRINSIC(C_memcmp,\ "memcmp", INTRNNOSIDEEFFECT | INTRNISPURE, kArgTyI32, kArgTyPtr, kArgTyPtr, kArgTyU64) DEF_MIR_INTRINSIC(C_memcpy,\ @@ -296,4 +300,4 @@ DEF_MIR_INTRINSIC(C___atomic_is_lock_free,\ DEF_MIR_INTRINSIC(C___atomic_compare_exchange_n,\ "__atomic_compare_exchange_n", INTRNATOMIC, kArgTyU1, kArgTyPtr, kArgTyPtr, kArgTyDynany, kArgTyU1, kArgTyI32, kArgTyI32) DEF_MIR_INTRINSIC(C___atomic_compare_exchange,\ - "__atomic_compare_exchange", INTRNATOMIC, kArgTyU1, kArgTyPtr, kArgTyPtr, kArgTyPtr, kArgTyU1, kArgTyI32, kArgTyI32) \ No newline at end of file + "__atomic_compare_exchange", INTRNATOMIC, kArgTyU1, kArgTyPtr, kArgTyPtr, kArgTyPtr, kArgTyU1, kArgTyI32, kArgTyI32) -- Gitee