diff --git a/src/mapleall/maple_be/include/be/lower.h b/src/mapleall/maple_be/include/be/lower.h index 1d6cf68d6d196586fb3515831d437a63f85936db..ae5fc5873ec2ad112f8a0270fa345a7048fdfa06 100644 --- a/src/mapleall/maple_be/include/be/lower.h +++ b/src/mapleall/maple_be/include/be/lower.h @@ -162,7 +162,7 @@ class CGLowerer { BaseNode *SplitBinaryNodeOpnd1(BinaryNode &bNode, BlockNode &blkNode); BaseNode *SplitTernaryNodeResult(TernaryNode &tNode, BaseNode &parent, BlockNode &blkNode); bool IsComplexSelect(const TernaryNode &tNode) const; - int32 FindTheCurrentStmtFreq(const StmtNode *stmt) const; + FreqType FindTheCurrentStmtFreq(const StmtNode *stmt) const; BaseNode *LowerComplexSelect(const TernaryNode &tNode, BaseNode &parent, BlockNode &blkNode); BaseNode *LowerFarray(ArrayNode &array); BaseNode *LowerArrayDim(ArrayNode &array, int32 dim); @@ -268,7 +268,7 @@ class CGLowerer { void BuildLabel2FreqMap(); - std::unordered_map &GetLabel2Freq() { + std::unordered_map &GetLabel2Freq() { return l2fMap; } @@ -341,7 +341,8 @@ class CGLowerer { uint32 labelIdx = 0; static std::unordered_map intrinFuncIDs; static std::unordered_map arrayClassCacheIndex; - std::unordered_map l2fMap; // Map label to frequency on profileUse + std::unordered_map l2fMap; // Map label to frequency on profileUse + FuncProfInfo *funcProfData; }; } /* namespace maplebe */ diff --git a/src/mapleall/maple_be/include/be/switch_lowerer.h b/src/mapleall/maple_be/include/be/switch_lowerer.h index 18450a1b39e0dd1d797054c80a519f8255932cd8..7d583517eecac2d5005a4cb72f7eae07d023c0a8 100644 --- a/src/mapleall/maple_be/include/be/switch_lowerer.h +++ b/src/mapleall/maple_be/include/be/switch_lowerer.h @@ -72,7 +72,8 @@ class SwitchLowerer { maple::GotoNode *BuildGotoNode(int32 idx); maple::CondGotoNode *BuildCondGotoNode(int32 idx, Opcode opCode, BaseNode &cond); maple::BlockNode *BuildCodeForSwitchItems(int32 start, int32 end, bool lowBlockNodeChecked, - bool highBlockNodeChecked, LabelIdx newLabelIdx = 0); + bool highBlockNodeChecked, FreqType freqSum, LabelIdx newLabelIdx = 0); + FreqType sumFreq(uint32 startIdx, uint32 endIdx); }; } /* namespace maplebe */ diff --git a/src/mapleall/maple_be/include/cg/cg_profile_use.h b/src/mapleall/maple_be/include/cg/cg_profile_use.h index 4a24d649319dae717024d1d96082d742d21f332b..40eeddb32d47b20f55832f4a961ee12bcc28c7fc 100644 --- a/src/mapleall/maple_be/include/cg/cg_profile_use.h +++ b/src/mapleall/maple_be/include/cg/cg_profile_use.h @@ -20,20 +20,20 @@ namespace maplebe { class CgProfUse { - public: + public: struct Edge { - BB *src; - BB *dst; - Edge *next = nullptr; // the edge with the same src - uint64 frequency = 0; // later int64_t - bool status = false; // True value indicates the edge's freq is determined - Edge(BB *bb1, BB *bb2) : src(bb1), dst(bb2) {} + BB *src; + BB *dst; + Edge *next = nullptr; // the edge with the same src + FreqType frequency = -1; + bool status = false; // True value indicates the edge's freq is determined + Edge(BB *bb1, BB *bb2) : src(bb1), dst(bb2) {} }; - CgProfUse(CGFunc &f, MemPool &mp) : - cgFunc(&f), memPool(&mp), alloc(&mp), allEdges(alloc.Adapter()), - BB2InEdges(alloc.Adapter()), BB2OutEdges(alloc.Adapter()) {} + CgProfUse(CGFunc &f, MemPool &mp) + : cgFunc(&f), memPool(&mp), alloc(&mp), allEdges(alloc.Adapter()), + BB2InEdges(alloc.Adapter()), BB2OutEdges(alloc.Adapter()) {} virtual ~CgProfUse() = default; diff --git a/src/mapleall/maple_be/include/cg/cgbb.h b/src/mapleall/maple_be/include/cg/cgbb.h index 38476f804e045ce96a2574dadd8eeaa4332233b0..ad99b0a291779cec88120697b555018fdcf9ad18 100644 --- a/src/mapleall/maple_be/include/cg/cgbb.h +++ b/src/mapleall/maple_be/include/cg/cgbb.h @@ -282,10 +282,10 @@ class BB { void SetFrequency(uint32 arg) { frequency = arg; } - uint64 GetProfFreq() const { + FreqType GetProfFreq() const { return profFreq; } - void SetProfFreq(uint64 arg) { + void SetProfFreq(FreqType arg) { profFreq = arg; } BB *GetNext() { @@ -815,7 +815,7 @@ class BB { succsProfFreq.resize(succs.size(), 0); } - uint64 GetEdgeProfFreq(const BB &bb) const { + FreqType GetEdgeProfFreq(const BB &bb) const { auto iter = std::find(succs.begin(), succs.end(), &bb); if (iter == std::end(succs) || succs.size() > succsProfFreq.size()) { return 0; @@ -826,7 +826,7 @@ class BB { return succsProfFreq[idx]; } - uint64 GetEdgeProfFreq(size_t idx) const { + FreqType GetEdgeProfFreq(size_t idx) const { if (idx >= succsProfFreq.size()) { return 0; } @@ -835,7 +835,7 @@ class BB { return succsProfFreq[idx]; } - void SetEdgeProfFreq(BB *bb, uint64 freq) { + void SetEdgeProfFreq(BB *bb, FreqType freq) { auto iter = std::find(succs.begin(), succs.end(), bb); CHECK_FATAL(iter != std::end(succs), "%d is not the successor of %d", bb->GetId(), this->GetId()); CHECK_FATAL(succs.size() == succsProfFreq.size(), @@ -849,7 +849,7 @@ class BB { uint32 id; uint32 level = 0; uint32 frequency = 0; - uint64 profFreq = 0; // profileUse + FreqType profFreq = 0; // profileUse BB *prev = nullptr; /* Doubly linked list of BBs; */ BB *next = nullptr; /* They represent the order in which blocks are to be emitted. */ @@ -867,7 +867,7 @@ class BB { MapleList loopPreds; MapleList loopSuccs; MapleVector succsFreq; - MapleVector succsProfFreq; + MapleVector succsProfFreq; /* this is for live in out analysis */ MapleSet liveInRegNO; MapleSet liveOutRegNO; diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index 4077f2393801bb2c6d207ef8df411acdffec5487..9690854a671e363ebd970a2d59c0f77bd20b3cca 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -196,7 +196,7 @@ class CGFunc { Insn &BuildScopeInsn(int64 id, bool isEnd); void GenerateLoc(StmtNode *stmt, SrcPosition &lastSrcPos, SrcPosition &lastMplPos); void GenerateScopeLabel(StmtNode *stmt, SrcPosition &lastSrcPos, bool &posDone); - int32 GetFreqFromStmt(uint32 stmtId); + FreqType GetFreqFromStmt(uint32 stmtId); void GenerateInstruction(); bool MemBarOpt(const StmtNode &membar); void UpdateCallBBFrequency(); diff --git a/src/mapleall/maple_be/include/cg/reg_alloc_color_ra.h b/src/mapleall/maple_be/include/cg/reg_alloc_color_ra.h index 7f7830e95d34487a2ff4272130c23d44bb49f666..fbe85344ca84b1e3bd0c79709382a36ac48a4fe7 100644 --- a/src/mapleall/maple_be/include/cg/reg_alloc_color_ra.h +++ b/src/mapleall/maple_be/include/cg/reg_alloc_color_ra.h @@ -568,12 +568,12 @@ class LiveRange { numUses = val; } - uint32 GetFrequency() const { + FreqType GetFrequency() const { return frequency; } - void SetFrequency(uint32 frequencyVal) { - this->frequency = frequencyVal; + void SetFrequency(FreqType freq) { + frequency = freq; } #endif /* OPTIMIZE_FOR_PROLOG */ @@ -716,7 +716,7 @@ class LiveRange { #ifdef OPTIMIZE_FOR_PROLOG uint32 numDefs = 0; uint32 numUses = 0; - uint32 frequency = 0; + FreqType frequency = 0; #endif /* OPTIMIZE_FOR_PROLOG */ MemOperand *spillMem = nullptr; /* memory operand used for spill, if any */ regno_t spillReg = 0; /* register operand for spill at current point */ diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 8713b9ec1c07c0e903c8eb4e8a153a4e4c6f7877..47f8b340028adaf1eab7c1a0e697487888f75cff 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -278,7 +278,9 @@ BaseNode *CGLowerer::SplitBinaryNodeOpnd1(BinaryNode &bNode, BlockNode &blkNode) BaseNode *dreadNode = mirbuilder->CreateExprDread(*dnodeSt); bNode.SetOpnd(dreadNode, 1); - + if (funcProfData) { + funcProfData->CopyStmtFreq(dnode->GetStmtID(), blkNode.GetStmtID()); + } return &bNode; } @@ -303,7 +305,9 @@ BaseNode *CGLowerer::SplitTernaryNodeResult(TernaryNode &tNode, BaseNode &parent break; } } - + if (funcProfData) { + funcProfData->CopyStmtFreq(dassignNode->GetStmtID(), blkNode.GetStmtID()); + } return dreadNode; } @@ -327,9 +331,9 @@ bool CGLowerer::IsComplexSelect(const TernaryNode &tNode) const { return false; } -int32 CGLowerer::FindTheCurrentStmtFreq(const StmtNode *stmt) const { +FreqType CGLowerer::FindTheCurrentStmtFreq(const StmtNode *stmt) const { while (stmt != nullptr) { - int32 freq = mirModule.CurFunction()->GetFreqFromLastStmt(stmt->GetStmtID()); + FreqType freq = mirModule.CurFunction()->GetFreqFromLastStmt(stmt->GetStmtID()); if (freq != -1) { return freq; } @@ -378,18 +382,21 @@ BaseNode *CGLowerer::LowerComplexSelect(const TernaryNode &tNode, BaseNode &pare mirModule.CurFunction()->GetLabelTab()->AddToStringLabelMap(targetIdx); brTargetStmt->SetOffset(targetIdx); // Update the current stmt frequence - int32 currentStmtFreq = 0; + FreqType currentStmtFreq = 0; if (kOpcodeInfo.IsStmt(parent.GetOpCode())) { currentStmtFreq = FindTheCurrentStmtFreq(static_cast(&parent)); } currentStmtFreq = currentStmtFreq == -1 ? 0 : currentStmtFreq; - func->SetLastFreqMap(brTargetStmt->GetStmtID(), static_cast(currentStmtFreq)); + func->SetLastFreqMap(brTargetStmt->GetStmtID(), currentStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), brTargetStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(brTargetStmt->GetStmtID(), static_cast(&parent)->GetStmtID()); + } union { MIRSymbol *resSym; PregIdx resPreg; } cplxSelRes; // complex select result - uint32 fallthruStmtFreq = static_cast((currentStmtFreq + 1) / 2); + FreqType fallthruStmtFreq = (currentStmtFreq + 1) / 2; if (tNode.GetPrimType() == PTY_agg) { static uint32 val = 0; std::string name("ComplexSelectTmp"); @@ -399,6 +406,9 @@ BaseNode *CGLowerer::LowerComplexSelect(const TernaryNode &tNode, BaseNode &pare // Fallthru: update the frequence 1 func->SetFirstFreqMap(dassignTrue->GetStmtID(), fallthruStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), dassignTrue); + if (funcProfData) { + funcProfData->CopyStmtFreq(dassignTrue->GetStmtID(), static_cast(&parent)->GetStmtID()); + } } else { cplxSelRes.resPreg = mirbuilder->GetCurrentFunction()->GetPregTab()->CreatePreg(tNode.GetPrimType()); RegassignNode *regassignTrue = @@ -406,6 +416,9 @@ BaseNode *CGLowerer::LowerComplexSelect(const TernaryNode &tNode, BaseNode &pare // Update the frequence first opnd func->SetFirstFreqMap(regassignTrue->GetStmtID(), fallthruStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), regassignTrue); + if (funcProfData) { + funcProfData->CopyStmtFreq(regassignTrue->GetStmtID(), static_cast(&parent)->GetStmtID()); + } } GotoNode *gotoStmt = mirModule.CurFuncCodeMemPool()->New(OP_goto); @@ -421,25 +434,37 @@ BaseNode *CGLowerer::LowerComplexSelect(const TernaryNode &tNode, BaseNode &pare lableStmt->SetLabelIdx(targetIdx); func->SetFirstFreqMap(lableStmt->GetStmtID(), targetStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), lableStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(lableStmt->GetStmtID(), static_cast(&parent)->GetStmtID()); + } if (tNode.GetPrimType() == PTY_agg) { DassignNode *dassignFalse = mirbuilder->CreateStmtDassign(*cplxSelRes.resSym, 0, tNode.Opnd(2)); // Update the frequence second opnd func->SetLastFreqMap(dassignFalse->GetStmtID(), targetStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), dassignFalse); + if (funcProfData) { + funcProfData->CopyStmtFreq(dassignFalse->GetStmtID(), static_cast(&parent)->GetStmtID()); + } } else { RegassignNode *regassignFalse = mirbuilder->CreateStmtRegassign(tNode.GetPrimType(), cplxSelRes.resPreg, tNode.Opnd(2)); // Update the frequence 2 func->SetLastFreqMap(regassignFalse->GetStmtID(), targetStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), regassignFalse); + if (funcProfData) { + funcProfData->CopyStmtFreq(regassignFalse->GetStmtID(), static_cast(&parent)->GetStmtID()); + } } lableStmt = mirModule.CurFuncCodeMemPool()->New(); lableStmt->SetLabelIdx(endIdx); // Update the frequence third opnd - func->SetFirstFreqMap(lableStmt->GetStmtID(), static_cast(currentStmtFreq)); + func->SetFirstFreqMap(lableStmt->GetStmtID(), currentStmtFreq); blkNode.InsertAfter(blkNode.GetLast(), lableStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(lableStmt->GetStmtID(), static_cast(&parent)->GetStmtID()); + } BaseNode *exprNode = (tNode.GetPrimType() == PTY_agg) ? static_cast(mirbuilder->CreateExprDread(*cplxSelRes.resSym)) : @@ -813,7 +838,11 @@ StmtNode *CGLowerer::WriteBitField(const std::pair &byteBitOffsets } auto depositBits = builder->CreateExprDepositbits(OP_depositbits, primType, static_cast(bitOffset), bitSize, bitField, rhs); - return builder->CreateStmtIassignoff(primType, byteOffset, baseAddr, depositBits); + StmtNode *iAssignoff = builder->CreateStmtIassignoff(primType, byteOffset, baseAddr, depositBits); + if (funcProfData) { + funcProfData->CopyStmtFreq(iAssignoff->GetStmtID(), block->GetStmtID()); + } + return iAssignoff; } // if space not enough in the unit with size of primType, we would make an extra assignment from next bound auto bitsRemained = (static_cast(bitOffset) + bitSize) - primTypeBitSize; @@ -833,6 +862,10 @@ StmtNode *CGLowerer::WriteBitField(const std::pair &byteBitOffsets builder->CreateExprDepositbits(OP_depositbits, primType, 0, bitsRemained, bitFieldRemained, extractedHigherBits); auto *assignedHigherBits = builder->CreateStmtIassignoff(primType, byteOffset + static_cast(GetPrimTypeSize(primType)), baseAddr, depositedHigherBits); + if (funcProfData) { + funcProfData->CopyStmtFreq(assignedLowerBits->GetStmtID(), block->GetStmtID()); + funcProfData->CopyStmtFreq(assignedHigherBits->GetStmtID(), block->GetStmtID()); + } return assignedHigherBits; } @@ -946,6 +979,10 @@ BlockNode *CGLowerer::LowerReturnStructUsingFakeParm(NaryStmtNode &retNode) { MIRPtrType *retTy = static_cast(retSt->GetType()); IassignNode *iassign = mirModule.CurFuncCodeMemPool()->New(); iassign->SetTyIdx(retTy->GetTypeIndex()); + if (funcProfData) { + funcProfData->CopyStmtFreq(iassign->GetStmtID(), retNode.GetStmtID()); + funcProfData->CopyStmtFreq(blk->GetStmtID(), retNode.GetStmtID()); + } ASSERT(opnd0 != nullptr, "opnd0 should not be nullptr"); if ((beCommon.GetTypeSize(retTy->GetPointedTyIdx().GetIdx()) <= k16ByteSize) && (opnd0->GetPrimType() == PTY_agg)) { /* struct goes into register. */ @@ -1054,6 +1091,9 @@ void CGLowerer::LowerIassign(IassignNode &iassign, BlockNode &newBlk) { LowerStmt(iassign, newBlk); newStmt = &iassign; } + if (funcProfData) { + funcProfData->CopyStmtFreq(newStmt->GetStmtID(), newBlk.GetStmtID()); + } newBlk.AddStatement(newStmt); } @@ -1092,6 +1132,9 @@ void CGLowerer::LowerAsmStmt(AsmNode *asmNode, BlockNode *newBlk) { } newBlk->AddStatement(assignNode); asmNode->SetOpnd(readOpnd, i); + if (funcProfData) { + funcProfData->CopyStmtFreq(assignNode->GetStmtID(), newBlk->GetStmtID()); + } } newBlk->AddStatement(asmNode); } @@ -1179,6 +1222,9 @@ void CGLowerer::LowerCallStmt(StmtNode &stmt, StmtNode *&nextStmt, BlockNode &ne newStmt = LowerCall(static_cast(*newStmt), nextStmt, newBlk, retty, uselvar); } newStmt->SetSrcPos(stmt.GetSrcPos()); + if (funcProfData) { + funcProfData->CopyStmtFreq(newStmt->GetStmtID(), newBlk.GetStmtID()); + } newBlk.AddStatement(newStmt); if (CGOptions::GetInstance().GetOptimizeLevel() >= CGOptions::kLevel2 && stmt.GetOpCode() == OP_intrinsiccall) { /* Try to expand memset and memcpy call lowered from intrinsiccall */ @@ -1241,6 +1287,9 @@ StmtNode *CGLowerer::GenIntrinsiccallNode(const StmtNode &stmt, PUIdx &funcCalle } newCall->SetSrcPos(stmt.GetSrcPos()); funcCalled = bFunc; + if (funcProfData) { + funcProfData->CopyStmtFreq(newCall->GetStmtID(), stmt.GetStmtID()); + } } return newCall; } @@ -1255,6 +1304,9 @@ StmtNode *CGLowerer::GenIcallNode(PUIdx &funcCalled, IcallNode &origCall) { newCall->SetSrcPos(origCall.GetSrcPos()); CHECK_FATAL(newCall != nullptr, "nullptr is not expected"); funcCalled = kFuncNotFound; + if (funcProfData) { + funcProfData->CopyStmtFreq(newCall->GetStmtID(), origCall.GetStmtID()); + } return newCall; } @@ -1262,6 +1314,9 @@ BlockNode *CGLowerer::GenBlockNode(StmtNode &newCall, const CallReturnVector &p2 const PUIdx &funcCalled, bool handledAtLowerLevel, bool uselvar) { BlockNode *blk = mirModule.CurFuncCodeMemPool()->New(); blk->AddStatement(&newCall); + if (funcProfData) { + funcProfData->CopyStmtFreq(blk->GetStmtID(), newCall.GetStmtID()); + } if (!handledAtLowerLevel) { CHECK_FATAL(p2nRets.size() <= 1, "make sure p2nRets size <= 1"); /* Create DassignStmt to save kSregRetval0. */ @@ -1352,6 +1407,9 @@ BlockNode *CGLowerer::LowerMemop(StmtNode &stmt) { auto *next = stmt.GetNext(); auto *blk = mirModule.CurFuncCodeMemPool()->New(); blk->AddStatement(&stmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(blk->GetStmtID(), stmt.GetStmtID()); + } uint32 oldTypeTableSize = GlobalTables::GetTypeTable().GetTypeTableSize(); bool success = simplifyOp.AutoSimplify(stmt, *blk, true); uint32 newTypeTableSize = GlobalTables::GetTypeTable().GetTypeTableSize(); @@ -1388,6 +1446,10 @@ BlockNode *CGLowerer::LowerIntrinsiccallAassignedToAssignStmt(IntrinsiccallNode auto intrinsicOp = builder->CreateExprIntrinsicop(intrinsicID, OP_intrinsicop, primType, TyIdx(0), opndVector); auto regAssign = builder->CreateStmtRegassign(primType, regIdx, intrinsicOp); block->AddStatement(regAssign); + if (funcProfData) { + funcProfData->CopyStmtFreq(block->GetStmtID(), intrinsicCall.GetStmtID()); + funcProfData->CopyStmtFreq(regAssign->GetStmtID(), block->GetStmtID()); + } } else { auto fieldID = regFieldPair.GetFieldID(); auto stIdx = returnPair->first; @@ -1395,6 +1457,10 @@ BlockNode *CGLowerer::LowerIntrinsiccallAassignedToAssignStmt(IntrinsiccallNode auto intrinsicOp = builder->CreateExprIntrinsicop(intrinsicID, OP_intrinsicop, *type, opndVector); auto dAssign = builder->CreateStmtDassign(stIdx, fieldID, intrinsicOp); block->AddStatement(dAssign); + if (funcProfData) { + funcProfData->CopyStmtFreq(block->GetStmtID(), intrinsicCall.GetStmtID()); + funcProfData->CopyStmtFreq(dAssign->GetStmtID(), block->GetStmtID()); + } } return LowerBlock(*block); } @@ -1421,7 +1487,7 @@ BlockNode *CGLowerer::LowerCallAssignedStmt(StmtNode &stmt, bool uselvar) { static_cast(newCall)->SetReturnVec(*p2nRets); MIRFunction *curFunc = mirModule.CurFunction(); curFunc->SetLastFreqMap(newCall->GetStmtID(), - static_cast(curFunc->GetFreqFromLastStmt(stmt.GetStmtID()))); + curFunc->GetFreqFromLastStmt(stmt.GetStmtID())); break; } case OP_intrinsiccallassigned: @@ -1468,6 +1534,10 @@ BlockNode *CGLowerer::LowerCallAssignedStmt(StmtNode &stmt, bool uselvar) { /* transfer srcPosition location info */ newCall->SetSrcPos(stmt.GetSrcPos()); + if (funcProfData) { + funcProfData->CopyStmtFreq(newCall->GetStmtID(), stmt.GetStmtID()); + } + return GenBlockNode(*newCall, *p2nRets, stmt.GetOpCode(), funcCalled, handledAtLowerLevel, uselvar); } @@ -1528,6 +1598,10 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, DassignNode *dass = mirModule.GetMIRBuilder()->CreateStmtDassign( retPair.first, retPair.second.GetFieldID(), rhs); oldBlk->InsertBefore(nextStmt, dass); + if (funcProfData) { + funcProfData->CopyStmtFreq(dass->GetStmtID(), stmt->GetStmtID()); + } + nextStmt = dass; // update CallReturnVector to the new temporary (*p2nrets)[0].first = temp->GetStIdx(); @@ -1587,6 +1661,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, CallNode *callStmt = mirModule.GetMIRBuilder()->CreateStmtCall(callNode->GetPUIdx(), callNode->GetNopnd()); callStmt->SetSrcPos(callNode->GetSrcPos()); newBlk.AddStatement(callStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(callStmt->GetStmtID(), stmt->GetStmtID()); + } } else if (stmt->GetOpCode() == OP_icallassigned || stmt->GetOpCode() == OP_icallprotoassigned) { auto *icallNode = static_cast(stmt); for (size_t i = 0; i < icallNode->GetNopndSize(); ++i) { @@ -1601,6 +1678,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, } icallStmt->SetSrcPos(icallNode->GetSrcPos()); newBlk.AddStatement(icallStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(icallStmt->GetStmtID(), stmt->GetStmtID()); + } } else { return false; } @@ -1615,6 +1695,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, pIdx1R = GetCurrentFunc()->GetPregTab()->CreatePreg(PTY_u64); aStmt = mirBuilder->CreateStmtRegassign(PTY_u64, pIdx1R, reg); newBlk.AddStatement(aStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(aStmt->GetStmtID(), stmt->GetStmtID()); + } /* save x1 */ if (origSize > k8ByteSize) { @@ -1622,6 +1705,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, pIdx2R = GetCurrentFunc()->GetPregTab()->CreatePreg(PTY_u64); aStmt = mirBuilder->CreateStmtRegassign(PTY_u64, pIdx2R, reg); newBlk.AddStatement(aStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(aStmt->GetStmtID(), stmt->GetStmtID()); + } } /* save &s */ @@ -1630,6 +1716,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, PregIdx pIdxL = GetCurrentFunc()->GetPregTab()->CreatePreg(GetLoweredPtrType()); aStmt = mirBuilder->CreateStmtRegassign(PTY_a64, pIdxL, regAddr); newBlk.AddStatement(aStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(aStmt->GetStmtID(), stmt->GetStmtID()); + } uint32 curSize = 0; PregIdx pIdxS; @@ -1683,6 +1772,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, pIdx1R = pIdx2R = pIdxS; newBlk.AddStatement(sStmp); + if (funcProfData) { + funcProfData->CopyStmtFreq(sStmp->GetStmtID(), stmt->GetStmtID()); + } size -= k4ByteSize; curSize += k4ByteSize; } else if (size >= k2ByteSize) { @@ -1714,6 +1806,9 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, pIdx1R = pIdx2R = pIdxS; newBlk.AddStatement(sStmp); + if (funcProfData) { + funcProfData->CopyStmtFreq(sStmp->GetStmtID(), stmt->GetStmtID()); + } size -= k2ByteSize; curSize += k2ByteSize; } else { @@ -1743,10 +1838,16 @@ bool CGLowerer::LowerStructReturn(BlockNode &newBlk, StmtNode *stmt, pIdx1R = pIdx2R = pIdxS; newBlk.AddStatement(sStmp); + if (funcProfData) { + funcProfData->CopyStmtFreq(sStmp->GetStmtID(), stmt->GetStmtID()); + } size -= k1ByteSize; curSize += k1ByteSize; } newBlk.AddStatement(aStmt); + if (funcProfData) { + funcProfData->CopyStmtFreq(aStmt->GetStmtID(), stmt->GetStmtID()); + } } } nextStmt = nextStmt->GetNext(); // skip the dassign @@ -1769,8 +1870,11 @@ void CGLowerer::LowerSwitchOpnd(StmtNode &stmt, BlockNode &newBlk) { RegassignNode *regAss = mirBuilder->CreateStmtRegassign(ptyp, pIdx, opnd); newBlk.AddStatement(regAss); GetCurrentFunc()->SetLastFreqMap(regAss->GetStmtID(), - static_cast(GetCurrentFunc()->GetFreqFromLastStmt(stmt.GetStmtID()))); + GetCurrentFunc()->GetFreqFromLastStmt(stmt.GetStmtID())); stmt.SetOpnd(mirBuilder->CreateExprRegread(ptyp, pIdx), 0); + if (funcProfData) { + funcProfData->CopyStmtFreq(regAss->GetStmtID(), stmt.GetStmtID()); + } } else { stmt.SetOpnd(LowerExpr(stmt, *stmt.Opnd(0), newBlk), 0); } @@ -1814,6 +1918,9 @@ StmtNode *CGLowerer::CreateFflushStmt(StmtNode &stmt) { argsFflush.push_back(mirBuilder->CreateExprDread(*stdoutSym)); StmtNode *callFflush = mirBuilder->CreateStmtCall(fflush->GetPuidx(), argsFflush); callFflush->SetSrcPos(stmt.GetSrcPos()); + if (funcProfData) { + funcProfData->CopyStmtFreq(callFflush->GetStmtID(), stmt.GetStmtID()); + } return callFflush; } @@ -1915,6 +2022,9 @@ void CGLowerer::LowerAssertBoundary(StmtNode &stmt, BlockNode &block, BlockNode brFalseNode->SetSrcPos(stmt.GetSrcPos()); labelBC->SetSrcPos(stmt.GetSrcPos()); + if (funcProfData) { + funcProfData->CopyStmtFreq(brFalseNode->GetStmtID(), stmt.GetStmtID()); + } callPrintf->SetSrcPos(stmt.GetSrcPos()); abortModeNode->SetSrcPos(stmt.GetSrcPos()); @@ -1930,6 +2040,9 @@ void CGLowerer::LowerAssertBoundary(StmtNode &stmt, BlockNode &block, BlockNode BlockNode *CGLowerer::LowerBlock(BlockNode &block) { BlockNode *newBlk = mirModule.CurFuncCodeMemPool()->New(); + if (funcProfData) { + funcProfData->CopyStmtFreq(newBlk->GetStmtID(), block.GetStmtID()); + } BlockNode *tmpBlockNode = nullptr; std::vector abortNode; if (block.GetFirst() == nullptr) { @@ -2188,11 +2301,17 @@ void CGLowerer::SplitCallArg(CallNode &callNode, BaseNode *newOpnd, size_t i, Bl DassignNode *dassignNode = mirBuilder->CreateStmtDassign(*ret, 0, newOpnd); newBlk.AddStatement(dassignNode); callNode.SetOpnd(mirBuilder->CreateExprDread(*type, 0, *ret), i); + if (funcProfData) { + funcProfData->CopyStmtFreq(dassignNode->GetStmtID(), callNode.GetStmtID()); + } } else { PregIdx pregIdx = mirModule.CurFunction()->GetPregTab()->CreatePreg(newOpnd->GetPrimType()); RegassignNode *temp = mirBuilder->CreateStmtRegassign(newOpnd->GetPrimType(), pregIdx, newOpnd); newBlk.AddStatement(temp); callNode.SetOpnd(mirBuilder->CreateExprRegread(newOpnd->GetPrimType(), pregIdx), i); + if (funcProfData) { + funcProfData->CopyStmtFreq(temp->GetStmtID(), callNode.GetStmtID()); + } } } else { callNode.SetOpnd(newOpnd, i); @@ -2987,6 +3106,9 @@ StmtNode *CGLowerer::LowerDassignToThreadLocal(StmtNode &stmt) { auto ptrType = GlobalTables::GetTypeTable().GetOrCreatePointerType(*symbol->GetType()); auto iassign = mirModule.GetMIRBuilder()->CreateStmtIassign(*ptrType, dAssign.GetFieldID(), addr, dAssign.GetRHS()); result = iassign; + if (funcProfData) { + funcProfData->CopyStmtFreq(iassign->GetStmtID(), stmt.GetStmtID()); + } } uint32 newTypeTableSize = GlobalTables::GetTypeTable().GetTypeTableSize(); if (newTypeTableSize != oldTypeTableSize) { @@ -3049,6 +3171,9 @@ void CGLowerer::LowerResetStmt(StmtNode &stmt, BlockNode &block) { dassignNode->SetRHS(exprConst); dassignNode->SetFieldID(addrofNode->GetFieldID()); block.AddStatement(dassignNode); + if (funcProfData) { + funcProfData->CopyStmtFreq(dassignNode->GetStmtID(), stmt.GetStmtID()); + } } StmtNode *CGLowerer::LowerIntrinsicopDassign(const DassignNode &dsNode, @@ -3094,6 +3219,9 @@ StmtNode *CGLowerer::LowerIntrinsicopDassign(const DassignNode &dsNode, CallNode *callStmt = mirModule.CurFuncCodeMemPool()->New(mirModule, OP_call); callStmt->SetPUIdx(st->GetFunction()->GetPuidx()); callStmt->SetNOpnd(newOpnd); + if (funcProfData) { + funcProfData->CopyStmtFreq(callStmt->GetStmtID(), dsNode.GetStmtID()); + } return callStmt; } @@ -4222,6 +4350,9 @@ void CGLowerer::LowerFunc(MIRFunction &func) { hasTry = false; LowerEntry(func); LowerPseudoRegs(func); + if (Options::profileUse) { + funcProfData = mirModule.CurFunction()->GetFuncProfData(); + } BuildLabel2FreqMap(); BlockNode *origBody = func.GetBody(); CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); diff --git a/src/mapleall/maple_be/src/be/switch_lowerer.cpp b/src/mapleall/maple_be/src/be/switch_lowerer.cpp index b9f12d3a405caa2a04946401551ebc453c638a7f..61b2ab29af42f3d5856572817aee94658a4c76c4 100644 --- a/src/mapleall/maple_be/src/be/switch_lowerer.cpp +++ b/src/mapleall/maple_be/src/be/switch_lowerer.cpp @@ -173,12 +173,49 @@ CondGotoNode *SwitchLowerer::BuildCondGotoNode(int32 idx, Opcode opCode, BaseNod return cGotoStmt; } +FreqType SwitchLowerer::sumFreq(uint32 startIdx, uint32 endIdx) { + ASSERT(startIdx >= 0 && endIdx >=0 && endIdx >= startIdx, "startIdx or endIdx is invalid"); + if (Options::profileUse && cgLowerer->GetLabel2Freq().size() > 0 && + (startIdx <= switchItems.size() - 1) && (endIdx <= switchItems.size() - 1) && + (startIdx <= endIdx)) { + FreqType freqSum = 0; + bool valid = false; + // Tolerate -1 in [startIdx, endIdx]? + for (uint32 swIdx = startIdx; swIdx <= endIdx; swIdx++) { + FreqType freq; + if (switchItems[swIdx].second == 0) { + freq = cgLowerer->GetLabel2Freq()[stmt->GetCasePair(static_cast(switchItems[swIdx].first)).second]; + if (freq >= 0) { + freqSum += freq; + valid = true; + } + } else { + for (uint32 caseIdx = switchItems[swIdx].first; caseIdx <= switchItems[swIdx].second; caseIdx++) { + freq = cgLowerer->GetLabel2Freq()[stmt->GetCasePair(caseIdx).second]; + if (freq >= 0) { + freqSum += freq; + valid = true; + } + } + } + } + return valid ? freqSum : -1; + } else { + return -1; + } +} + /* start and end is with respect to switchItems */ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool lowBlockNodeChecked, - bool highBlockNodeChecked, LabelIdx newLabelIdx) { + bool highBlockNodeChecked, FreqType freqSum, LabelIdx newLabelIdx) { ASSERT(start >= 0, "invalid args start"); ASSERT(end >= 0, "invalid args end"); BlockNode *localBlk = mirModule.CurFuncCodeMemPool()->New(); + FuncProfInfo *funcProfData = mirModule.CurFunction()->GetFuncProfData(); + FreqType freqSumChecked = 0; + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(localBlk->GetStmtID(), freqSum); + } if (start > end) { return localBlk; } @@ -197,16 +234,27 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l cGoto = BuildCondGotoNode(-1, OP_brtrue, *BuildCmpNode(OP_lt, switchItems[start].first)); if (cGoto != nullptr) { localBlk->AddStatement(cGoto); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(cGoto->GetStmtID(), freqSum - freqSumChecked); + } } } } rangeGoto = BuildRangeGotoNode(switchItems[start].first, switchItems[start].second, newLabelIdx); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(rangeGoto->GetStmtID(), freqSum - freqSumChecked); + freqSumChecked += sumFreq(start, start); + } if (stmt->GetDefaultLabel() == 0) { localBlk->AddStatement(rangeGoto); } else { cmpNode = BuildCmpNode(OP_le, switchItems[start].second); ifStmt = static_cast(mirModule.GetMIRBuilder()->CreateStmtIf(cmpNode)); ifStmt->GetThenPart()->AddStatement(rangeGoto); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(ifStmt->GetThenPart()->GetStmtID(), freqSum + sumFreq(start, start)); + funcProfData->SetStmtFreq(ifStmt->GetStmtID(), freqSum + sumFreq(start, start)); + } localBlk->AppendStatementsFromBlock(*mirLowerer.LowerIfStmt(*ifStmt, false)); } if (start < end) { @@ -221,16 +269,27 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l cGoto = BuildCondGotoNode(-1, OP_brtrue, *BuildCmpNode(OP_gt, switchItems[end].second)); if (cGoto != nullptr) { localBlk->AddStatement(cGoto); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(cGoto->GetStmtID(), freqSum - freqSumChecked); + } } highBlockNodeChecked = true; } rangeGoto = BuildRangeGotoNode(switchItems[end].first, switchItems[end].second, newLabelIdx); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(rangeGoto->GetStmtID(), freqSum - freqSumChecked); + freqSumChecked += sumFreq(end, end); + } if (stmt->GetDefaultLabel() == 0) { localBlk->AddStatement(rangeGoto); } else { cmpNode = BuildCmpNode(OP_ge, switchItems[end].first); ifStmt = static_cast(mirModule.GetMIRBuilder()->CreateStmtIf(cmpNode)); ifStmt->GetThenPart()->AddStatement(rangeGoto); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(ifStmt->GetThenPart()->GetStmtID(), freqSum + sumFreq(end, end)); + funcProfData->SetStmtFreq(ifStmt->GetStmtID(), freqSum + sumFreq(end, end)); + } localBlk->AppendStatementsFromBlock(*mirLowerer.LowerIfStmt(*ifStmt, false)); } if (start < end) { @@ -247,6 +306,9 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l GotoNode *gotoDft = BuildGotoNode(-1); if (gotoDft != nullptr) { localBlk->AddStatement(gotoDft); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(gotoDft->GetStmtID(), freqSum - freqSumChecked); + } jumpToDefaultBlockGenerated = true; } } @@ -257,16 +319,19 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l auto *gotoStmt = BuildGotoNode(switchItems[static_cast(start)].first); if (gotoStmt != nullptr) { localBlk->AddStatement(gotoStmt); + if (Options::profileUse && funcProfData != nullptr) { + funcProfData->SetStmtFreq(gotoStmt->GetStmtID(), freqSum - freqSumChecked); + } } return localBlk; } if (end < (start + kClusterSwitchCutoff)) { /* generate equality checks for what remains */ - std::vector > freq2case; + std::vector > freq2case; int32 lastIdx = -1; bool freqPriority = false; // The setting of kClusterSwitchDensityLow to such a lower value (0.2) makes other strategies less useful - if (Options::profileUse && cgLowerer->GetLabel2Freq().size()) { + if (Options::profileUse && funcProfData != nullptr && cgLowerer->GetLabel2Freq().size()) { for (int32 idx = start; idx <= end; idx++) { if (switchItems[static_cast(idx)].second == 0) { freq2case.push_back(std::make_pair(cgLowerer->GetLabel2Freq()[stmt->GetCasePair(static_cast( @@ -283,12 +348,14 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l } } - if (Options::profileUse && freqPriority) { - for (std::pair f2c : freq2case) { + if (Options::profileUse && funcProfData != nullptr && freqPriority) { + for (std::pair f2c : freq2case) { uint32 idx = static_cast(f2c.second); cGoto = BuildCondGotoNode(idx, OP_brtrue, *BuildCmpNode(OP_eq, idx)); if (cGoto != nullptr) { localBlk->AddStatement(cGoto); + funcProfData->SetStmtFreq(cGoto->GetStmtID(), freqSum - freqSumChecked); + freqSumChecked += cgLowerer->GetLabel2Freq()[stmt->GetCasePair(idx).second]; } } @@ -320,7 +387,8 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l } } if (start <= end) { /* recursive call */ - BlockNode *tmp = BuildCodeForSwitchItems(start, end, lowBlockNodeChecked, highBlockNodeChecked); + BlockNode *tmp = BuildCodeForSwitchItems(start, end, lowBlockNodeChecked, highBlockNodeChecked, + sumFreq(start, end) ); CHECK_FATAL(tmp != nullptr, "tmp should not be nullptr"); localBlk->AppendStatementsFromBlock(*tmp); } else if (!lowBlockNodeChecked || !highBlockNodeChecked) { @@ -362,11 +430,28 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l stmt->GetCasePair(switchItems.at(mid).first).first) || (stmt->GetCasePair(switchItems.at(mid - 1).second).first + 1 == stmt->GetCasePair(switchItems.at(mid).first).first); - ifStmt->SetThenPart(BuildCodeForSwitchItems(start, mid - 1, lowBlockNodeChecked, leftHighBNdChecked)); - ifStmt->SetElsePart(BuildCodeForSwitchItems(mid, end, true, highBlockNodeChecked)); + if (Options::profileUse && funcProfData != nullptr) { + ifStmt->SetThenPart(BuildCodeForSwitchItems(start, mid - 1, lowBlockNodeChecked, leftHighBNdChecked, + sumFreq(start, mid - 1))); + ifStmt->SetElsePart(BuildCodeForSwitchItems(mid, end, true, highBlockNodeChecked, + sumFreq(mid, end))); + } else { + ifStmt->SetThenPart(BuildCodeForSwitchItems(start, mid - 1, lowBlockNodeChecked, leftHighBNdChecked, -1)); + ifStmt->SetElsePart(BuildCodeForSwitchItems(mid, end, true, highBlockNodeChecked, -1)); + } if (ifStmt->GetElsePart()) { ifStmt->SetNumOpnds(kOperandNumTernary); } + if (Options::profileUse && funcProfData != nullptr) { + if ((funcProfData->GetStmtFreq(ifStmt->GetThenPart()->GetStmtID()) >= 0) && + (funcProfData->GetStmtFreq(ifStmt->GetElsePart()->GetStmtID()) >= 0)) { + funcProfData->SetStmtFreq(ifStmt->GetStmtID(), + funcProfData->GetStmtFreq(ifStmt->GetThenPart()->GetStmtID()) + + funcProfData->GetStmtFreq(ifStmt->GetElsePart()->GetStmtID())); + } else { + funcProfData->SetStmtFreq(ifStmt->GetStmtID(), -1); + } + } localBlk->AppendStatementsFromBlock(*mirLowerer.LowerIfStmt(*ifStmt, false)); } return localBlk; @@ -392,7 +477,8 @@ BlockNode *SwitchLowerer::LowerSwitch(LabelIdx newLabelIdx) { stmt->SortCasePair(CasePairKeyLessThan); FindClusters(clusters); InitSwitchItems(clusters); - BlockNode *blkNode = BuildCodeForSwitchItems(0, static_cast(switchItems.size()) - 1, false, false, newLabelIdx); + BlockNode *blkNode = BuildCodeForSwitchItems(0, static_cast(switchItems.size()) - 1, false, false, + sumFreq(0, switchItems.size() - 1), newLabelIdx); if (!jumpToDefaultBlockGenerated) { GotoNode *gotoDft = BuildGotoNode(-1); if (gotoDft != nullptr) { diff --git a/src/mapleall/maple_be/src/cg/cg_profile_use.cpp b/src/mapleall/maple_be/src/cg/cg_profile_use.cpp index 2bc70097573a8555994c57e1905d6e330f691fbd..bbdf814610af1a162c9b8300d0ec3a7d4301e411 100644 --- a/src/mapleall/maple_be/src/cg/cg_profile_use.cpp +++ b/src/mapleall/maple_be/src/cg/cg_profile_use.cpp @@ -26,18 +26,19 @@ void CgProfUse::setupProf() { // Initialize all BBs with freq of stmts within it FOR_ALL_BB(bb, cgFunc) { bb->InitEdgeProfFreq(); +#if DEBUG if (bb->GetFirstStmt() && - (static_cast(funcProf->GetStmtFreq(bb->GetFirstStmt()->GetStmtID())) > 0)) { + (static_cast(funcProf->GetStmtFreq(bb->GetFirstStmt()->GetStmtID())) >= 0)) { bb->SetProfFreq(funcProf->GetStmtFreq(bb->GetFirstStmt()->GetStmtID())); } else if (bb->GetLastStmt() && - (static_cast(funcProf->GetStmtFreq(bb->GetLastStmt()->GetStmtID())) > 0)) { + (static_cast(funcProf->GetStmtFreq(bb->GetLastStmt()->GetStmtID())) >= 0)) { bb->SetProfFreq(funcProf->GetStmtFreq(bb->GetLastStmt()->GetStmtID())); } else { -#if DEBUG - LogInfo::MapleLogger() << "BB" << bb->GetId() << " is not in execution path\n"; -#endif + if (!CGOptions::IsQuiet()) { + LogInfo::MapleLogger() << "BB" << bb->GetId() << " : frequency undetermined\n"; + } } - +#endif } // Propagate BB freq to edge freq @@ -71,10 +72,10 @@ bool CGProfUse::PhaseRun(maplebe::CGFunc &f) { cgprofuse.setupProf(); #if DEBUG - LogInfo::MapleLogger() << "Setup CG ProfileUse : " << f.GetName() << "\n"; - DotGenerator::GenerateDot("CGProfUse", f, f.GetMirModule(), false, f.GetName()); + if (CGOptions::FuncFilter(f.GetName())) { + DotGenerator::GenerateDot("after-CGProfUse", f, f.GetMirModule(), false, f.GetName()); + } #endif - return false; } diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index d80a758c8fb75e30ac82c166e472af54b35093ee..0672c700750de79efdbf58f3d94304c8ffa8cfbf 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -1656,14 +1656,13 @@ void CGFunc::GenerateScopeLabel(StmtNode *stmt, SrcPosition &lastSrcPos, bool &p } } -int32 CGFunc::GetFreqFromStmt(uint32 stmtId) { - int32 freq = GetFunction().GetFreqFromLastStmt(stmtId); +FreqType CGFunc::GetFreqFromStmt(uint32 stmtId) { + FreqType freq = GetFunction().GetFreqFromLastStmt(stmtId); if (freq != -1) { return freq; } return GetFunction().GetFreqFromFirstStmt(stmtId); } - LmbcFormalParamInfo *CGFunc::GetLmbcFormalParamInfo(uint32 offset) { MapleVector ¶mVec = GetLmbcParamVec(); for (auto *param : paramVec) { diff --git a/src/mapleall/maple_ipa/include/ipa_clone.h b/src/mapleall/maple_ipa/include/ipa_clone.h index ccb3f4ac78a3a142f02e7b92c60a134d62034cda..c2a3b7bc1892aeef3dc892a591bbce910585572f 100644 --- a/src/mapleall/maple_ipa/include/ipa_clone.h +++ b/src/mapleall/maple_ipa/include/ipa_clone.h @@ -39,7 +39,7 @@ class IpaClone : public AnalysisResult { static void IpaClonePregTable(MIRFunction &newFunc, const MIRFunction &oldFunc); MIRFunction *IpaCloneFunction(MIRFunction &originalFunction, const std::string &fullName) const; MIRFunction *IpaCloneFunctionWithFreq(MIRFunction &originalFunction, - const std::string &fullName, uint64_t callSiteFreq) const; + const std::string &fullName, FreqType callSiteFreq) const; bool IsBrCondOrIf(Opcode op) const; void DoIpaClone(); void InitParams(); diff --git a/src/mapleall/maple_ipa/include/stmt_identify.h b/src/mapleall/maple_ipa/include/stmt_identify.h index b60e80770aeb184162c215bedcf13eb29cdbdb32..78caef149f5a28c9d8c726a6ef79c95312b9fe36 100644 --- a/src/mapleall/maple_ipa/include/stmt_identify.h +++ b/src/mapleall/maple_ipa/include/stmt_identify.h @@ -196,11 +196,11 @@ class StmtInfo { return puIdx; } - const uint64 GetFrequency() const { + const FreqType GetFrequency() const { return frequency; } - void SetFrequency(uint64 freq) { + void SetFrequency(FreqType freq) { frequency = freq; } @@ -235,7 +235,7 @@ class StmtInfo { BlockNode *currBlock = nullptr; MeStmt *meStmt = nullptr; PUIdx puIdx = kInvalidPuIdx; - uint64 frequency = 0; + FreqType frequency = 0; bool valid = true; MapleVector hashCandidate; MapleUnorderedMap symbolDefUse; diff --git a/src/mapleall/maple_ipa/src/ipa_clone.cpp b/src/mapleall/maple_ipa/src/ipa_clone.cpp index fcbcf84c832052263b51a4cc6d1b02dceed3f898..ca617549af9d536db4a3b697f409682be27d91a0 100644 --- a/src/mapleall/maple_ipa/src/ipa_clone.cpp +++ b/src/mapleall/maple_ipa/src/ipa_clone.cpp @@ -129,7 +129,7 @@ MIRFunction *IpaClone::IpaCloneFunction(MIRFunction &originalFunction, const std } MIRFunction *IpaClone::IpaCloneFunctionWithFreq(MIRFunction &originalFunction, - const std::string &fullName, uint64_t callSiteFreq) const { + const std::string &fullName, FreqType callSiteFreq) const { MapleAllocator cgAlloc(originalFunction.GetDataMemPool()); ArgVector argument(cgAlloc.Adapter()); IpaCloneArgument(originalFunction, argument); @@ -386,7 +386,7 @@ void IpaClone::DecideCloneFunction(std::vector &result, uint32 paramInd InlineTransformer::ConvertPStaticToFStatic(*curFunc); MIRFunction *newFunc = nullptr; if (Options::profileUse && curFunc->GetFuncProfData()) { - uint64_t clonedSiteFreqs = 0; + FreqType clonedSiteFreqs = 0; for (auto &value: calleeValue) { for (auto &callSite : calleeInfo[keyPair][value]) { MIRFunction *callerFunc = GlobalTables::GetFunctionTable().GetFunctionFromPuidx(callSite.GetPuidx()); @@ -398,7 +398,7 @@ void IpaClone::DecideCloneFunction(std::vector &result, uint32 paramInd if (callerFunc->GetFuncProfData() == nullptr) { continue; } - uint64_t callsiteFreq = callerFunc->GetFuncProfData()->GetStmtFreq(stmtId); + FreqType callsiteFreq = callerFunc->GetFuncProfData()->GetStmtFreq(stmtId); clonedSiteFreqs += callsiteFreq; } } @@ -540,7 +540,7 @@ void IpaClone::CloneNoImportantExpressFunction(MIRFunction *func, uint32 paramIn InlineTransformer::ConvertPStaticToFStatic(*func); MIRFunction *newFunc = nullptr; if (Options::profileUse && func->GetFuncProfData()) { - uint64_t clonedSiteFreqs = 0; + FreqType clonedSiteFreqs = 0; int64_t value = calleeInfo[keyPair].cbegin()->first; for (auto &callSite : std::as_const(calleeInfo[keyPair][value])) { MIRFunction *callerFunc = GlobalTables::GetFunctionTable().GetFunctionFromPuidx(callSite.GetPuidx()); @@ -552,7 +552,7 @@ void IpaClone::CloneNoImportantExpressFunction(MIRFunction *func, uint32 paramIn if (callerFunc->GetFuncProfData() == nullptr) { continue; } - uint64_t callsiteFreq = callerFunc->GetFuncProfData()->GetStmtFreq(stmtId); + FreqType callsiteFreq = callerFunc->GetFuncProfData()->GetStmtFreq(stmtId); clonedSiteFreqs += callsiteFreq; } newFunc = IpaCloneFunctionWithFreq(*func, newFuncName, clonedSiteFreqs); diff --git a/src/mapleall/maple_ir/include/mir_function.h b/src/mapleall/maple_ir/include/mir_function.h index 2363180fa4fd458e6c613cfa682bb1ae3d5fe1c0..2c7b3e38a1f9cf73ab6fa4438f6f55d6fb86853e 100644 --- a/src/mapleall/maple_ir/include/mir_function.h +++ b/src/mapleall/maple_ir/include/mir_function.h @@ -771,22 +771,22 @@ class MIRFunction { return freqFirstMap != nullptr; } - const MapleMap &GetFirstFreqMap() const { + const MapleMap &GetFirstFreqMap() const { return *freqFirstMap; } - void SetFirstFreqMap(uint32 stmtID, uint64 freq) { + void SetFirstFreqMap(uint32 stmtID, FreqType freq) { if (freqFirstMap == nullptr) { - freqFirstMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); + freqFirstMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); } (*freqFirstMap)[stmtID] = freq; } - const MapleMap &GetLastFreqMap() const { + const MapleMap &GetLastFreqMap() const { return *freqLastMap; } - int64 GetFreqFromLastStmt(uint32 stmtId) const { + FreqType GetFreqFromLastStmt(uint32 stmtId) const { if (freqLastMap == nullptr) { return -1; } @@ -796,7 +796,7 @@ class MIRFunction { return (*freqLastMap)[stmtId]; } - int64 GetFreqFromFirstStmt(uint32 stmtId) const { + FreqType GetFreqFromFirstStmt(uint32 stmtId) const { if (freqFirstMap == nullptr) { return -1; } @@ -806,9 +806,9 @@ class MIRFunction { return (*freqFirstMap)[stmtId]; } - void SetLastFreqMap(uint32 stmtID, uint64 freq) { + void SetLastFreqMap(uint32 stmtID, FreqType freq) { if (freqLastMap == nullptr) { - freqLastMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); + freqLastMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); } (*freqLastMap)[stmtID] = freq; } @@ -1284,9 +1284,9 @@ class MIRFunction { FuncProfInfo* GetFuncProfData() const { return funcProfData; } - void SetStmtFreq(uint32_t stmtID, uint64_t freq) { + void SetStmtFreq(uint32_t stmtID, FreqType freq) { ASSERT((funcProfData != nullptr && freq > 0), "nullptr check"); - funcProfData->SetStmtFreq(stmtID, static_cast(freq)); + funcProfData->SetStmtFreq(stmtID, freq); } void SetMayWriteToAddrofStack() { @@ -1345,8 +1345,8 @@ class MIRFunction { MIRInfoVector info{module->GetMPAllocator().Adapter()}; MapleVector infoIsString{module->GetMPAllocator().Adapter()}; // tells if an entry has string value MIRScope *scope = nullptr; - MapleMap *freqFirstMap = nullptr; // save bb frequency in its first_stmt, key is stmtId - MapleMap *freqLastMap = nullptr; // save bb frequency in its last_stmt, key is stmtId + MapleMap *freqFirstMap = nullptr; // save bb frequency in its first_stmt, key is stmtId + MapleMap *freqLastMap = nullptr; // save bb frequency in its last_stmt, key is stmtId MapleSet referedPregs{module->GetMPAllocator().Adapter()}; bool referedRegsValid = false; bool hasVlaOrAlloca = false; diff --git a/src/mapleall/maple_ir/include/mir_lower.h b/src/mapleall/maple_ir/include/mir_lower.h index 104a0a932aed185e70955c2aab803e6f2e5ba0c7..44f1713276eb0564ff2c57a77e92748980a646d9 100644 --- a/src/mapleall/maple_ir/include/mir_lower.h +++ b/src/mapleall/maple_ir/include/mir_lower.h @@ -153,7 +153,7 @@ class MIRLower { if (newStmt == oldStmt) { return; } - uint64_t freq = GetFuncProfData()->GetStmtFreq(oldStmt->GetStmtID()); + FreqType freq = GetFuncProfData()->GetStmtFreq(oldStmt->GetStmtID()); GetFuncProfData()->SetStmtFreq(newStmt->GetStmtID(), freq); } diff --git a/src/mapleall/maple_ir/include/mir_nodes.h b/src/mapleall/maple_ir/include/mir_nodes.h index 4457509c6c8c1a1e5980c8f65cced68ed3952ef1..e5f35f8283b70b30eaa25a50887f6f90de150e2d 100644 --- a/src/mapleall/maple_ir/include/mir_nodes.h +++ b/src/mapleall/maple_ir/include/mir_nodes.h @@ -27,6 +27,7 @@ #include "opcodes.h" #include "ptr_list_ref.h" #include "src_position.h" +#include "types_def.h" namespace maple { constexpr size_t kFirstOpnd = 0; @@ -2421,8 +2422,8 @@ class BlockNode : public StmtNode { BlockNode *CloneTreeWithSrcPosition(const MIRModule &mod, const GStrIdx &idx = GStrIdx(), bool setInlinedPos = false, const SrcPosition &inlinedPosition = SrcPosition()); - BlockNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, - std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, + BlockNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, + std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, uint32_t updateOp); bool IsEmpty() const { @@ -2563,18 +2564,18 @@ class IfStmtNode : public UnaryStmtNode { return node; } - IfStmtNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, - std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, + IfStmtNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, + std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, uint32_t updateOp) { auto *node = allocator.GetMemPool()->New(*this); node->SetStmtID(stmtIDNext++); node->SetOpnd(Opnd()->CloneTree(allocator), 0); if (fromFreqs.count(GetStmtID()) > 0) { - uint64_t oldFreq = fromFreqs[GetStmtID()]; - uint64_t newFreq = numer == 0 ? 0 : (denom > 0 ? (oldFreq * numer / denom) : oldFreq); + FreqType oldFreq = fromFreqs[GetStmtID()]; + FreqType newFreq = numer == 0 ? 0 : (denom > 0 ? (oldFreq * numer / denom) : oldFreq); toFreqs[node->GetStmtID()] = (newFreq > 0 || numer == 0) ? newFreq : 1; if (updateOp & kUpdateOrigFreq) { - uint64_t left = ((oldFreq - newFreq) > 0 || oldFreq == 0) ? (oldFreq - newFreq) : 1; + FreqType left = ((oldFreq - newFreq) > 0 || oldFreq == 0) ? (oldFreq - newFreq) : 1; fromFreqs[GetStmtID()] = left; } } @@ -2648,18 +2649,18 @@ class WhileStmtNode : public UnaryStmtNode { return node; } - WhileStmtNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, - std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, + WhileStmtNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, + std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, uint32_t updateOp) { auto *node = allocator.GetMemPool()->New(*this); node->SetStmtID(stmtIDNext++); if (fromFreqs.count(GetStmtID()) > 0) { - int64_t oldFreq = fromFreqs[GetStmtID()]; - int64_t newFreq = + FreqType oldFreq = fromFreqs[GetStmtID()]; + FreqType newFreq = numer == 0 ? 0 : (denom > 0 ? static_cast(static_cast(oldFreq) * numer / denom) : oldFreq); toFreqs[node->GetStmtID()] = (newFreq > 0 || numer == 0) ? static_cast(newFreq) : 1; if (updateOp & kUpdateOrigFreq) { - int64_t left = (oldFreq - newFreq) > 0 ? (oldFreq - newFreq) : 1; + FreqType left = (oldFreq - newFreq) > 0 ? (oldFreq - newFreq) : 1; fromFreqs[GetStmtID()] = static_cast(left); } } @@ -2719,27 +2720,27 @@ class DoloopNode : public StmtNode { return node; } - DoloopNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, - std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, + DoloopNode *CloneTreeWithFreqs(MapleAllocator &allocator, std::unordered_map &toFreqs, + std::unordered_map &fromFreqs, uint64_t numer, uint64_t denom, uint32_t updateOp) { auto *node = allocator.GetMemPool()->New(*this); node->SetStmtID(stmtIDNext++); if (fromFreqs.count(GetStmtID()) > 0) { - uint64_t oldFreq = fromFreqs[GetStmtID()]; - uint64_t newFreq = oldFreq; + FreqType oldFreq = fromFreqs[GetStmtID()]; + FreqType newFreq = oldFreq; if ((updateOp & kUpdateFreqbyScale) != 0) { // used in inline/clone newFreq = numer == 0 ? 0 : (denom > 0 ? (oldFreq * numer / denom) : oldFreq); } else if ((updateOp & kUpdateUnrolledFreq) != 0) { // used in unrolled part - uint64_t bodyFreq = fromFreqs[GetDoBody()->GetStmtID()]; + FreqType bodyFreq = fromFreqs[GetDoBody()->GetStmtID()]; newFreq = denom > 0 ? (bodyFreq * numer / denom + (oldFreq - bodyFreq)) : oldFreq; } else if ((updateOp & kUpdateUnrollRemainderFreq) != 0) { // used in unrolled remainder - uint64_t bodyFreq = fromFreqs[GetDoBody()->GetStmtID()]; + FreqType bodyFreq = fromFreqs[GetDoBody()->GetStmtID()]; newFreq = denom > 0 ? (((bodyFreq * numer) % denom) + (oldFreq - bodyFreq)) : oldFreq; } toFreqs[node->GetStmtID()] = static_cast(newFreq); ASSERT(oldFreq >= newFreq, "sanity check"); if ((updateOp & kUpdateOrigFreq) != 0) { - uint64_t left = oldFreq - newFreq; + FreqType left = oldFreq - newFreq; fromFreqs[GetStmtID()] = left; } } diff --git a/src/mapleall/maple_ir/include/types_def.h b/src/mapleall/maple_ir/include/types_def.h index fa5f01ade2afd3bbc4272ecf4e21c636cd15393d..cb86accc54b29fb865097ac525091ee526364e47 100644 --- a/src/mapleall/maple_ir/include/types_def.h +++ b/src/mapleall/maple_ir/include/types_def.h @@ -33,6 +33,7 @@ using uint8 = std::uint8_t; using uint16 = std::uint16_t; using uint32 = std::uint32_t; using uint64 = std::uint64_t; +using FreqType = std::int64_t; class StIdx { // scope nesting level + symbol table index public: union un { diff --git a/src/mapleall/maple_ir/src/mir_lower.cpp b/src/mapleall/maple_ir/src/mir_lower.cpp index 38b6507b76e3d393df6dcac22786912ab11d2bd2..3acc19613973bd1f02e44bac2e59528be4681a0c 100644 --- a/src/mapleall/maple_ir/src/mir_lower.cpp +++ b/src/mapleall/maple_ir/src/mir_lower.cpp @@ -157,9 +157,9 @@ void MIRLower::CreateBrFalseStmt(BlockNode &blk, const IfStmtNode &ifStmt) { // set stmtfreqs if (GetFuncProfData()) { ASSERT(GetFuncProfData()->GetStmtFreq(ifStmt.GetThenPart()->GetStmtID()) >= 0, "sanity check"); - int64_t freq = static_cast(GetFuncProfData()->GetStmtFreq(ifStmt.GetStmtID()) - - GetFuncProfData()->GetStmtFreq(ifStmt.GetThenPart()->GetStmtID())); - GetFuncProfData()->SetStmtFreq(lableStmt->GetStmtID(), static_cast(freq)); + FreqType freq = GetFuncProfData()->GetStmtFreq(ifStmt.GetStmtID()) - + GetFuncProfData()->GetStmtFreq(ifStmt.GetThenPart()->GetStmtID()); + GetFuncProfData()->SetStmtFreq(lableStmt->GetStmtID(), freq); } } @@ -171,9 +171,9 @@ void MIRLower::CreateBrTrueStmt(BlockNode &blk, const IfStmtNode &ifStmt) { // set stmtfreqs if (GetFuncProfData()) { ASSERT(GetFuncProfData()->GetStmtFreq(ifStmt.GetElsePart()->GetStmtID()) >= 0, "sanity check"); - int64_t freq = static_cast(GetFuncProfData()->GetStmtFreq(ifStmt.GetStmtID()) - - GetFuncProfData()->GetStmtFreq(ifStmt.GetElsePart()->GetStmtID())); - GetFuncProfData()->SetStmtFreq(lableStmt->GetStmtID(), static_cast(freq)); + FreqType freq = GetFuncProfData()->GetStmtFreq(ifStmt.GetStmtID()) - + GetFuncProfData()->GetStmtFreq(ifStmt.GetElsePart()->GetStmtID()); + GetFuncProfData()->SetStmtFreq(lableStmt->GetStmtID(), freq); } } @@ -383,10 +383,10 @@ BlockNode *MIRLower::LowerWhileStmt(WhileStmtNode &whileStmt) { lableStmt->SetLabelIdx(lalbeIdx); blk->AddStatement(lableStmt); if (GetFuncProfData()) { - int64_t freq = static_cast(GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) - - GetFuncProfData()->GetStmtFreq(blk->GetLast()->GetStmtID())); + FreqType freq = GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) - + GetFuncProfData()->GetStmtFreq(blk->GetLast()->GetStmtID()); ASSERT(freq >= 0, "sanity check"); - GetFuncProfData()->SetStmtFreq(lableStmt->GetStmtID(), static_cast(freq)); + GetFuncProfData()->SetStmtFreq(lableStmt->GetStmtID(), freq); } return blk; } @@ -403,11 +403,11 @@ BlockNode *MIRLower::LowerWhileStmt(WhileStmtNode &whileStmt) { BlockNode *MIRLower::LowerDoloopStmt(DoloopNode &doloop) { ASSERT(doloop.GetDoBody() != nullptr, "nullptr check"); doloop.SetDoBody(LowerBlock(*doloop.GetDoBody())); - int64_t doloopnodeFreq = 0; - int64_t bodynodeFreq = 0; + FreqType doloopnodeFreq = 0; + FreqType bodynodeFreq = 0; if (GetFuncProfData()) { - doloopnodeFreq = static_cast(GetFuncProfData()->GetStmtFreq(doloop.GetStmtID())); - bodynodeFreq = static_cast(GetFuncProfData()->GetStmtFreq(doloop.GetDoBody()->GetStmtID())); + doloopnodeFreq = GetFuncProfData()->GetStmtFreq(doloop.GetStmtID()); + bodynodeFreq = GetFuncProfData()->GetStmtFreq(doloop.GetDoBody()->GetStmtID()); if (doloopnodeFreq < bodynodeFreq) { doloopnodeFreq = bodynodeFreq; } @@ -432,7 +432,7 @@ BlockNode *MIRLower::LowerDoloopStmt(DoloopNode &doloop) { blk->AddStatement(startDassign); } if (GetFuncProfData()) { - GetFuncProfData()->SetStmtFreq(blk->GetLast()->GetStmtID(), static_cast(doloopnodeFreq - bodynodeFreq)); + GetFuncProfData()->SetStmtFreq(blk->GetLast()->GetStmtID(), doloopnodeFreq - bodynodeFreq); } auto *brFalseStmt = mirModule.CurFuncCodeMemPool()->New(OP_brfalse); brFalseStmt->SetOpnd(doloop.GetCondExpr(), 0); @@ -442,7 +442,7 @@ BlockNode *MIRLower::LowerDoloopStmt(DoloopNode &doloop) { blk->AddStatement(brFalseStmt); // udpate stmtFreq if (GetFuncProfData()) { - GetFuncProfData()->SetStmtFreq(brFalseStmt->GetStmtID(), static_cast(doloopnodeFreq - bodynodeFreq)); + GetFuncProfData()->SetStmtFreq(brFalseStmt->GetStmtID(), doloopnodeFreq - bodynodeFreq); } LabelIdx bodyLabelIdx = mirModule.CurFunction()->GetLabelTab()->CreateLabel(); mirModule.CurFunction()->GetLabelTab()->AddToStringLabelMap(bodyLabelIdx); @@ -451,7 +451,7 @@ BlockNode *MIRLower::LowerDoloopStmt(DoloopNode &doloop) { blk->AddStatement(labelStmt); // udpate stmtFreq if (GetFuncProfData()) { - GetFuncProfData()->SetStmtFreq(labelStmt->GetStmtID(), static_cast(bodynodeFreq)); + GetFuncProfData()->SetStmtFreq(labelStmt->GetStmtID(), bodynodeFreq); } blk->AppendStatementsFromBlock(*doloop.GetDoBody()); if (doloop.IsPreg()) { @@ -487,14 +487,14 @@ BlockNode *MIRLower::LowerDoloopStmt(DoloopNode &doloop) { blk->AddStatement(brTrueStmt); // udpate stmtFreq if (GetFuncProfData()) { - GetFuncProfData()->SetStmtFreq(brTrueStmt->GetStmtID(), static_cast(bodynodeFreq)); + GetFuncProfData()->SetStmtFreq(brTrueStmt->GetStmtID(), bodynodeFreq); } labelStmt = mirModule.CurFuncCodeMemPool()->New(); labelStmt->SetLabelIdx(lIdx); blk->AddStatement(labelStmt); // udpate stmtFreq if (GetFuncProfData()) { - GetFuncProfData()->SetStmtFreq(labelStmt->GetStmtID(), static_cast(doloopnodeFreq - bodynodeFreq)); + GetFuncProfData()->SetStmtFreq(labelStmt->GetStmtID(), doloopnodeFreq - bodynodeFreq); } return blk; } diff --git a/src/mapleall/maple_ir/src/mir_nodes.cpp b/src/mapleall/maple_ir/src/mir_nodes.cpp index af7dba2c217e320a461c68df92fde9a7e092d30d..1ef02442b7b0395c0fd339b9a171fee4e54afa1e 100644 --- a/src/mapleall/maple_ir/src/mir_nodes.cpp +++ b/src/mapleall/maple_ir/src/mir_nodes.cpp @@ -282,14 +282,14 @@ BlockNode *BlockNode::CloneTreeWithSrcPosition(const MIRModule &mod, const GStrI } BlockNode *BlockNode::CloneTreeWithFreqs(MapleAllocator &allocator, - std::unordered_map& toFreqs, - std::unordered_map& fromFreqs, + std::unordered_map& toFreqs, + std::unordered_map& fromFreqs, uint64_t numer, uint64_t denom, uint32_t updateOp) { auto *nnode = allocator.GetMemPool()->New(); nnode->SetStmtID(stmtIDNext++); if (fromFreqs.count(GetStmtID()) > 0) { - uint64_t oldFreq = fromFreqs[GetStmtID()]; - uint64_t newFreq; + FreqType oldFreq = fromFreqs[GetStmtID()]; + FreqType newFreq; if (updateOp & kUpdateUnrollRemainderFreq) { newFreq = denom > 0 ? (oldFreq * numer % denom) : oldFreq; } else { @@ -319,8 +319,8 @@ BlockNode *BlockNode::CloneTreeWithFreqs(MapleAllocator &allocator, } else { newStmt = static_cast(stmt.CloneTree(allocator)); if (fromFreqs.count(stmt.GetStmtID()) > 0) { - uint64_t oldFreq = fromFreqs[stmt.GetStmtID()]; - uint64_t newFreq; + FreqType oldFreq = fromFreqs[stmt.GetStmtID()]; + FreqType newFreq; if ((updateOp & kUpdateUnrollRemainderFreq) != 0) { newFreq = denom > 0 ? (oldFreq * numer % denom) : oldFreq; } else { @@ -329,7 +329,7 @@ BlockNode *BlockNode::CloneTreeWithFreqs(MapleAllocator &allocator, toFreqs[newStmt->GetStmtID()] = (newFreq > 0 || oldFreq == 0 || numer == 0) ? static_cast(newFreq) : 1; if ((updateOp & kUpdateOrigFreq) != 0) { - int64_t left = static_cast(((oldFreq - newFreq) > 0 || oldFreq == 0) ? (oldFreq - newFreq) : 1); + FreqType left = static_cast(((oldFreq - newFreq) > 0 || oldFreq == 0) ? (oldFreq - newFreq) : 1); fromFreqs[stmt.GetStmtID()] = static_cast(left); } } @@ -727,7 +727,7 @@ void StmtNode::DumpBase(int32 indent) const { srcPosition.DumpLoc(lastPrintedLineNum, lastPrintedColumnNum); // dump stmtFreqs if (Options::profileUse && theMIRModule->CurFunction()->GetFuncProfData()) { - int64_t freq = static_cast(theMIRModule->CurFunction()->GetFuncProfData()->GetStmtFreq(GetStmtID())); + FreqType freq = theMIRModule->CurFunction()->GetFuncProfData()->GetStmtFreq(GetStmtID()); if (freq >= 0) { LogInfo::MapleLogger() << "stmtID " << GetStmtID() << " freq " << freq << "\n"; } @@ -1368,7 +1368,7 @@ void BlockNode::Dump(int32 indent, const MIRSymbolTable *theSymTab, MIRPregTable srcPosition.DumpLoc(lastPrintedLineNum, lastPrintedColumnNum); // dump stmtFreqs if (Options::profileUse && theMIRModule->CurFunction()->GetFuncProfData()) { - int64_t freq = static_cast(theMIRModule->CurFunction()->GetFuncProfData()->GetStmtFreq(GetStmtID())); + FreqType freq = theMIRModule->CurFunction()->GetFuncProfData()->GetStmtFreq(GetStmtID()); if (freq >= 0) { LogInfo::MapleLogger() << "stmtID " << GetStmtID() << " freq " << freq << "\n"; } @@ -1386,7 +1386,7 @@ void LabelNode::Dump(int32 indent [[maybe_unused]]) const { } // dump stmtFreqs if (Options::profileUse && theMIRModule->CurFunction()->GetFuncProfData()) { - int64_t freq = static_cast(theMIRModule->CurFunction()->GetFuncProfData()->GetStmtFreq(GetStmtID())); + FreqType freq = theMIRModule->CurFunction()->GetFuncProfData()->GetStmtFreq(GetStmtID()); if (freq >= 0) { LogInfo::MapleLogger() << "stmtID " << GetStmtID() << " freq " << freq << "\n"; } diff --git a/src/mapleall/maple_me/include/bb.h b/src/mapleall/maple_me/include/bb.h index ed14b265bb2ed18d97b098a5bb41c61a8b9cf4b3..492a3d8bf037a2e3311827bfe4b44b7637851bbb 100644 --- a/src/mapleall/maple_me/include/bb.h +++ b/src/mapleall/maple_me/include/bb.h @@ -365,11 +365,11 @@ class BB : public BaseGraphNode { bbLabel = idx; } - uint64 GetFrequency() const { + FreqType GetFrequency() const { return frequency; } - void SetFrequency(uint64 f) { + void SetFrequency(FreqType f) { frequency = f; } @@ -424,18 +424,20 @@ class BB : public BaseGraphNode { pred[cnt] = pp; } - void PushBackSuccFreq(uint64 freq) { + void PushBackSuccFreq(FreqType freq) { return succFreq.push_back(freq); } - MapleVector &GetSuccFreq() { + MapleVector &GetSuccFreq() { return succFreq; } - void SetSuccFreq(int idx, uint64 freq) { + + void SetSuccFreq(int idx, FreqType freq) { ASSERT(idx >= 0 && idx <= succFreq.size(), "sanity check"); succFreq[static_cast(idx)] = freq; } - void AddSuccFreq(uint64 freq, size_t pos = UINT32_MAX) { + + void AddSuccFreq(FreqType freq, size_t pos = UINT32_MAX) { ASSERT((pos <= succFreq.size() || pos == UINT32_MAX), "Invalid position."); if (pos == UINT32_MAX) { succFreq.push_back(freq); @@ -443,7 +445,6 @@ class BB : public BaseGraphNode { succFreq.insert(succFreq.begin() + pos, freq); } } - // update edge frequency void UpdateEdgeFreqs(bool updateSuccFreq = true); @@ -507,7 +508,7 @@ class BB : public BaseGraphNode { mePhiList.clear(); } - uint64 GetEdgeFreq(const BB *bb) const { + FreqType GetEdgeFreq(const BB *bb) const { auto iter = std::find(succ.begin(), succ.end(), bb); CHECK_FATAL(iter != std::end(succ), "%d is not the successor of %d", bb->UintID(), this->UintID()); CHECK_FATAL(succ.size() == succFreq.size(), "succfreq size doesn't match succ size"); @@ -515,13 +516,13 @@ class BB : public BaseGraphNode { return succFreq[idx]; } - uint64 GetEdgeFreq(size_t idx) const { + FreqType GetEdgeFreq(size_t idx) const { CHECK_FATAL(idx < succFreq.size(), "out of range in BB::GetEdgeFreq"); CHECK_FATAL(succ.size() == succFreq.size(), "succfreq size doesn't match succ size"); return succFreq[idx]; } - void SetEdgeFreq(const BB *bb, uint64 freq) { + void SetEdgeFreq(const BB *bb, FreqType freq) { auto iter = std::find(succ.begin(), succ.end(), bb); CHECK_FATAL(iter != std::end(succ), "%d is not the successor of %d", bb->UintID(), this->UintID()); CHECK_FATAL(succ.size() == succFreq.size(), "succfreq size %d doesn't match succ size %d", succFreq.size(), @@ -571,11 +572,11 @@ class BB : public BaseGraphNode { MapleVector pred; // predecessor list MapleVector succ; // successor list // record the edge freq from curBB to succ BB - MapleVector succFreq; + MapleVector succFreq; MapleMap phiList; MapleMap mePhiList; MapleMap> meVarPiList; - uint64 frequency = 0; + FreqType frequency = 0; BBKind kind = kBBUnknown; uint32 attributes = 0; diff --git a/src/mapleall/maple_me/include/me_function.h b/src/mapleall/maple_me/include/me_function.h index 33169261da8028613b68a25000c34a94cc35955e..4fe826e3f70040a22fd99e3d49165e2f7d722051 100644 --- a/src/mapleall/maple_me/include/me_function.h +++ b/src/mapleall/maple_me/include/me_function.h @@ -302,11 +302,11 @@ class MeFunction : public FuncEmit { profValid = val; } - uint64 GetFrequency() const { + FreqType GetFrequency() const { return frequency; } - void SetFrequency(uint64 f) { + void SetFrequency(FreqType f) { frequency = f; } @@ -399,7 +399,7 @@ class MeFunction : public FuncEmit { bool hasWriteInputAsmNode = false; /* set when ssa tab build */ bool profValid = false; IRProfileDesc *profileDesc = nullptr; - uint64 frequency = 0; + FreqType frequency = 0; // lfo bool isLfo = false; // during pme processing diff --git a/src/mapleall/maple_me/include/me_loop_unrolling.h b/src/mapleall/maple_me/include/me_loop_unrolling.h index d28667d9a8c74bb6788fed97b7dc449f1422da2c..46acd571fba2285f1ce8a53141fb0c8bb2f35816 100644 --- a/src/mapleall/maple_me/include/me_loop_unrolling.h +++ b/src/mapleall/maple_me/include/me_loop_unrolling.h @@ -80,7 +80,7 @@ class LoopUnrolling { MeExpr *CreateExprWithCRNode(CRNode &crNode); void InsertCondGotoBB(); void ResetFrequency(BB &bb); - void ResetFrequency(BB &newCondGotoBB, BB &exitingBB, const BB &exitedBB, uint64 headFreq); + void ResetFrequency(BB &newCondGotoBB, BB &exitingBB, const BB &exitedBB, FreqType headFreq); void ResetFrequency(); void ResetFrequency(const BB &curBB, const BB &succ, const BB &exitBB, BB &curCopyBB, bool copyAllLoop); BB *CopyBB(BB &bb, bool isInLoop); diff --git a/src/mapleall/maple_me/include/me_predict.h b/src/mapleall/maple_me/include/me_predict.h index 3a5640273d43511d6f456be7632cc56b00e70f63..422b6ccd3efe2d3b500fff372fbf9b5cf2fe40e6 100644 --- a/src/mapleall/maple_me/include/me_predict.h +++ b/src/mapleall/maple_me/include/me_predict.h @@ -45,7 +45,7 @@ struct Edge { BB &dest; Edge *next = nullptr; // the edge with the same src uint32 probability = 0; - uint32 frequency = 0; + FreqType frequency = 0; Edge(BB &bb1, BB &bb2) : src(bb1), dest(bb2) {} void Dump(bool dumpNext = false) const; }; diff --git a/src/mapleall/maple_me/include/me_profile_use.h b/src/mapleall/maple_me/include/me_profile_use.h index e5d1de857cbf3e41a4354bd2a4a4bccc99a87c62..78ff1e9a7d475edf9eb74a8ad3050cc360e740b7 100644 --- a/src/mapleall/maple_me/include/me_profile_use.h +++ b/src/mapleall/maple_me/include/me_profile_use.h @@ -26,12 +26,12 @@ class BBUseEdge : public BBEdge { : BBEdge(src, dest, w, isCritical, isFake) {} virtual ~BBUseEdge() = default; - void SetCount(uint64 value) { + void SetCount(FreqType value) { countValue = value; valid = true; } - uint64 GetCount() const { + FreqType GetCount() const { return countValue; } @@ -40,7 +40,7 @@ class BBUseEdge : public BBEdge { } private: bool valid = false; - uint64 countValue = 0; + FreqType countValue = 0; }; @@ -50,11 +50,11 @@ class BBUseInfo { : tmpAlloc(&tmpPool), inEdges(tmpAlloc.Adapter()), outEdges(tmpAlloc.Adapter()) {} virtual ~BBUseInfo() = default; - void SetCount(uint64 value) { + void SetCount(FreqType value) { countValue = value; valid = true; } - uint64 GetCount() const { + FreqType GetCount() const { return countValue; } @@ -118,7 +118,7 @@ class BBUseInfo { private: bool valid = false; - uint64 countValue = 0; + FreqType countValue = 0; uint32 unknownInEdges = 0; uint32 unknownOutEdges = 0; MapleAllocator tmpAlloc; @@ -141,8 +141,8 @@ class MeProfUse : public PGOInstrument { void CheckSumFail(const uint64 hash, const uint32 expectedCheckSum, const std::string &tag); private: bool IsAllZero(Profile::BBInfo &result) const; - void SetEdgeCount(BBUseEdge &edge, size_t value); - void SetEdgeCount(MapleVector &edges, uint64 value); + void SetEdgeCount(BBUseEdge &edge, FreqType value); + void SetEdgeCount(MapleVector &edges, FreqType value); void ComputeEdgeFreq(); void InitBBEdgeInfo(); void ComputeBBFreq(BBUseInfo &bbInfo, bool &changed); diff --git a/src/mapleall/maple_me/include/pme_emit.h b/src/mapleall/maple_me/include/pme_emit.h index 895f1f1256298e405be738387d44719e9cb4a4aa..8b87c3c4acca74b9fa21947e6d76a4031ecd7424 100644 --- a/src/mapleall/maple_me/include/pme_emit.h +++ b/src/mapleall/maple_me/include/pme_emit.h @@ -82,7 +82,7 @@ class PreMeEmitter : public AnalysisResult { MapleMap *GetPreMeExprExtensionMap() { return &preMeExprExtensionMap; } FuncProfInfo *GetFuncProfData() { return mirFunc->GetFuncProfData(); } void SetIpaInfo(CollectIpaInfo *info) { ipaInfo = info; } - void UpdateStmtInfo(const MeStmt &meStmt, StmtNode &stmt, BlockNode &currBlock, uint64 frequency); + void UpdateStmtInfo(const MeStmt &meStmt, StmtNode &stmt, BlockNode &currBlock, FreqType frequency); void UpdateStmtInfoForLabelNode(LabelNode &label, BB &bb); private: ArrayNode *ConvertToArray(BaseNode *x, TyIdx ptrTyIdx); diff --git a/src/mapleall/maple_me/src/bb.cpp b/src/mapleall/maple_me/src/bb.cpp index 3b254e332b7dc94a230e4a7ec2e3d53c656471a5..52f28e65f53fe00cce6a52642d14c5c81eb4d9a0 100644 --- a/src/mapleall/maple_me/src/bb.cpp +++ b/src/mapleall/maple_me/src/bb.cpp @@ -482,7 +482,7 @@ void BB::DumpMePhiList(const IRMap *irMap) { void BB::UpdateEdgeFreqs(bool updateBBFreqOfSucc) { int len = GetSucc().size(); ASSERT(len == GetSuccFreq().size(), "sanity check"); - int64_t succFreqs = 0; + FreqType succFreqs = 0; for (int i = 0; i < len; i++) { succFreqs += GetSuccFreq()[i]; } @@ -493,14 +493,14 @@ void BB::UpdateEdgeFreqs(bool updateBBFreqOfSucc) { return; } for (int i = 0; i < len; ++i) { - uint64 sfreq = GetSuccFreq()[i]; - uint64 scalefreq = (succFreqs == 0 ? (frequency / len) : (sfreq * frequency / succFreqs)); + FreqType sfreq = GetSuccFreq()[i]; + FreqType scalefreq = (succFreqs == 0 ? (frequency / len) : (sfreq * frequency / succFreqs)); SetSuccFreq(i, scalefreq); // update succ frequency with new difference if needed if (updateBBFreqOfSucc) { auto *succBBLoc = GetSucc(static_cast(static_cast(i))); - int64_t diffFreq = scalefreq - sfreq; - int64_t succBBnewFreq = static_cast(succBBLoc->GetFrequency()) + diffFreq; + FreqType diffFreq = scalefreq - sfreq; + FreqType succBBnewFreq = static_cast(succBBLoc->GetFrequency()) + diffFreq; if (succBBnewFreq >= 0) { succBBLoc->SetFrequency(static_cast(succBBnewFreq)); } diff --git a/src/mapleall/maple_me/src/dse.cpp b/src/mapleall/maple_me/src/dse.cpp index 9168dee6a11171935214d0aa145d26c7aa83c92a..899ae891e3ec7cc468c7d39892d51fb26d3b265e 100644 --- a/src/mapleall/maple_me/src/dse.cpp +++ b/src/mapleall/maple_me/src/dse.cpp @@ -234,7 +234,7 @@ void DSE::OnRemoveBranchStmt(BB &bb, const StmtNode &stmt) { } bb.SetKind(kBBFallthru); if (Options::profileUse && !bb.GetSuccFreq().empty()) { - int64_t succ0Freq = bb.GetSuccFreq()[0]; + FreqType succ0Freq = bb.GetSuccFreq()[0]; bb.GetSuccFreq().resize(1); bb.SetSuccFreq(0, bb.GetFrequency()); ASSERT(bb.GetFrequency() >= succ0Freq, "sanity check"); @@ -258,7 +258,7 @@ void DSE::OnRemoveBranchStmt(BB &bb, const StmtNode &stmt) { } // merge all frequency of condition goto bb to fallthru succ if (Options::profileUse && !bb.GetSuccFreq().empty()) { - int64_t succ0Freq = bb.GetSuccFreq()[0]; + FreqType succ0Freq = bb.GetSuccFreq()[0]; bb.GetSuccFreq().resize(1); bb.SetSuccFreq(0, bb.GetFrequency()); ASSERT(bb.GetFrequency() >= succ0Freq, "sanity check"); diff --git a/src/mapleall/maple_me/src/hdse.cpp b/src/mapleall/maple_me/src/hdse.cpp index 81a141a5e9a49770a1b9500243214247c36cb162..dd68e4600194ca47c8814f3fc8cb26c3d7a9af3c 100644 --- a/src/mapleall/maple_me/src/hdse.cpp +++ b/src/mapleall/maple_me/src/hdse.cpp @@ -250,7 +250,7 @@ void HDSE::RemoveNotRequiredStmtsInBB(BB &bb) { } bb.SetKind(kBBFallthru); if (UpdateFreq()) { - uint64 succ0Freq = bb.GetSuccFreq()[0]; + FreqType succ0Freq = bb.GetSuccFreq()[0]; bb.GetSuccFreq().resize(1); bb.SetSuccFreq(0, bb.GetFrequency()); ASSERT(bb.GetFrequency() >= succ0Freq, "sanity check"); @@ -280,7 +280,7 @@ void HDSE::RemoveNotRequiredStmtsInBB(BB &bb) { bool isPme = mirModule.CurFunction()->GetMeFunc()->GetPreMeFunc() != nullptr; if (mestmt->IsCondBr() && !isPme) { // see if foldable to unconditional branch CondGotoMeStmt *condbr = static_cast(mestmt); - uint64 removedFreq = 0; + FreqType removedFreq = 0; if (!mirModule.IsJavaModule() && condbr->GetOpnd()->GetMeOp() == kMeOpConst) { CHECK_FATAL(IsPrimitiveInteger(condbr->GetOpnd()->GetPrimType()), "MeHDSE::DseProcess: branch condition must be integer type"); diff --git a/src/mapleall/maple_me/src/lfo_loop_vec.cpp b/src/mapleall/maple_me/src/lfo_loop_vec.cpp index 17e548f7e6207dd649bda72427da8c04ed71942c..68fac5898a18c69d09f3130d43447c1a8ff9c468 100644 --- a/src/mapleall/maple_me/src/lfo_loop_vec.cpp +++ b/src/mapleall/maple_me/src/lfo_loop_vec.cpp @@ -29,10 +29,10 @@ void LoopVecInfo::UpdateDoloopProfData(MIRFunction *mirFunc, DoloopNode *doLoop, if (!profData) { return; } - int64_t doloopFreq = profData->GetStmtFreq(doLoop->GetStmtID()); - int64_t tempFreq; + FreqType doloopFreq = profData->GetStmtFreq(doLoop->GetStmtID()); + FreqType tempFreq; BlockNode *body = doLoop->GetDoBody(); - int64_t bodyFreq = profData->GetStmtFreq(body->GetStmtID()); + FreqType bodyFreq = profData->GetStmtFreq(body->GetStmtID()); if (isRemainder) { // update doLoop node in remainder tempFreq = (bodyFreq % vecLanes) + (doloopFreq - bodyFreq); diff --git a/src/mapleall/maple_me/src/me_bb_layout.cpp b/src/mapleall/maple_me/src/me_bb_layout.cpp index d5c65fb3f0f689984aa1309a5359d37b540cf405..51b00e9e4cc9a9215dc07696f13b4f64d3773b35 100644 --- a/src/mapleall/maple_me/src/me_bb_layout.cpp +++ b/src/mapleall/maple_me/src/me_bb_layout.cpp @@ -209,15 +209,15 @@ bool BBLayout::HasBetterLayoutPred(const BB &bb, BB &succ) { if (predList.size() <= 1) { return false; } - uint64 sumEdgeFreq = succ.GetFrequency(); + FreqType sumEdgeFreq = succ.GetFrequency(); const double hotEdgeFreqPercent = 0.8; // should further fine tuning - uint64 hotEdgeFreq = static_cast(sumEdgeFreq * hotEdgeFreqPercent); + FreqType hotEdgeFreq = static_cast(sumEdgeFreq * hotEdgeFreqPercent); // if edge freq(bb->succ) contribute more than 80% to succ block freq, no better layout pred than bb for (uint32 i = 0; i < predList.size(); ++i) { if (predList[i] == &bb) { continue; } - uint64 edgeFreq = predList[i]->GetEdgeFreq(&succ); + FreqType edgeFreq = predList[i]->GetEdgeFreq(&succ); if (edgeFreq > (sumEdgeFreq - hotEdgeFreq)) { return true; } @@ -231,7 +231,7 @@ BB *BBLayout::GetBestSucc(BB &bb, const BBChain &chain, const MapleVector bool considerBetterPredForSucc) { // (1) search in succ CHECK_FATAL(bb2chain[bb.GetBBId()] == &chain, "bb2chain mis-match"); - uint32 bestEdgeFreq = 0; + FreqType bestEdgeFreq = 0; BB *bestSucc = nullptr; for (uint32 i = 0; i < bb.GetSucc().size(); ++i) { BB *succ = bb.GetSucc(i); @@ -241,7 +241,7 @@ BB *BBLayout::GetBestSucc(BB &bb, const BBChain &chain, const MapleVector if (considerBetterPredForSucc && HasBetterLayoutPred(bb, *succ)) { continue; } - uint32 currEdgeFreq = static_cast(bb.GetEdgeFreq(i)); // attention: entryBB->succFreq[i] is always 0 + FreqType currEdgeFreq = bb.GetEdgeFreq(i); // attention: entryBB->succFreq[i] is always 0 if (bb.GetBBId() == 0) { // special case for common entry BB CHECK_FATAL(bb.GetSucc().size() == 1, "common entry BB should not have more than 1 succ"); bestSucc = succ; @@ -261,7 +261,7 @@ BB *BBLayout::GetBestSucc(BB &bb, const BBChain &chain, const MapleVector } // (2) search in readyToLayoutChains - uint64 bestFreq = 0; + FreqType bestFreq = 0; for (auto it = readyToLayoutChains.begin(); it != readyToLayoutChains.end(); ++it) { BBChain *readyChain = *it; BB *header = readyChain->GetHeader(); @@ -275,9 +275,9 @@ BB *BBLayout::GetBestSucc(BB &bb, const BBChain &chain, const MapleVector bestSucc = header; } } else { // use edge freq - uint32 subBestFreq = 0; + FreqType subBestFreq = 0; for (auto *pred : header->GetPred()) { - uint32 curFreq = static_cast(pred->GetEdgeFreq(header)); + FreqType curFreq = pred->GetEdgeFreq(header); if (curFreq > subBestFreq) { subBestFreq = curFreq; } @@ -371,8 +371,8 @@ bool BBLayout::ChooseTargetAsFallthru(const BB &bb, const BB &targetBB, const BB return false; } if (profValid) { - uint64 freqToTargetBB = bb.GetEdgeFreq(&targetBB); - uint64 freqToFallthru = bb.GetEdgeFreq(&fallThru); + FreqType freqToTargetBB = bb.GetEdgeFreq(&targetBB); + FreqType freqToFallthru = bb.GetEdgeFreq(&fallThru); if (enabledDebug) { LogInfo::MapleLogger() << func.GetName() << " " << bb.GetBBId() << "->" << targetBB.GetBBId() << " freq " << freqToTargetBB << " " << bb.GetBBId() << "->" << fallThru.GetBBId() << " freq " @@ -1203,7 +1203,7 @@ void BBLayout::BuildEdges() { auto *bb = *bIt; for (size_t i = 0; i < bb->GetSucc().size(); ++i) { BB *dest = bb->GetSucc(i); - uint64 w = bb->GetEdgeFreq(i); + FreqType w = bb->GetEdgeFreq(i); allEdges.emplace_back(layoutAlloc.GetMemPool()->New(bb, dest, w)); } } @@ -1255,13 +1255,13 @@ BB *BBLayout::NextBBProf(BB &bb) { return NextBBProf(*succBB); } // max freq intial - uint64 maxFreq = 0; + FreqType maxFreq = 0; size_t idx = 0; bool found = false; for (size_t i = 0; i < bb.GetSucc().size(); ++i) { BB *succBB = bb.GetSucc(i); if (!laidOut[succBB->GetBBId()]) { - uint64 edgeFreqFromBB = bb.GetEdgeFreq(i); + FreqType edgeFreqFromBB = bb.GetEdgeFreq(i); // if bb isn't executed, choose the first valid bb if (bb.GetFrequency() == 0) { idx = i; diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index b2e02eeaa2ead7a9b4699aebcad8269298044f6c..755156f58926c4f3a6613cea28f0daa70d864e83 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1558,9 +1558,9 @@ void MeCFG::CreateBasicBlocks() { curBB->SetBBLabel(labelIdx); // label node is not real node in bb, get frequency information to bb if (Options::profileUse && func.GetMirFunc()->GetFuncProfData()) { - uint64 freq = func.GetMirFunc()->GetFuncProfData()->GetStmtFreq(stmt->GetStmtID()); + FreqType freq = func.GetMirFunc()->GetFuncProfData()->GetStmtFreq(stmt->GetStmtID()); if (freq >= 0) { - curBB->SetFrequency(static_cast(freq)); + curBB->SetFrequency(freq); } } break; @@ -1874,7 +1874,7 @@ inline void ConstructEdgeFreqForBBWith2Succs(BB &bb) { BB *fallthru = bb.GetSucc(0); BB *targetBB = bb.GetSucc(1); if (fallthru->GetPred().size() == 1) { - uint64 succ0Freq = fallthru->GetFrequency(); + FreqType succ0Freq = fallthru->GetFrequency(); bb.PushBackSuccFreq(succ0Freq); if (bb.GetFrequency() > succ0Freq) { bb.PushBackSuccFreq(bb.GetFrequency() - succ0Freq); @@ -1882,7 +1882,7 @@ inline void ConstructEdgeFreqForBBWith2Succs(BB &bb) { bb.PushBackSuccFreq(0); } } else if (targetBB->GetPred().size() == 1) { - uint64 succ1Freq = targetBB->GetFrequency(); + FreqType succ1Freq = targetBB->GetFrequency(); if (bb.GetAttributes(kBBAttrWontExit) && bb.GetSuccFreq().size() == 1) { // special case: WontExitAnalysis() has pushed 0 to bb->succFreq bb.GetSuccFreq()[0] = bb.GetFrequency(); @@ -1961,7 +1961,7 @@ void MeCFG::ConstructBBFreqFromStmtFreq() { } // add common entry and common exit auto *bb = *common_entry(); - uint64_t freq = 0; + FreqType freq = 0; for (size_t i = 0; i < bb->GetSucc().size(); ++i) { freq += bb->GetSucc(i)->GetFrequency(); } @@ -2022,11 +2022,11 @@ void MeCFG::UpdateEdgeFreqWithBBFreq() { } // collect pred total except entry if (!bb->GetAttributes(kBBAttrIsEntry)) { - int64_t inputFreq = 0; + FreqType inputFreq = 0; for (auto *pred : bb->GetPred()) { int idx = pred->GetSuccIndex(*bb); ASSERT(idx >= 0 && idx < pred->GetSuccFreq().size(), "sanity check"); - inputFreq += static_cast(pred->GetSuccFreq()[static_cast(idx)]); + inputFreq += pred->GetSuccFreq()[static_cast(idx)]; } bb->SetFrequency(static_cast(inputFreq)); } @@ -2053,7 +2053,7 @@ void MeCFG::ClearFuncFreqInfo() { // return value is 0 means pass verification, else has problem int MeCFG::VerifyBBFreq(bool checkFatal) { - int64_t entryFreq = static_cast(func.GetMirFunc()->GetFuncProfData()->GetFuncFrequency()); + FreqType entryFreq = func.GetMirFunc()->GetFuncProfData()->GetFuncFrequency(); ASSERT(entryFreq >= 0, "sanity check"); bool entryIsZero = entryFreq == 0 ? true : false; for (size_t i = 2; i < bbVec.size(); ++i) { // skip common entry and common exit @@ -2083,14 +2083,14 @@ int MeCFG::VerifyBBFreq(bool checkFatal) { } } // check case 3: bb freq == sum(out edge freq) - uint64 succSumFreq = 0; + FreqType succSumFreq = 0; for (auto succFreq : bb->GetSuccFreq()) { succSumFreq += succFreq; } if (succSumFreq == bb->GetFrequency()) { continue; } - int64 diff = static_cast(succSumFreq - bb->GetFrequency()); + FreqType diff = succSumFreq - bb->GetFrequency(); diff = diff >= 0 ? diff : -diff; if (diff > 1) { if (checkFatal) { diff --git a/src/mapleall/maple_me/src/me_critical_edge.cpp b/src/mapleall/maple_me/src/me_critical_edge.cpp index dc52c67bc2e4e60aa27f8800ab94fb3fa6e19b87..534fad833083355d829f03f550bbb155ac47308a 100644 --- a/src/mapleall/maple_me/src/me_critical_edge.cpp +++ b/src/mapleall/maple_me/src/me_critical_edge.cpp @@ -165,7 +165,7 @@ void MeSplitCEdge::BreakCriticalEdge(MeFunction &func, BB &pred, BB &succ) const if (cfg->UpdateCFGFreq() && (!(func.IsPme() || func.IsLfo()))) { int idx = pred.GetSuccIndex(*newBB); ASSERT(idx >= 0 && idx < pred.GetSucc().size(), "sanity check"); - uint64_t freq = pred.GetEdgeFreq(idx); + FreqType freq = pred.GetEdgeFreq(idx); newBB->SetFrequency(freq); newBB->PushBackSuccFreq(freq); } diff --git a/src/mapleall/maple_me/src/me_jump_threading.cpp b/src/mapleall/maple_me/src/me_jump_threading.cpp index 7233560d44e6d0c21d4db6a56c366edd67b297b6..0d76bfffc733c4f1c6d9f3811b2d408225ba2e01 100644 --- a/src/mapleall/maple_me/src/me_jump_threading.cpp +++ b/src/mapleall/maple_me/src/me_jump_threading.cpp @@ -168,9 +168,9 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorAddSucc(*newSuccBB); if (func.GetCfg()->UpdateCFGFreq()) { - uint64 freqUsed = old2NewBB[idxOfCurrBB].second->GetFrequency(); + FreqType freqUsed = old2NewBB[idxOfCurrBB].second->GetFrequency(); old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(freqUsed); - newSuccBB->SetFrequency(static_cast(freqUsed)); + newSuccBB->SetFrequency(freqUsed); } break; } diff --git a/src/mapleall/maple_me/src/me_loop_canon.cpp b/src/mapleall/maple_me/src/me_loop_canon.cpp index fa6502b41eab2f251e3995ebb415585b173dcb21..41b6645440aa3ca8b60a357dac41f76ae4a8801b 100644 --- a/src/mapleall/maple_me/src/me_loop_canon.cpp +++ b/src/mapleall/maple_me/src/me_loop_canon.cpp @@ -100,7 +100,7 @@ void MeLoopCanon::SplitPreds(const std::vector &splitList, BB *splittedBB, if (updateFreqs) { int idx = pred->GetSuccIndex(*splittedBB); ASSERT(idx >= 0 && idx < pred->GetSucc().size(), "sanity check"); - uint64_t freq = pred->GetEdgeFreq(static_cast(idx)); + FreqType freq = pred->GetEdgeFreq(static_cast(idx)); mergedBB->SetFrequency(freq); mergedBB->PushBackSuccFreq(freq); } @@ -123,7 +123,7 @@ void MeLoopCanon::SplitPreds(const std::vector &splitList, BB *splittedBB, mergedBB->GetMePhiList().emplace(latchLhs->GetOstIdx(), latchPhi); phiIter.second->GetOpnds().push_back(latchLhs); } - uint64_t freq = 0; + FreqType freq = 0; for (BB *pred : splitList) { auto pos = splittedBB->GetPredIndex(*pred); for (auto &phiIter : std::as_const(mergedBB->GetMePhiList())) { @@ -268,7 +268,7 @@ void MeLoopCanon::InsertExitBB(LoopDesc &loop) { BB *newExitBB = cfg->NewBasicBlock(); newExitBB->SetKind(kBBFallthru); auto pos = succ->GetPredIndex(*curBB); - uint64_t freq = 0; + FreqType freq = 0; if (updateFreqs) { int idx = curBB->GetSuccIndex(*succ); ASSERT(idx >= 0 && idx < curBB->GetSuccFreq().size(), "sanity check"); diff --git a/src/mapleall/maple_me/src/me_loop_inversion.cpp b/src/mapleall/maple_me/src/me_loop_inversion.cpp index 7a615caf7753db991caeff7b479440625913604a..0f3a3ad65cb7a2e1e70d5ef455ca236a63b4fe77 100644 --- a/src/mapleall/maple_me/src/me_loop_inversion.cpp +++ b/src/mapleall/maple_me/src/me_loop_inversion.cpp @@ -138,7 +138,7 @@ void MeLoopInversion::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapUpdateCFGFreq()) { int idx = pred.GetSuccIndex(bb); ASSERT(idx >= 0 && idx < pred.GetSucc().size(), "sanity check"); - uint64_t freq = pred.GetEdgeFreq(idx); + FreqType freq = pred.GetEdgeFreq(idx); latchBB->SetFrequency(freq); // update bb frequency: remove pred frequency since pred is deleted ASSERT(bb.GetFrequency() >= freq, "sanity check"); @@ -247,7 +247,7 @@ void MeLoopInversion::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapPushBackSuccFreq(0); } } else if (latchBB->GetKind() == kBBFallthru || latchBB->GetKind() == kBBGoto) { - int64_t newsuccFreq = (latchBB->GetFrequency() == 0) ? 0 : latchBB->GetSuccFreq()[0] - 1; + FreqType newsuccFreq = (latchBB->GetFrequency() == 0) ? 0 : latchBB->GetSuccFreq()[0] - 1; latchBB->SetSuccFreq(0, newsuccFreq); // loop is changed to do-while format bb.SetSuccFreq(0, bb.GetFrequency()); } else { diff --git a/src/mapleall/maple_me/src/me_loop_unrolling.cpp b/src/mapleall/maple_me/src/me_loop_unrolling.cpp index bb2f79e8539dc9b6f44adfbad2904dedb85d79a2..6cbfad845b045bb4fcf732dc7d04557c1cb769ba 100644 --- a/src/mapleall/maple_me/src/me_loop_unrolling.cpp +++ b/src/mapleall/maple_me/src/me_loop_unrolling.cpp @@ -309,7 +309,7 @@ void LoopUnrolling::CopyLoopBody(BB &newHeadBB, std::unordered_map &ol // Update frequency of old BB. void LoopUnrolling::ResetFrequency(BB &bb) { - uint64 freq = bb.GetFrequency() / replicatedLoopNum; + FreqType freq = bb.GetFrequency() / replicatedLoopNum; if ((!instrumentProf) && freq == 0 && partialCount == 0 && bb.GetFrequency() != 0) { freq = 1; } @@ -328,13 +328,13 @@ void LoopUnrolling::ResetFrequency() { auto exitBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); auto latchBB = loop->latch; if (isUnrollWithVar) { - uint64 latchFreq = loop->head->GetFrequency() % replicatedLoopNum - loop->preheader->GetFrequency(); + FreqType latchFreq = loop->head->GetFrequency() % replicatedLoopNum - loop->preheader->GetFrequency(); exitBB->SetFrequency(loop->head->GetFrequency() % replicatedLoopNum - latchFreq); exitBB->SetEdgeFreq(latchBB, latchFreq); latchBB->SetFrequency(latchFreq); latchBB->SetEdgeFreq(loop->head, latchFreq); } else { - uint64 exitFreq = exitBB->GetFrequency() / replicatedLoopNum; + FreqType exitFreq = exitBB->GetFrequency() / replicatedLoopNum; if (exitFreq == 0 && exitBB->GetFrequency() != 0) { exitFreq = 1; } @@ -538,7 +538,7 @@ LoopUnrolling::ReturnKindOfFullyUnroll LoopUnrolling::LoopFullyUnroll(int64 trip return kCanFullyUnroll; } -void LoopUnrolling::ResetFrequency(BB &newCondGotoBB, BB &exitingBB, const BB &exitedBB, uint64 headFreq) { +void LoopUnrolling::ResetFrequency(BB &newCondGotoBB, BB &exitingBB, const BB &exitedBB, FreqType headFreq) { if (profValid) { newCondGotoBB.SetFrequency(headFreq); exitingBB.SetEdgeFreq(&exitedBB, 0); @@ -550,7 +550,7 @@ void LoopUnrolling::InsertCondGotoBB() { BB *exitingBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); BB *exitedBB = *(loop->inloopBB2exitBBs.cbegin()->second->cbegin()); BB *newCondGotoBB = CopyBB(*exitingBB, true); - uint64 headFreq = loop->head->GetFrequency(); + FreqType headFreq = loop->head->GetFrequency(); ResetFrequency(*newCondGotoBB, *exitingBB, *exitedBB, headFreq); MeStmt *lastMeStmt = newCondGotoBB->GetLastMe(); CHECK_FATAL(lastMeStmt != nullptr, "last meStmt must not be nullptr"); @@ -632,7 +632,7 @@ void LoopUnrolling::AddPreHeader(BB *oldPreHeader, BB *head) { auto *preheader = cfg->NewBasicBlock(); preheader->SetAttributes(kBBAttrArtificial); preheader->SetKind(kBBFallthru); - uint64 preheaderFreq = 0; + FreqType preheaderFreq = 0; if (profValid) { preheaderFreq = oldPreHeader->GetEdgeFreq(head); } diff --git a/src/mapleall/maple_me/src/me_predict.cpp b/src/mapleall/maple_me/src/me_predict.cpp index 4afb1392ff6ffd51cba86749581fe032f8797da7..f3115a884cfd663693998c8696cf65e6d24445c1 100644 --- a/src/mapleall/maple_me/src/me_predict.cpp +++ b/src/mapleall/maple_me/src/me_predict.cpp @@ -741,7 +741,7 @@ bool MePrediction::DoPropFreq(const BB *head, std::vector *headers, BB &bb) return false; } } - uint64 freq = 0; + FreqType freq = 0; double cyclicProb = 0; for (BB *pred : bb.GetPred()) { Edge *edge = FindEdge(*pred, bb); @@ -1016,7 +1016,7 @@ void MePrediction::VerifyFreq(const MeFunction &meFunc) { continue; } // bb freq == sum(out edge freq) - uint64 succSumFreq = 0; + FreqType succSumFreq = 0; for (auto succFreq : bb->GetSuccFreq()) { succSumFreq += succFreq; } diff --git a/src/mapleall/maple_me/src/me_profile_use.cpp b/src/mapleall/maple_me/src/me_profile_use.cpp index d22d98e97a47a28e6ce2ba337cf26c4407cdc9ed..e149d1845ba19d71c6ff3815175a7a7f79492239 100644 --- a/src/mapleall/maple_me/src/me_profile_use.cpp +++ b/src/mapleall/maple_me/src/me_profile_use.cpp @@ -80,7 +80,7 @@ void MeProfUse::InitBBEdgeInfo() { // If all input edges or output edges determined, caculate BB freq void MeProfUse::ComputeBBFreq(BBUseInfo &bbInfo, bool &changed) { - uint64 count = 0; + FreqType count = 0; if (!bbInfo.GetStatus()) { if (bbInfo.GetUnknownOutEdges() == 0) { count = SumEdgesCount(bbInfo.GetOutEdges()); @@ -149,7 +149,7 @@ void MeProfUse::ComputeEdgeFreq() { * this used to set the edge count for the unknown edge * ensure only one unkown edge in the edges */ -void MeProfUse::SetEdgeCount(MapleVector &edges, uint64 value) { +void MeProfUse::SetEdgeCount(MapleVector &edges, FreqType value) { for (const auto &e : edges) { if (!e->GetStatus()) { e->SetCount(value); @@ -163,7 +163,7 @@ void MeProfUse::SetEdgeCount(MapleVector &edges, uint64 value) { CHECK(false, "can't find unkown edge"); } -void MeProfUse::SetEdgeCount(BBUseEdge &edge, size_t value) { +void MeProfUse::SetEdgeCount(BBUseEdge &edge, FreqType value) { // edge counter already valid skip if (edge.GetStatus()) { return; diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index e1271928d477db0513487708cad90c70d902c4f4..f1f9a26b1ceb6b971a9987c25f33fba9fdf34e9a 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3845,7 +3845,7 @@ bool ValueRangePropagation::ChangeTheSuccOfPred2TrueBranch( if (exitCopyFallthru != nullptr) { PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); size_t index = FindBBInSuccs(pred, bb); - uint64 edgeFreq = 0; + FreqType edgeFreq = 0; if (func.GetCfg()->UpdateCFGFreq()) { edgeFreq = pred.GetSuccFreq()[index]; } @@ -3894,7 +3894,7 @@ bool ValueRangePropagation::ChangeTheSuccOfPred2TrueBranch( mergeAllFallthruBBs->AddMeStmtLast(gotoMeStmt); PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); size_t index = FindBBInSuccs(pred, bb); - uint64 edgeFreq = 0; + FreqType edgeFreq = 0; if (func.GetCfg()->UpdateCFGFreq()) { edgeFreq = pred.GetSuccFreq()[index]; } @@ -3978,7 +3978,7 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( if (OnlyHaveCondGotoStmt(bb)) { PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); size_t index = FindBBInSuccs(pred, bb); - uint64 edgeFreq = 0; + FreqType edgeFreq = 0; if (func.GetCfg()->UpdateCFGFreq()) { edgeFreq = pred.GetSuccFreq()[index]; } @@ -4002,7 +4002,7 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( if (exitCopyFallthru != nullptr) { PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); size_t index = FindBBInSuccs(pred, bb); - uint64 edgeFreq = 0; + FreqType edgeFreq = 0; if (func.GetCfg()->UpdateCFGFreq()) { edgeFreq = pred.GetSuccFreq()[index]; ASSERT(bb.GetFrequency() >= edgeFreq, "sanity check"); @@ -4015,14 +4015,14 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( if (bb.GetFrequency() > edgeFreq) { bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); exitCopyFallthru->SetFrequency(static_cast(edgeFreq)); - exitCopyFallthru->PushBackSuccFreq(static_cast(edgeFreq)); + exitCopyFallthru->PushBackSuccFreq(edgeFreq); size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); - int64_t updatedtrueFreq = static_cast( - bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); + FreqType updatedtrueFreq = static_cast( + bb.GetSuccFreq()[trueBranchIdx] - edgeFreq); ASSERT(updatedtrueFreq >= 0, "sanity check"); - bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + bb.SetSuccFreq(static_cast(trueBranchIdx), updatedtrueFreq); } - pred.AddSuccFreq(static_cast(edgeFreq), index); + pred.AddSuccFreq(edgeFreq, index); } return true; } @@ -4037,7 +4037,7 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( newBB->AddMeStmtLast(gotoMeStmt); PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); size_t index = FindBBInSuccs(pred, bb); - uint64 edgeFreq = 0; + FreqType edgeFreq = 0; if (func.GetCfg()->UpdateCFGFreq()) { edgeFreq = pred.GetSuccFreq()[index]; } @@ -4050,8 +4050,8 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( bb.UpdateEdgeFreqs(); } newBB->SetFrequency(static_cast(edgeFreq)); - newBB->PushBackSuccFreq(static_cast(edgeFreq)); - pred.AddSuccFreq(static_cast(edgeFreq), index); + newBB->PushBackSuccFreq(edgeFreq); + pred.AddSuccFreq(edgeFreq, index); } DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); (void)func.GetOrCreateBBLabel(trueBranch); diff --git a/src/mapleall/maple_me/src/optimizeCFG.cpp b/src/mapleall/maple_me/src/optimizeCFG.cpp index 15742bf8ca00920c8f0e1b1b4c62973aa521c273..0c3dc910ed9bdfc1700a3260bdf0a681b04efe1e 100644 --- a/src/mapleall/maple_me/src/optimizeCFG.cpp +++ b/src/mapleall/maple_me/src/optimizeCFG.cpp @@ -1047,7 +1047,7 @@ bool OptimizeBB::OptimizeCondBB2UnCond() { // case 1 BB *ftBB = currBB->GetSucc(0); BB *gtBB = currBB->GetSucc(1); - uint64_t removedSuccFreq = 0; + FreqType removedSuccFreq = 0; if (isMeIR) { MeStmt *brStmt = currBB->GetLastMe(); ASSERT_NOT_NULL(brStmt); @@ -1604,7 +1604,7 @@ bool OptimizeBB::FoldCondBranch() { succBB->RemoveLastMeStmt(); succBB->SetKind(kBBFallthru); if (cfg->UpdateCFGFreq()) { - uint64 freqToMove = succBB->GetSuccFreq()[1]; + FreqType freqToMove = succBB->GetSuccFreq()[1]; currBB->SetSuccFreq(0, currBB->GetSuccFreq()[0] - freqToMove); succBB->SetFrequency(succBB->GetFrequency() - freqToMove); currBB->SetSuccFreq(1, currBB->GetSuccFreq()[1] + freqToMove); @@ -1864,7 +1864,7 @@ bool OptimizeBB::SkipRedundantCond(BB &pred, BB &succ) { DEBUG_LOG() << "SkipRedundantCond : Remove condBr in BB" << LOG_BBID(&succ) << ", turn it to fallthruBB\n"; } BB *rmBB = (FindFirstRealSucc(succ.GetSucc(0)) == newTarget) ? succ.GetSucc(1) : succ.GetSucc(0); - uint64 deletedSuccFreq = 0; + FreqType deletedSuccFreq = 0; if (cfg->UpdateCFGFreq()) { int idx = succ.GetSuccIndex(*rmBB); deletedSuccFreq = succ.GetSuccFreq()[static_cast(idx)]; @@ -1928,19 +1928,19 @@ bool OptimizeBB::SkipRedundantCond(BB &pred, BB &succ) { if (cfg->UpdateCFGFreq()) { int idx = pred.GetSuccIndex(*newBB); ASSERT(idx >= 0 && idx < pred.GetSucc().size(), "sanity check"); - uint64 freq = pred.GetEdgeFreq(idx); + FreqType freq = pred.GetEdgeFreq(idx); newBB->SetFrequency(freq); newBB->PushBackSuccFreq(freq); // update frequency of succ because one of its pred is removed // frequency of - uint64 freqOfSucc = succ.GetFrequency(); + FreqType freqOfSucc = succ.GetFrequency(); ASSERT(freqOfSucc >= freq, "sanity check"); succ.SetFrequency(freqOfSucc - freq); // update edge frequency BB *affectedBB = (tfBranch == kBrTrue) ? stfSucc.first : stfSucc.second; idx = succ.GetSuccIndex(*affectedBB); ASSERT(idx >= 0 && idx < succ.GetSucc().size(), "sanity check"); - uint64_t oldedgeFreq = succ.GetSuccFreq()[static_cast(idx)]; + FreqType oldedgeFreq = succ.GetSuccFreq()[static_cast(idx)]; if (oldedgeFreq >= freq) { succ.SetSuccFreq(idx, oldedgeFreq - freq); } else { @@ -2129,7 +2129,7 @@ bool OptimizeBB::MergeGotoBBToPred(BB *succ, BB *pred) { } int predIdx = succ->GetPredIndex(*pred); bool needUpdatePhi = false; - uint64 removedFreq = 0; + FreqType removedFreq = 0; if (cfg->UpdateCFGFreq()) { int idx = pred->GetSuccIndex(*succ); removedFreq = pred->GetSuccFreq()[static_cast(idx)]; @@ -2145,7 +2145,7 @@ bool OptimizeBB::MergeGotoBBToPred(BB *succ, BB *pred) { needUpdatePhi = true; } if (cfg->UpdateCFGFreq()) { - uint64 succFreq = succ->GetFrequency(); + FreqType succFreq = succ->GetFrequency(); if (succFreq >= removedFreq) { succ->SetFrequency(succFreq - removedFreq); succ->SetSuccFreq(0, succ->GetFrequency()); diff --git a/src/mapleall/maple_me/src/pme_emit.cpp b/src/mapleall/maple_me/src/pme_emit.cpp index cab8aab318d120a2597fc3c60a3e6b2329c67678..043383452849084d7035216ad0416f21578817fd 100644 --- a/src/mapleall/maple_me/src/pme_emit.cpp +++ b/src/mapleall/maple_me/src/pme_emit.cpp @@ -772,7 +772,7 @@ void PreMeEmitter::UpdateStmtInfoForLabelNode(LabelNode &label, BB &bb) { label.SetStmtInfoId(ipaInfo->GetRealFirstStmtInfoId(bb)); } -void PreMeEmitter::UpdateStmtInfo(const MeStmt &meStmt, StmtNode &stmt, BlockNode &currBlock, uint64 frequency) { +void PreMeEmitter::UpdateStmtInfo(const MeStmt &meStmt, StmtNode &stmt, BlockNode &currBlock, FreqType frequency) { if (ipaInfo == nullptr || meStmt.GetStmtInfoId() == kInvalidIndex) { return; } @@ -785,8 +785,6 @@ void PreMeEmitter::UpdateStmtInfo(const MeStmt &meStmt, StmtNode &stmt, BlockNod void PreMeEmitter::EmitBB(BB *bb, BlockNode *curBlk) { CHECK_FATAL(curBlk != nullptr, "null ptr check"); - bool setFirstFreq = (GetFuncProfData() != nullptr); - bool setLastFreq = false; bool bbIsEmpty = bb->GetMeStmts().empty(); // emit head. label LabelIdx labidx = bb->GetBBLabel(); @@ -812,12 +810,7 @@ void PreMeEmitter::EmitBB(BB *bb, BlockNode *curBlk) { curBlk->AddStatement(stmt); // add for first stmt in bb in curblk if (GetFuncProfData() != nullptr) { - if (setFirstFreq || (stmt->GetOpCode() == OP_call) || IsCallAssigned(stmt->GetOpCode())) { - GetFuncProfData()->SetStmtFreq(stmt->GetStmtID(), bb->GetFrequency()); - setFirstFreq = false; - } else { - setLastFreq = true; - } + GetFuncProfData()->SetStmtFreq(stmt->GetStmtID(), bb->GetFrequency()); } } if (bb->GetAttributes(kBBAttrIsTryEnd)) { @@ -826,13 +819,10 @@ void PreMeEmitter::EmitBB(BB *bb, BlockNode *curBlk) { curBlk->AddStatement(endtry); PreMeMIRExtension *pmeExt = preMeMP->New(curBlk); preMeStmtExtensionMap[endtry->GetStmtID()] = pmeExt; - setLastFreq = true; } // add stmtnode to last if (GetFuncProfData()) { - if (setLastFreq) { - GetFuncProfData()->SetStmtFreq(curBlk->GetLast()->GetStmtID(), bb->GetFrequency()); - } else if (bbIsEmpty) { + if (bbIsEmpty) { if (!MeOption::quiet) { LogInfo::MapleLogger() << " bb " << bb->GetBBId() << ": no stmt used to add frequency; added comment node\n"; } @@ -951,7 +941,7 @@ uint32 PreMeEmitter::Raise2PreMeWhile(uint32 curJ, BlockNode *curBlk) { } // set dobody freq if (GetFuncProfData()) { - int64_t freq = (endlblbb == suc0) ? suc1->GetFrequency() : suc0->GetFrequency(); + FreqType freq = (endlblbb == suc0) ? suc1->GetFrequency() : suc0->GetFrequency(); GetFuncProfData()->SetStmtFreq(dobody->GetStmtID(), static_cast(freq)); } return curJ; @@ -1077,8 +1067,8 @@ uint32 PreMeEmitter::Raise2PreMeIf(uint32 curJ, BlockNode *curBlk) { CHECK_FATAL(j < bbvec.size(), ""); if (GetFuncProfData()) { // set then part/else part frequency - uint64 ifFreq = GetFuncProfData()->GetStmtFreq(ifStmtNode->GetStmtID()); - uint64 branchFreq = bbvec[curJ + 1]->GetFrequency(); + FreqType ifFreq = GetFuncProfData()->GetStmtFreq(ifStmtNode->GetStmtID()); + FreqType branchFreq = bbvec[curJ + 1]->GetFrequency(); GetFuncProfData()->SetStmtFreq(branchBlock->GetStmtID(), branchFreq); GetFuncProfData()->SetStmtFreq(emptyBlock->GetStmtID(), ifFreq - branchFreq); } diff --git a/src/mapleall/maple_me/src/pme_mir_lower.cpp b/src/mapleall/maple_me/src/pme_mir_lower.cpp index f79c32534fb63c0d845f8a4df7921ed2bc8910ef..4780644d79e332c38f8515e2c9643ccc6592eee1 100644 --- a/src/mapleall/maple_me/src/pme_mir_lower.cpp +++ b/src/mapleall/maple_me/src/pme_mir_lower.cpp @@ -51,10 +51,11 @@ BlockNode *PreMeMIRLower::LowerWhileStmt(WhileStmtNode &whileStmt) { CHECK_FATAL(whileStmt.GetBody(), "null ptr check"); blk->AppendStatementsFromBlock(*whileStmt.GetBody()); GotoNode *whilegotonode = mirbuilder->CreateStmtGoto(OP_goto, whilelblidx); - if (GetFuncProfData() && blk->GetLast()) { - ASSERT(GetFuncProfData()->GetStmtFreq(blk->GetLast()->GetStmtID()) >= 0, - "last stmt of while body should has freq"); - GetFuncProfData()->CopyStmtFreq(whilegotonode->GetStmtID(), blk->GetLast()->GetStmtID()); + if (GetFuncProfData()) { + // The last stmt in body is not reliable as it could be an end stmt of an inner loop + ASSERT(GetFuncProfData()->GetStmtFreq(whileStmt.GetBody()->GetStmtID()) >= 0, + "while body should have freq set"); + GetFuncProfData()->CopyStmtFreq(whilegotonode->GetStmtID(), whileStmt.GetBody()->GetStmtID()); } blk->AddStatement(whilegotonode); // create endlabel @@ -71,10 +72,8 @@ BlockNode *PreMeMIRLower::LowerWhileStmt(WhileStmtNode &whileStmt) { endlblstmt->SetSrcPos(pos); blk->AddStatement(endlblstmt); if (GetFuncProfData()) { - CHECK_FATAL(GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) >= - GetFuncProfData()->GetStmtFreq(whilegotonode->GetStmtID()), "inconsistent profiling on while stmt"); - int64_t freq = GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) - - GetFuncProfData()->GetStmtFreq(whilegotonode->GetStmtID()); + CHECK_FATAL(GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) >= 0, "while stmt's freq is not set"); + FreqType freq = GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()); GetFuncProfData()->SetStmtFreq(endlblstmt->GetStmtID(), freq); } return blk; @@ -155,8 +154,8 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { // set stmtfreqs if (GetFuncProfData()) { ASSERT(GetFuncProfData()->GetStmtFreq(ifstmt.GetThenPart()->GetStmtID()) >= 0, "sanity check"); - int64_t freq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()) - - GetFuncProfData()->GetStmtFreq(ifstmt.GetThenPart()->GetStmtID()); + FreqType freq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()) - + GetFuncProfData()->GetStmtFreq(ifstmt.GetThenPart()->GetStmtID()); GetFuncProfData()->SetStmtFreq(labstmt->GetStmtID(), freq); } } else if (thenempty && !Options::profileUse && !Options::profileGen) { @@ -192,8 +191,8 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { // set stmtfreqs if (GetFuncProfData()) { ASSERT(GetFuncProfData()->GetStmtFreq(ifstmt.GetElsePart()->GetStmtID()) > 0, "sanity check"); - int64_t freq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()) - - GetFuncProfData()->GetStmtFreq(ifstmt.GetElsePart()->GetStmtID()); + FreqType freq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()) - + GetFuncProfData()->GetStmtFreq(ifstmt.GetElsePart()->GetStmtID()); GetFuncProfData()->SetStmtFreq(labstmt->GetStmtID(), freq); } } else { @@ -244,9 +243,9 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { if (!thenempty) { if (ifstmt.GetThenPart()->GetLast()->IsCondBr()) { // Estimate a freq within [0, ifstmt-freq] without going after further - uint64 ifFreq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()); - uint64 ifElseFreq = GetFuncProfData()->GetStmtFreq(ifstmt.GetElsePart()->GetStmtID()); - uint64 freqDiff = (ifFreq >= ifElseFreq) ? (ifFreq - ifElseFreq) : 0; + FreqType ifFreq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()); + FreqType ifElseFreq = GetFuncProfData()->GetStmtFreq(ifstmt.GetElsePart()->GetStmtID()); + FreqType freqDiff = (ifFreq >= ifElseFreq) ? (ifFreq - ifElseFreq) : 0; GetFuncProfData()->SetStmtFreq(gotostmt->GetStmtID(), freqDiff); } else { GetFuncProfData()->CopyStmtFreq(gotostmt->GetStmtID(), ifstmt.GetThenPart()->GetStmtID()); diff --git a/src/mapleall/maple_me/src/seqvec.cpp b/src/mapleall/maple_me/src/seqvec.cpp index 1feb9978d1c031fcc6334d626b2c905ff9ce5701..9653a4684cc753cb90381987262594d80e6e513c 100644 --- a/src/mapleall/maple_me/src/seqvec.cpp +++ b/src/mapleall/maple_me/src/seqvec.cpp @@ -572,7 +572,7 @@ void SeqVectorize::MergeIassigns(MapleVector &cands) { blockParent->InsertBefore(iassign, dupScalarStmt); iassign->SetRHS(regreadNode); if (Options::profileUse && mirFunc->GetFuncProfData()) { - std::unordered_map &stmtFreqs = mirFunc->GetFuncProfData()->stmtFreqs; + std::unordered_map &stmtFreqs = mirFunc->GetFuncProfData()->stmtFreqs; if (stmtFreqs.find(iassign->GetStmtID()) != stmtFreqs.end()) { mirFunc->GetFuncProfData()->CopyStmtFreq(dupScalarStmt->GetStmtID(), iassign->GetStmtID()); } diff --git a/src/mapleall/maple_util/include/mpl_profdata.h b/src/mapleall/maple_util/include/mpl_profdata.h index 767bbf2ea4b3145b9b85803717acd5846e2a95af..a6f994330e5a555fb756a79a4c3f5b4c094f0f10 100644 --- a/src/mapleall/maple_util/include/mpl_profdata.h +++ b/src/mapleall/maple_util/include/mpl_profdata.h @@ -18,7 +18,7 @@ #include #include #include "mempool_allocator.h" - +#include "types_def.h" namespace maple { constexpr uint32_t HOTCALLSITEFREQ = 100; enum UpdateFreqOp { @@ -106,31 +106,31 @@ class FuncProfInfo { counts(alloc->Adapter()){}; ~FuncProfInfo() = default; - uint64_t GetFuncFrequency() const { + FreqType GetFuncFrequency() const { return entryFreq; } - void SetFuncFrequency(uint64_t freq) { + void SetFuncFrequency(FreqType freq) { entryFreq = freq; } - uint64_t GetFuncRealFrequency() const { + FreqType GetFuncRealFrequency() const { return realEntryfreq; } - void SetFuncRealFrequency(uint64_t freq) { + void SetFuncRealFrequency(FreqType freq) { realEntryfreq = freq; } - std::unordered_map &GetStmtFreqs() { + std::unordered_map &GetStmtFreqs() { return stmtFreqs; } - uint64_t GetStmtFreq(uint32_t stmtID) { + FreqType GetStmtFreq(uint32_t stmtID) { if (stmtFreqs.count(stmtID) > 0) { return stmtFreqs[stmtID]; } return -1; // unstored } - void SetStmtFreq(uint32_t stmtID, uint64_t freq) { - if (static_cast(freq) == -1) { + void SetStmtFreq(uint32_t stmtID, FreqType freq) { + if (freq == -1) { return; } stmtFreqs[stmtID] = freq; @@ -147,7 +147,7 @@ class FuncProfInfo { } bool IsHotCallSite(uint32_t stmtID) { if (stmtFreqs.count(stmtID) > 0) { - uint64_t freq = stmtFreqs[stmtID]; + FreqType freq = stmtFreqs[stmtID]; return (freq >= HOTCALLSITEFREQ); } ASSERT(0, "should not be here"); @@ -161,10 +161,10 @@ class FuncProfInfo { // Raw arc coverage counts. unsigned edgeCounts; - MapleVector counts; - uint64_t entryFreq; // record entry bb frequence - std::unordered_map stmtFreqs; // stmt_id is key, counter value - uint64_t realEntryfreq; // function prof data may be modified after clone/inline + MapleVector counts; + FreqType entryFreq; // record entry bb frequence + std::unordered_map stmtFreqs; // stmt_id is key, counter value + FreqType realEntryfreq; // function prof data may be modified after clone/inline }; class MplProfileData { diff --git a/src/mapleall/mpl2mpl/include/call_graph.h b/src/mapleall/mpl2mpl/include/call_graph.h index 932be4a4077a6cd98ea93f192245e01bad0ca855..bcb07ffbedfb5b0daa0c597930e45b23b626b210 100644 --- a/src/mapleall/mpl2mpl/include/call_graph.h +++ b/src/mapleall/mpl2mpl/include/call_graph.h @@ -475,8 +475,8 @@ class CGNode : public BaseGraphNode { return sccIdentity; } // check frequency - uint64_t GetFuncFrequency() const; - uint64_t GetCallsiteFrequency(const StmtNode *callstmt) const; + FreqType GetFuncFrequency() const; + FreqType GetCallsiteFrequency(const StmtNode *callstmt) const; private: // mirFunc is generated from callStmt's puIdx from mpl instruction diff --git a/src/mapleall/mpl2mpl/src/call_graph.cpp b/src/mapleall/mpl2mpl/src/call_graph.cpp index 2c35f1301cf1b2d1a9bfd33390f029ef837f9d9e..4ad3d7b98f8bef580bb745cfafcc7a18793d74df 100644 --- a/src/mapleall/mpl2mpl/src/call_graph.cpp +++ b/src/mapleall/mpl2mpl/src/call_graph.cpp @@ -245,7 +245,7 @@ bool CGNode::IsCalleeOf(CGNode *func) const { return callers.find(func) != callers.end(); } -uint64_t CGNode::GetCallsiteFrequency(const StmtNode *callstmt) const { +FreqType CGNode::GetCallsiteFrequency(const StmtNode *callstmt) const { FuncProfInfo *funcInfo = mirFunc->GetFuncProfData(); if (funcInfo->stmtFreqs.count(callstmt->GetStmtID()) > 0) { return funcInfo->stmtFreqs[callstmt->GetStmtID()]; @@ -254,7 +254,7 @@ uint64_t CGNode::GetCallsiteFrequency(const StmtNode *callstmt) const { return UINT64_MAX; } -uint64_t CGNode::GetFuncFrequency() const { +FreqType CGNode::GetFuncFrequency() const { FuncProfInfo *funcInfo = mirFunc->GetFuncProfData(); if (funcInfo) { return funcInfo->GetFuncRealFrequency(); diff --git a/src/mapleall/mpl2mpl/src/constantfold.cpp b/src/mapleall/mpl2mpl/src/constantfold.cpp index 783aaf82c8ce57d8b3f742ab2a35e1768d89178d..cf4e60de7812d731e6a1b1eeacced0d1af0d1b60 100644 --- a/src/mapleall/mpl2mpl/src/constantfold.cpp +++ b/src/mapleall/mpl2mpl/src/constantfold.cpp @@ -2264,7 +2264,7 @@ StmtNode *ConstantFold::SimplifyCondGoto(CondGotoNode *node) { ASSERT_NOT_NULL(intConst); if ((node->GetOpCode() == OP_brtrue && !intConst->IsZero()) || (node->GetOpCode() == OP_brfalse && intConst->IsZero())) { - uint32 freq = mirModule->CurFunction()->GetFreqFromLastStmt(node->GetStmtID()); + FreqType freq = mirModule->CurFunction()->GetFreqFromLastStmt(node->GetStmtID()); GotoNode *gotoNode = mirModule->CurFuncCodeMemPool()->New(OP_goto); gotoNode->SetOffset(node->GetOffset()); if (Options::profileUse && mirModule->CurFunction()->GetFuncProfData()) { diff --git a/src/mapleall/mpl2mpl/src/inline.cpp b/src/mapleall/mpl2mpl/src/inline.cpp index e0ea423abdf2f09a99d0aa48b8523d1239d0ba7d..bd6213e1106e5846ad3ad00e9db957d92a62a855 100644 --- a/src/mapleall/mpl2mpl/src/inline.cpp +++ b/src/mapleall/mpl2mpl/src/inline.cpp @@ -298,7 +298,7 @@ bool MInline::IsHotCallSite(const MIRFunction &caller, const MIRFunction &callee // use maple instrument profile if (Options::profileUse) { if (!caller.GetFuncProfData()) {return false;} - int64_t freq = static_cast(caller.GetFuncProfData()->GetStmtFreq(callStmt.GetStmtID())); + FreqType freq = caller.GetFuncProfData()->GetStmtFreq(callStmt.GetStmtID()); ASSERT(freq > 0, "sanity check"); return module.GetMapleProfile()->IsHotCallSite(static_cast(freq)); } diff --git a/src/mapleall/mpl2mpl/src/inline_summary.cpp b/src/mapleall/mpl2mpl/src/inline_summary.cpp index 0bf46fe98f0d5ee368571cd547bda985a8ed1734..24c06b1cea46619aa1e59f336756aae8e8f82612 100644 --- a/src/mapleall/mpl2mpl/src/inline_summary.cpp +++ b/src/mapleall/mpl2mpl/src/inline_summary.cpp @@ -658,7 +658,7 @@ void InlineSummary::MergeEdgeSummary(const InlineSummary &fromSummary, uint32 callStmtId = callEdgeSummaryPair.first; InlineEdgeSummary *callEdgeSummary = callEdgeSummaryPair.second; auto *callBBPredicate = callEdgeSummary ? callEdgeSummary->predicate : Predicate::TruePredicate(); - int64 callBBFreq = callEdgeSummary ? callEdgeSummary->frequency : -1; + FreqType callBBFreq = callEdgeSummary ? callEdgeSummary->frequency : -1; for (auto &pair : fromSummary.edgeSummaryMap) { auto fromCallId = pair.first; auto jt = callMeStmtIdMap.find(fromCallId); diff --git a/src/mapleall/mpl2mpl/src/inline_transformer.cpp b/src/mapleall/mpl2mpl/src/inline_transformer.cpp index 62e96db7636f03cb702394b003fe833a691fc105..559b7fab5851db85e01f124eafc20dcfd7a85d5d 100644 --- a/src/mapleall/mpl2mpl/src/inline_transformer.cpp +++ b/src/mapleall/mpl2mpl/src/inline_transformer.cpp @@ -500,8 +500,8 @@ BlockNode *InlineTransformer::CloneFuncBody(BlockNode &funcBody, bool recursiveF if (updateFreq) { auto *callerProfData = caller.GetFuncProfData(); auto *calleeProfData = callee.GetFuncProfData(); - uint64_t callsiteFreq = callerProfData->GetStmtFreq(callStmt.GetStmtID()); - uint64_t calleeEntryFreq = calleeProfData->GetFuncFrequency(); + FreqType callsiteFreq = callerProfData->GetStmtFreq(callStmt.GetStmtID()); + FreqType calleeEntryFreq = calleeProfData->GetFuncFrequency(); uint32_t updateOp = (kKeepOrigFreq | kUpdateFreqbyScale); BlockNode *blockNode; if (recursiveFirstClone) { diff --git a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp index a0702c4cf273cc100bc31c846e22afae0506ddfb..88c4bfc92843094e28132f3a31496c072a07aa6b 100644 --- a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp +++ b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp @@ -77,7 +77,7 @@ int FunctionProfileImport::ReadFuncProfile(MplProfileData *profData) { CHECK_FATAL(funcProf != nullptr, "nullptr check"); funcProf->counts.resize(countNum); for (uint32_t j = 0; j < countNum; j++) { - funcProf->counts[j] = ReadNum(); + funcProf->counts[j] = static_cast(ReadNum()); } } return 0;