diff --git a/src/mapleall/maple_me/include/me_cfg.h b/src/mapleall/maple_me/include/me_cfg.h index 8465e37e7593e7444d293e906a2c00a923c244e9..eee61dc7ebbf90fff25d08cc8164cc8ac67d12b4 100644 --- a/src/mapleall/maple_me/include/me_cfg.h +++ b/src/mapleall/maple_me/include/me_cfg.h @@ -45,7 +45,11 @@ class MeCFG : public AnalysisResult { endTryBB2TryBB(mecfgAlloc.Adapter()), sccTopologicalVec(mecfgAlloc.Adapter()), sccOfBB(mecfgAlloc.Adapter()), - backEdges(mecfgAlloc.Adapter()) {} + backEdges(mecfgAlloc.Adapter()) { + if (MeOption::dumpCfgOfPhases) { + dumpIRProfileFile = true; + } + } ~MeCFG() = default; diff --git a/src/mapleall/maple_me/include/me_option.h b/src/mapleall/maple_me/include/me_option.h index ae78247e38781d8e6dec55793bdcad5158ab5730..4e43e1e05ee3d0633387a685ff83c26582dc813d 100644 --- a/src/mapleall/maple_me/include/me_option.h +++ b/src/mapleall/maple_me/include/me_option.h @@ -184,6 +184,7 @@ class MeOption { static uint8 rematLevel; static bool layoutWithPredict; static bool unifyRets; + static bool dumpCfgOfPhases; // safety check option begin static SafetyCheckMode npeCheckMode; static bool isNpeCheckAll; diff --git a/src/mapleall/maple_me/include/me_options.h b/src/mapleall/maple_me/include/me_options.h index 4b009116a771e5a3a083503d149e77052c87773c..da127b6d3d45a753f4c16c9f54c752d5bdfdd902 100644 --- a/src/mapleall/maple_me/include/me_options.h +++ b/src/mapleall/maple_me/include/me_options.h @@ -134,6 +134,7 @@ extern maplecl::Option warning; extern maplecl::Option remat; extern maplecl::Option unifyrets; extern maplecl::Option lfo; +extern maplecl::Option dumpCfgOfPhases; } diff --git a/src/mapleall/maple_me/src/bb.cpp b/src/mapleall/maple_me/src/bb.cpp index 181e6ed83170d9c8128d68290e69f4fd8488c06c..b01f4f0738a422eb125ba328d38a74eb3375b2ab 100644 --- a/src/mapleall/maple_me/src/bb.cpp +++ b/src/mapleall/maple_me/src/bb.cpp @@ -485,7 +485,11 @@ void BB::UpdateEdgeFreqs(bool updateBBFreqOfSucc) { succFreqs += GetSuccFreq()[i]; } int diff = static_cast(abs(succFreqs - GetFrequency())); - if (len == 0 || diff <= 1) {return;} + if (len == 0 || + (len == 1 && diff == 0) || + (len > 1 && diff <= 1)) { + return; + } for (uint32 i = 0; i < len; ++i) { int64_t sfreq = GetSuccFreq()[static_cast(i)]; int64_t scalefreq = (succFreqs == 0 ? (frequency / len) : (sfreq * frequency / succFreqs)); diff --git a/src/mapleall/maple_me/src/irmap.cpp b/src/mapleall/maple_me/src/irmap.cpp index ef20a5385ad48a905551e9aaeb88d516884fc52b..4d93ad365b11b44c960bddc0b0eeede8d593ffa5 100644 --- a/src/mapleall/maple_me/src/irmap.cpp +++ b/src/mapleall/maple_me/src/irmap.cpp @@ -1942,7 +1942,7 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { return opnd0; } else if ((cmpop == OP_ne && opnd1const->IsOne()) || (cmpop == OP_eq && opnd1const->IsZero())) { if (IsCompareHasReverseOp(opnd0->GetOp())) { - OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_u1, opnd0->GetNumOpnds()); + OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_i32, opnd0->GetNumOpnds()); reverseMeExpr.SetOpnd(0, opnd0->GetOpnd(0)); reverseMeExpr.SetOpnd(1, opnd0->GetOpnd(1)); reverseMeExpr.SetOpndType(static_cast(opnd0)->GetOpndType()); @@ -1959,7 +1959,7 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { if (constVal->GetKind() == kConstInt && constVal->IsZero()) { auto *subOpnd0 = opnd0->GetOpnd(0); auto *subOpnd1 = opnd0->GetOpnd(1); - return CreateMeExprCompare(cmpop, PTY_u1, subOpnd0->GetPrimType(), *subOpnd0, *subOpnd1); + return CreateMeExprCompare(cmpop, PTY_i32, subOpnd0->GetPrimType(), *subOpnd0, *subOpnd1); } } break; @@ -1976,13 +1976,13 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { } // we prefer ne/eq in following optimizations if (opnd0->GetMeOp() == kMeOpConst && static_cast(opnd0)->GetConstVal()->IsZero()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_eq : OP_ne, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_eq : OP_ne, PTY_i32, cmpExpr->GetOpndType(), *opnd1, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; } if (opnd1->GetMeOp() == kMeOpConst && static_cast(opnd1)->GetConstVal()->IsOne()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_ne : OP_eq, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_ne : OP_eq, PTY_i32, cmpExpr->GetOpndType(), *opnd0, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; @@ -2002,13 +2002,13 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { } // we prefer ne/eq in following optimizations if (opnd1->GetMeOp() == kMeOpConst && static_cast(opnd1)->GetConstVal()->IsZero()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_ne : OP_eq, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_ne : OP_eq, PTY_i32, cmpExpr->GetOpndType(), *opnd0, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; } if (opnd0->GetMeOp() == kMeOpConst && static_cast(opnd0)->GetConstVal()->IsOne()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_eq : OP_ne, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_eq : OP_ne, PTY_i32, cmpExpr->GetOpndType(), *opnd1, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; @@ -2599,7 +2599,7 @@ MeExpr *IRMap::SimplifyOpMeExpr(OpMeExpr *opmeexpr) { case OP_lnot: { MeExpr *opnd0 = opmeexpr->GetOpnd(0); if (IsCompareHasReverseOp(opnd0->GetOp())) { - OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_u1, opnd0->GetNumOpnds()); + OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_i32, opnd0->GetNumOpnds()); reverseMeExpr.SetOpnd(0, opnd0->GetOpnd(0)); reverseMeExpr.SetOpnd(1, opnd0->GetOpnd(1)); reverseMeExpr.SetOpndType(opnd0->GetOpnd(0)->GetPrimType()); diff --git a/src/mapleall/maple_me/src/me_bb_layout.cpp b/src/mapleall/maple_me/src/me_bb_layout.cpp index 8c5612db86016f3290de438029c7e6974687076a..ceafc3e6b0db1879f2f39fa32a73603dbc1ba8bb 100644 --- a/src/mapleall/maple_me/src/me_bb_layout.cpp +++ b/src/mapleall/maple_me/src/me_bb_layout.cpp @@ -1377,16 +1377,17 @@ bool MEBBLayout::PhaseRun(maple::MeFunction &f) { bbLayout->RunLayout(); f.SetLaidOutBBs(bbLayout->GetBBs()); - if (DEBUGFUNC_NEWPM(f) || Options::profileUse) { - // verify CFG : check condBB's succs should be different - bbLayout->VerifyBB(); - bbLayout->DumpBBPhyOrder(); + if (Options::profileUse) { if (cfg->UpdateCFGFreq() && cfg->DumpIRProfileFile()) { cfg->DumpToFile("after-bblayout", false, true); - } else { - cfg->DumpToFile("afterBBLayout", false); } } + if (DEBUGFUNC_NEWPM(f)) { + // verify CFG : check condBB's succs should be different + bbLayout->VerifyBB(); + bbLayout->DumpBBPhyOrder(); + cfg->DumpToFile("afterBBLayout", false); + } return true; } } // namespace maple diff --git a/src/mapleall/maple_me/src/me_critical_edge.cpp b/src/mapleall/maple_me/src/me_critical_edge.cpp index 4dca8f82a2c5424782fe08116a83f7d2471c08da..fd97bb2452e55a003cecf55c0b7ee4b1c39d2599 100644 --- a/src/mapleall/maple_me/src/me_critical_edge.cpp +++ b/src/mapleall/maple_me/src/me_critical_edge.cpp @@ -313,6 +313,9 @@ bool MESplitCEdge::PhaseRun(maple::MeFunction &f) { f.GetPreMeFunc()->pmeCreatedWhileLabelSet.clear(); } } + if (f.GetCfg()->UpdateCFGFreq() && (f.GetCfg()->DumpIRProfileFile())) { + f.GetCfg()->DumpToFile("after-splitcriticaledge", false, true); + } return false; } } // namespace maple diff --git a/src/mapleall/maple_me/src/me_irmap_build.cpp b/src/mapleall/maple_me/src/me_irmap_build.cpp index f34555f5c6156ad569897886097af21634264e0c..76449544f7488144e05683fdd71835e6b1aa390f 100644 --- a/src/mapleall/maple_me/src/me_irmap_build.cpp +++ b/src/mapleall/maple_me/src/me_irmap_build.cpp @@ -58,6 +58,9 @@ bool MEIRMapBuild::PhaseRun(maple::MeFunction &f) { } if (DEBUGFUNC_NEWPM(f)) { irMap->Dump(); + if (f.GetCfg()->UpdateCFGFreq()) { + f.GetCfg()->DumpToFile("irmapbuild", true, true); + } } // delete mempool for meirmap temporaries diff --git a/src/mapleall/maple_me/src/me_jump_threading.cpp b/src/mapleall/maple_me/src/me_jump_threading.cpp index 9d8580ae67a528153b82b16d588165e564fbc26b..9f6cee909658096b4ba71b66a30bad7b450e07c4 100644 --- a/src/mapleall/maple_me/src/me_jump_threading.cpp +++ b/src/mapleall/maple_me/src/me_jump_threading.cpp @@ -163,6 +163,11 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorAddSucc(*newSuccBB); + if (func.GetCfg()->UpdateCFGFreq()) { + uint64 freqUsed = old2NewBB[idxOfCurrBB].second->GetFrequency(); + old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(freqUsed); + newSuccBB->SetFrequency(static_cast(freqUsed)); + } break; } for (auto *temp : currentBB->GetSucc()) { @@ -182,8 +187,16 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorReplaceSucc(temp, newTemp, true); + if (func.GetCfg()->UpdateCFGFreq()) { + int idxInSucc = currentBB->GetSuccIndex(*newTemp); + newTemp->SetFrequency(static_cast(currentBB->GetSuccFreq()[idxInSucc])); + } } else { old2NewBB[idxOfCurrBB].second->AddSucc(*newTemp); + if (func.GetCfg()->UpdateCFGFreq()) { + old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(0); + newTemp->SetFrequency(0); + } } } else if (currentBB != old2NewBB[idxOfCurrBB].second) { // Deal with the case like the succ of currentBB is not in path. @@ -192,6 +205,9 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorAddSucc(*newTemp); + if (func.GetCfg()->UpdateCFGFreq()) { + old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(newTemp->GetFrequency()); + } } // Set label for new bb. SetNewOffsetOfLastMeStmtForNewBB(*currentBB, *old2NewBB[idxOfCurrBB].second, *temp, *newTemp); @@ -770,4 +786,4 @@ bool MEJumpThreading::PhaseRun(maple::MeFunction &f) { } return false; } -} // namespace maple \ No newline at end of file +} // namespace maple diff --git a/src/mapleall/maple_me/src/me_option.cpp b/src/mapleall/maple_me/src/me_option.cpp index f07a369ab08b98ec9225995200ce7ec15742cbc7..d062458a0d0cadb7fd11280ee95254e4bd9679f4 100644 --- a/src/mapleall/maple_me/src/me_option.cpp +++ b/src/mapleall/maple_me/src/me_option.cpp @@ -131,6 +131,7 @@ bool MeOption::isNpeCheckAll = false; SafetyCheckMode MeOption::boundaryCheckMode = SafetyCheckMode::kNoCheck; bool MeOption::safeRegionMode = false; bool MeOption::unifyRets = false; +bool MeOption::dumpCfgOfPhases = false; #if MIR_JAVA std::string MeOption::acquireFuncName = "Landroid/location/LocationManager;|requestLocationUpdates|"; std::string MeOption::releaseFuncName = "Landroid/location/LocationManager;|removeUpdates|"; @@ -313,6 +314,7 @@ bool MeOption::SolveOptions(bool isDebug) { maplecl::CopyIfEnabled(dseKeepRef, opts::me::dsekeepref); maplecl::CopyIfEnabled(lessThrowAlias, opts::me::lessthrowalias); maplecl::CopyIfEnabled(propBase, opts::me::propbase); + maplecl::CopyIfEnabled(dumpCfgOfPhases, opts::me::dumpCfgOfPhases); if (opts::me::propiloadref.IsEnabledByUser()) { propIloadRef = opts::me::propiloadref; diff --git a/src/mapleall/maple_me/src/me_options.cpp b/src/mapleall/maple_me/src/me_options.cpp index 66103cf29fe64caec4ab43b3d1fd9c5eee006e82..79adab5f52fb8c5344b73ff209a164139213328f 100644 --- a/src/mapleall/maple_me/src/me_options.cpp +++ b/src/mapleall/maple_me/src/me_options.cpp @@ -695,4 +695,8 @@ maplecl::Option lfo({"--lfo"}, {meCategory}, maplecl::DisableWith("--no-lfo")); +maplecl::Option dumpCfgOfPhases({"--dumpcfgofphases"}, + " --dumpcfgofphases \tDump CFG from various phases to .dot files\n", + {meCategory}); + } diff --git a/src/mapleall/maple_me/src/optimizeCFG.cpp b/src/mapleall/maple_me/src/optimizeCFG.cpp index 776d206d46097b10f96253af1e3f07e1c851a41f..4dbe27cf57840a5bcfa49394c9ff9017f6d19fcd 100644 --- a/src/mapleall/maple_me/src/optimizeCFG.cpp +++ b/src/mapleall/maple_me/src/optimizeCFG.cpp @@ -2670,8 +2670,11 @@ bool MEOptimizeCFG::PhaseRun(maple::MeFunction &f) { MeSSAUpdate ssaUpdate(f, *f.GetMeSSATab(), *dom, cands); ssaUpdate.Run(); } - if (f.GetCfg()->DumpIRProfileFile()) { - f.GetCfg()->DumpToFile("after-OptimizeCFG", false, f.GetCfg()->UpdateCFGFreq()); + if (f.GetCfg()->UpdateCFGFreq()) { + f.GetCfg()->UpdateEdgeFreqWithBBFreq(); + if (f.GetCfg()->DumpIRProfileFile()) { + f.GetCfg()->DumpToFile("after-OptimizeCFG", false, f.GetCfg()->UpdateCFGFreq()); + } } } return change; diff --git a/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h b/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h index 1a83fa5c8321d01cdc6a6c5fb51e3c257f9ed43e..386895da8a566288f2678fd7b97d737fcb0bc63c 100644 --- a/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h +++ b/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h @@ -81,7 +81,7 @@ class ProfDataBinaryImportBase { class ProfileSummaryImport : public ProfDataBinaryImportBase { public: ProfileSummaryImport(std::string &outputFile, std::ifstream &input) : ProfDataBinaryImportBase(outputFile, input) {} - int ReadSummary(MplProfileData*); + void ReadSummary(MplProfileData*); private: void ReadMProfMagic(); diff --git a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp index eeaa70cd47058a210ea13f8161b04532158792b0..d8b5deefa1261f49b93548297eb8a4316bdeb4ea 100644 --- a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp +++ b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp @@ -40,12 +40,12 @@ T ProfDataBinaryImportBase::ReadNum() { return static_cast(val); } -int ProfileSummaryImport::ReadSummary(MplProfileData *profData) { +void ProfileSummaryImport::ReadSummary(MplProfileData *profData) { CHECK_FATAL(profData != nullptr, "sanity check"); uint64_t magicNum = ReadNum(); if (magicNum != kMapleProfDataMagicNumber) { LogInfo::MapleLogger() << "magic number error, quit\n"; - return 1; + abort(); } uint64_t checksum = ReadNum(); uint32_t runtimes = ReadNum(); @@ -62,8 +62,6 @@ int ProfileSummaryImport::ReadSummary(MplProfileData *profData) { uint64_t r4 = ReadNum(); profData->summary.AddHistogramRecord(r1, r2, r3, r4); } - - return 0; } int FunctionProfileImport::ReadFuncProfile(MplProfileData *profData) { @@ -97,19 +95,19 @@ int MplProfDataParser::ReadMapleProfileData() { static_cast(mprofDataFile.append("/")); } if (dumpDetail) { - LogInfo::MapleLogger() << "set env gcov_prefix= " << mprofDataFile << std::endl; + LogInfo::MapleLogger() << "set env GCOV_PREFIX= " << mprofDataFile << std::endl; } uint32_t stripnum = 0; if (const char *envGcovprefixstrip = std::getenv("GCOV_PREFIX_STRIP")) { std::string strip(envGcovprefixstrip); stripnum = static_cast(std::stoi(strip)); if (dumpDetail) { - LogInfo::MapleLogger() << "set env gcov_prefix_strip=" << strip << std::endl; + LogInfo::MapleLogger() << "set env GCOV_PREFIX_STRIP=" << strip << std::endl; } } std::string profDataFileName = m.GetProfileDataFileName(); if (dumpDetail) { - LogInfo::MapleLogger() << "module profdata Name: " << profDataFileName << std::endl; + LogInfo::MapleLogger() << "profdata file stem before strip: " << profDataFileName << std::endl; } // reduce path in profDataFileName while (stripnum > 0 && profDataFileName.size() > 1) { @@ -121,7 +119,7 @@ int MplProfDataParser::ReadMapleProfileData() { stripnum--; } if (dumpDetail) { - LogInfo::MapleLogger() << "after strip, module profdata Name: " << profDataFileName << std::endl; + LogInfo::MapleLogger() << "profdata file stem after strip: " << profDataFileName << std::endl; } CHECK_FATAL(profDataFileName.size() > 0, "sanity check"); static_cast(mprofDataFile.append(profDataFileName)); @@ -129,23 +127,21 @@ int MplProfDataParser::ReadMapleProfileData() { // if gcov_prefix is not set, find .mprofdata according to m.profiledata mprofDataFile = m.GetProfileDataFileName(); if (dumpDetail) { - LogInfo::MapleLogger() << "NO ENV, module profdata Name: " << mprofDataFile << std::endl; + LogInfo::MapleLogger() << "NO ENV, profdata file stem: " << mprofDataFile << std::endl; } } // add .mprofdata static_cast(mprofDataFile.append(namemangler::kMplProfFileNameExt)); } ASSERT(!mprofDataFile.empty(), "null check"); - if (dumpDetail) { - LogInfo::MapleLogger() << "will open mprofileData " << mprofDataFile << std::endl; - } + LogInfo::MapleLogger() << "profileUse will open " << mprofDataFile << std::endl; // create mpl profdata profData = mempool->New(mempool, &alloc); // read .mprofdata std::ifstream inputStream(mprofDataFile, (std::ios::in | std::ios::binary)); if (!inputStream) { LogInfo::MapleLogger() << "Could not open the file " << mprofDataFile << "\n"; - return 1; + abort(); } // get length of file static_cast(inputStream.seekg(0, std::ios::end)); @@ -160,18 +156,14 @@ int MplProfDataParser::ReadMapleProfileData() { // read 1st part summary ProfileSummaryImport summaryImport(mprofDataFile, inputStream); summaryImport.SetPosition(static_cast(static_cast(buffer.get()))); - int res = summaryImport.ReadSummary(profData); - if (res) { - LogInfo::MapleLogger() << "no summary part\n"; - return 1; - } + summaryImport.ReadSummary(profData); if (dumpDetail) { profData->summary.DumpSummary(); } // read 2nd part function profile data FunctionProfileImport funcImport(mprofDataFile, inputStream); funcImport.SetPosition(summaryImport.GetPosition()); - res = funcImport.ReadFuncProfile(profData); + int res = funcImport.ReadFuncProfile(profData); if (res) { LogInfo::MapleLogger() << "no function profile part\n"; return 1; @@ -188,7 +180,7 @@ void MMplProfDataParser::GetAnalysisDependence(AnalysisDep &aDep) const { bool MMplProfDataParser::PhaseRun(maple::MIRModule &m) { MemPool *memPool = m.GetMemPool(); // use global pool to store profile data - bool enableDebug = true; // true to dump trace + bool enableDebug = false; // true to dump trace MplProfDataParser parser(m, memPool, enableDebug); int res = parser.ReadMapleProfileData(); if (res) {