diff --git a/src/mapleall/maple_ir/src/mir_lower.cpp b/src/mapleall/maple_ir/src/mir_lower.cpp index 389dc6fc87adbacd1a42a9d3e5263498649c2fbc..9c989137c0587d57e4a0dab579670bb9febecf66 100644 --- a/src/mapleall/maple_ir/src/mir_lower.cpp +++ b/src/mapleall/maple_ir/src/mir_lower.cpp @@ -307,6 +307,10 @@ BlockNode *MIRLower::LowerSwitchStmt(SwitchNode *switchNode) { blk->AddStatement(condGoto); GotoNode *gotoStmt = builder->CreateStmtGoto(OP_goto, defaultLabel); blk->AddStatement(gotoStmt); + if (Options::profileUse && mirModule.CurFunction()->GetFuncProfData()) { + mirModule.CurFunction()->GetFuncProfData()->SetStmtFreq(gotoStmt->GetStmtID(), + mirModule.CurFunction()->GetFuncProfData()->GetStmtFreq(switchNode->GetStmtID())); + } } else { // brtrue (x < minCaseVal) @default_label // brtrue (x > maxCaseVal) @default_label diff --git a/src/mapleall/maple_me/src/me_emit.cpp b/src/mapleall/maple_me/src/me_emit.cpp index eaea1c8ede9397eef91e4b36343875634e051e78..67afe269e1e0fe698be92edcef5524dffc5e9a63 100644 --- a/src/mapleall/maple_me/src/me_emit.cpp +++ b/src/mapleall/maple_me/src/me_emit.cpp @@ -170,11 +170,62 @@ bool ProfileGenEmit::PhaseRun(maple::MeFunction &f) { sym->SetIsDeleted(); } } - for (BB *bb : f.GetCfg()->GetAllBBs()) { - if (bb == nullptr) { - continue; + if (Options::profileGen ) { + std::unordered_map visitedBBs; + std::stack listBBs; + std::vector predDep(f.GetCfg()->GetAllBBs().size(), nullptr); + std::vector succDep(f.GetCfg()->GetAllBBs().size(), nullptr); + // Set up pred-succ single fallthrough chain dependency + for (BB *bb : f.GetCfg()->GetAllBBs()) { + if (bb != nullptr && bb->GetSucc().size() != 0 && + ((bb->GetKind() == kBBCondGoto) || (bb->GetKind() == kBBFallthru) || bb->GetKind() == kBBUnknown)) { + predDep[bb->GetSucc()[0]->GetBBId()] = bb; + succDep[bb->GetBBId()] = bb->GetSucc()[0]; + } + } + // Emit all dependency chains of BBs firstd + for (BB *succ : succDep) { + if (!succ) { + continue; + } + BB *pred = predDep[succ->GetBBId()]; + if (pred && !visitedBBs[pred]) { + pred->EmitBB(*mirFunction->GetBody(), false); + visitedBBs[pred] = true; + while (succ && !visitedBBs[succ]) { + succ->EmitBB(*mirFunction->GetBody(), false); + visitedBBs[succ] = true; + succ = succDep[succ->GetBBId()]; + } + } + } + // Emit remaining BBs + for (BB *bb : f.GetCfg()->GetAllBBs()) { + if (bb == nullptr || visitedBBs[bb]) { + continue; + } else { + listBBs.push(bb); + while (!listBBs.empty()) { + bb = listBBs.top(); + listBBs.pop(); + bb->EmitBB(*mirFunction->GetBody(), false); + visitedBBs[bb] = true; + // Make sure default target BB to emit first + for (int32 idx = bb->GetSucc().size() - 1; idx >= 0; idx--) { + if (!visitedBBs[bb->GetSucc()[idx]]) { + listBBs.push(bb->GetSucc()[idx]); + } + } + } + } + } + } else { + for (BB *bb : f.GetCfg()->GetAllBBs()) { + if (bb == nullptr) { + continue; + } + bb->EmitBB(*mirFunction->GetBody(), false); } - bb->EmitBB(*mirFunction->GetBody(), false); } ResetDependentedSymbolLive(mirFunction); }