From 11e10cb9c5f8ba9ad7ed2b58f972c83523c79c61 Mon Sep 17 00:00:00 2001 From: linma Date: Wed, 6 Oct 2021 14:49:04 -0700 Subject: [PATCH 1/4] enable loopunroll in O2 --- .../maple_me/include/me_scalar_analysis.h | 4 +-- .../maple_me/src/me_loop_unrolling.cpp | 9 ++++--- .../maple_me/src/me_scalar_analysis.cpp | 25 ++++++++++++++----- src/mapleall/maple_phase/include/phases.def | 1 + 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/mapleall/maple_me/include/me_scalar_analysis.h b/src/mapleall/maple_me/include/me_scalar_analysis.h index 09aaafe7d1..552877597f 100644 --- a/src/mapleall/maple_me/include/me_scalar_analysis.h +++ b/src/mapleall/maple_me/include/me_scalar_analysis.h @@ -322,8 +322,8 @@ class LoopScalarAnalysisResult { std::set> allCRNodes; void DumpTripCount(const CR &cr, int32 value, const MeExpr *expr); uint32 ComputeTripCountWithCR(const CR &cr, const OpMeExpr &opMeExpr, int32 value); - uint32 ComputeTripCountWithSimpleConstCR(const OpMeExpr &opMeExpr, int32 value, int32 start, int32 stride) const; + uint32 ComputeTripCountWithSimpleConstCR(const OpMeExpr &opMeExpr, int32 value, int32 start, int32 stride, bool negop = false) const; bool computeTripCountForLoopUnroll = true; }; } // namespace maple -#endif // MAPLE_ME_INCLUDE_SCALAR_ANALYSIS_H \ No newline at end of file +#endif // MAPLE_ME_INCLUDE_SCALAR_ANALYSIS_H diff --git a/src/mapleall/maple_me/src/me_loop_unrolling.cpp b/src/mapleall/maple_me/src/me_loop_unrolling.cpp index c52347a6e8..b2ce4cda73 100644 --- a/src/mapleall/maple_me/src/me_loop_unrolling.cpp +++ b/src/mapleall/maple_me/src/me_loop_unrolling.cpp @@ -1236,13 +1236,14 @@ void LoopUnrollingExecutor::SetNestedLoop(const IdentifyLoops &meLoop, } bool LoopUnrollingExecutor::IsDoWhileLoop(MeFunction &func, LoopDesc &loop) const { + if (!loop.head || !loop.exitBB) return false; for (auto succ : loop.head->GetSucc()) { - if (!loop.Has(*succ)) { + if (!loop.Has(*succ) && (succ != loop.exitBB)) { return false; } } - auto exitBB = func.GetCfg()->GetBBFromID(loop.inloopBB2exitBBs.begin()->first); - for (auto pred : exitBB->GetPred()) { + + for (auto pred : loop.exitBB->GetPred()) { if (!loop.Has(*pred)) { return false; } @@ -1405,9 +1406,11 @@ void MELoopUnrolling::GetAnalysisDependence(maple::AnalysisDep &aDep) const { bool MELoopUnrolling::PhaseRun(maple::MeFunction &f) { // Do loop unrolling only when the function is hot in profile. +#if 0 if (!ProfileCheck(f)) { return false; } +#endif IdentifyLoops *meLoop = GET_ANALYSIS(MELoopAnalysis, f); if (meLoop == nullptr) { return false; diff --git a/src/mapleall/maple_me/src/me_scalar_analysis.cpp b/src/mapleall/maple_me/src/me_scalar_analysis.cpp index 36b4eb05ef..eab26d98b8 100644 --- a/src/mapleall/maple_me/src/me_scalar_analysis.cpp +++ b/src/mapleall/maple_me/src/me_scalar_analysis.cpp @@ -69,7 +69,7 @@ CRNode *CR::ComputeValueAtIteration(uint32 i, LoopScalarAnalysisResult &scalarAn MeExpr *LoopScalarAnalysisResult::TryToResolveVar(MeExpr &expr, std::set &visitedPhi, MeExpr &dummyExpr) { - CHECK_FATAL(expr.GetMeOp() == kMeOpVar, "must be"); + CHECK_FATAL((expr.GetMeOp() == kMeOpVar || expr.GetMeOp() == kMeOpReg), "must be"); auto *var = static_cast(&expr); if (var->GetDefBy() == kDefByStmt && !var->GetDefStmt()->GetRHS()->IsLeaf()) { return nullptr; @@ -78,7 +78,8 @@ MeExpr *LoopScalarAnalysisResult::TryToResolveVar(MeExpr &expr, std::setGetDefStmt()->GetRHS(); } if (var->GetDefBy() == kDefByStmt) { - CHECK_FATAL(var->GetDefStmt()->GetRHS()->GetMeOp() == kMeOpVar, "must be"); + CHECK_FATAL((var->GetDefStmt()->GetRHS()->GetMeOp() == kMeOpVar || + var->GetDefStmt()->GetRHS()->GetMeOp() == kMeOpReg), "must be"); return TryToResolveVar(*(var->GetDefStmt()->GetRHS()), visitedPhi, dummyExpr); } @@ -986,7 +987,7 @@ uint32 LoopScalarAnalysisResult::ComputeTripCountWithCR(const CR &cr, const OpMe } uint32 LoopScalarAnalysisResult::ComputeTripCountWithSimpleConstCR(const OpMeExpr &opMeExpr, int32 value, - int32 start, int32 stride) const { + int32 start, int32 stride, bool negOp) const { constexpr int32 javaIntMinValue = -2147483648; // -8fffffff constexpr int32 javaIntMaxValue = 2147483647; // 7fffffff CHECK_FATAL(stride != 0, "stride must not be zero"); @@ -1001,7 +1002,18 @@ uint32 LoopScalarAnalysisResult::ComputeTripCountWithSimpleConstCR(const OpMeExp return 0; } uint32 remainder = (value < start) ? ((start - value) % stride) : ((value - start) % stride); - switch (opMeExpr.GetOp()) { + Opcode op = opMeExpr.GetOp(); + if (negOp) { + switch (op) { + case OP_ge: op = OP_lt; break; + case OP_le: op = OP_gt; break; + case OP_gt: op = OP_le; break; + case OP_lt: op = OP_ge; break; + default: + break; + } + } + switch (op) { case OP_ge: { // < if (start >= value || start - stride >= value) { return 0; @@ -1116,7 +1128,7 @@ TripCountType LoopScalarAnalysisResult::ComputeTripCount(MeFunction &func, uint3 auto *opMeExpr = static_cast(brMeStmt->GetOpnd()); MeExpr *opnd1 = opMeExpr->GetOpnd(0); MeExpr *opnd2 = opMeExpr->GetOpnd(1); - if (opnd1->GetPrimType() != PTY_i32 || opnd2->GetPrimType() != PTY_i32) { + if (!IsPrimitiveInteger(opnd1->GetPrimType()) || !IsPrimitiveInteger(opnd2->GetPrimType())) { return kCouldNotComputeCR; } CRNode *crNode1 = GetOrCreateCRNode(*opnd1); @@ -1166,7 +1178,8 @@ TripCountType LoopScalarAnalysisResult::ComputeTripCount(MeFunction &func, uint3 if (cr->GetOpndsSize() == 2) { // cr has two opnds like {1, + 1} tripCountResult = ComputeTripCountWithSimpleConstCR(*opMeExpr, constNode->GetConstValue(), static_cast(cr->GetOpnd(0))->GetConstValue(), - static_cast(cr->GetOpnd(1))->GetConstValue()); + static_cast(cr->GetOpnd(1))->GetConstValue(), + (brTarget != loop->exitBB)); return kConstCR; } else { CHECK_FATAL(false, "NYI"); diff --git a/src/mapleall/maple_phase/include/phases.def b/src/mapleall/maple_phase/include/phases.def index 4bfd58725a..9e14628b67 100644 --- a/src/mapleall/maple_phase/include/phases.def +++ b/src/mapleall/maple_phase/include/phases.def @@ -49,6 +49,7 @@ ADDMAPLEMEPHASE("dse", MeOption::optLevel >= 2) ADDMAPLEMEPHASE("analyzector", JAVALANG) ADDMAPLEMEPHASE("abcopt", JAVALANG && MeOption::optLevel >= 2) ADDMAPLEMEPHASE("ssadevirt", JAVALANG && MeOption::optLevel >= 2) +ADDMAPLEMEPHASE("loopunrolling", MeOption::optLevel >= 2) ADDMAPLEMEPHASE("hprop", MeOption::optLevel >= 2) ADDMAPLEMEPHASE("valueRangePropagation", CLANG && MeOption::optLevel >= 2) ADDMAPLEMEPHASE("safetyWarning", CLANG && MeOption::optLevel >= 2) -- Gitee From 6ac1e176f32472c450d61a87b64fb0e60d4b53cd Mon Sep 17 00:00:00 2001 From: William Chen Date: Thu, 30 Sep 2021 21:00:56 -0700 Subject: [PATCH 2/4] Fix globalopt lsl->sxt->add lsl bug. Should not combine lsl. lsl 2->sxt->add lsl 2 cannot be combined to be add sxt 4. sxt is after lsl 2 then used by add's lsl. After combine all 3 the sxt is done before both lsl. Fix is to leave the lsl, but combine sxt and add. --- src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp index 262c3f9be5..0d0cb27ee5 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp @@ -1467,7 +1467,11 @@ void ExtendShiftOptPattern::ReplaceUseInsn(Insn &use, Insn &def, uint32 amount) cgFunc.GetRD()->InitGenUse(*defInsn->GetBB(), false); cgFunc.GetRD()->UpdateInOut(*use.GetBB(), true); newInsn = replaceUseInsn; - optSuccess = true; + if (isExten) { + optSuccess = false; + } else { + optSuccess = true; + } } /* -- Gitee From cdbf2a3806f0925da21787b4bd39b4f69e9d380a Mon Sep 17 00:00:00 2001 From: William Chen Date: Mon, 4 Oct 2021 06:16:32 -0700 Subject: [PATCH 3/4] Do not use move op to truncate u64 to u32. --- src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 834575ca2d..4fe8ab5fa1 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -4993,9 +4993,7 @@ void AArch64CGFunc::SelectCvtInt2Int(const BaseNode *parent, Operand *&resOpnd, opnd0 = &LoadIntoRegister(*opnd0, primType); if (fsize > tsize) { - if (tsize == k32BitSize) { - GetCurBB()->AppendInsn(GetCG()->BuildInstruction(MOP_wmovrr, *resOpnd, *opnd0)); - } else if (tsize == k8BitSize) { + if (tsize == k8BitSize) { MOperator mOp = IsSignedInteger(toType) ? MOP_xsxtb32 : MOP_xuxtb32; GetCurBB()->AppendInsn(GetCG()->BuildInstruction(mOp, *resOpnd, *opnd0)); } else if (tsize == k16BitSize) { -- Gitee From 9f9425e1c153e9a3c6d32099315f7e0b1610df27 Mon Sep 17 00:00:00 2001 From: linma Date: Sat, 9 Oct 2021 11:05:29 -0700 Subject: [PATCH 4/4] disable unroll when constNode is complex --- src/mapleall/maple_me/src/me_scalar_analysis.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mapleall/maple_me/src/me_scalar_analysis.cpp b/src/mapleall/maple_me/src/me_scalar_analysis.cpp index eab26d98b8..daf4e0bef3 100644 --- a/src/mapleall/maple_me/src/me_scalar_analysis.cpp +++ b/src/mapleall/maple_me/src/me_scalar_analysis.cpp @@ -1182,9 +1182,12 @@ TripCountType LoopScalarAnalysisResult::ComputeTripCount(MeFunction &func, uint3 (brTarget != loop->exitBB)); return kConstCR; } else { + return kCouldNotComputeCR; // complex cr node , can not compute tripcount +#if 0 CHECK_FATAL(false, "NYI"); tripCountResult = ComputeTripCountWithCR(*cr, *opMeExpr, constNode->GetConstValue()); return kConstCR; +#endif } return kConstCR; } -- Gitee