Index: llvm/lib/CodeGen/RegAllocGreedy.cpp =================================================================== --- llvm/lib/CodeGen/RegAllocGreedy.cpp +++ llvm/lib/CodeGen/RegAllocGreedy.cpp @@ -551,6 +551,8 @@ unsigned &FoldedReloads, unsigned &Spills, unsigned &FoldedSpills); + void reportCostOfSplillsReloads(); + /// Report the number of spills and reloads for each loop. void reportNumberOfSplillsReloads() { for (MachineLoop *L : *Loops) { @@ -558,6 +560,7 @@ reportNumberOfSplillsReloads(L, Reloads, FoldedReloads, Spills, FoldedSpills); } + reportCostOfSplillsReloads(); } }; @@ -3112,6 +3115,52 @@ return 0; } +void RAGreedy::reportCostOfSplillsReloads() { + using namespace ore; + + ORE->emit([&]() { + float TotalMemWeight = 0.0f; + unsigned TotalMemCount = 0; + float TotalCopyWeight = 0.0f; + unsigned TotalCopyCount = 0; + const MachineFrameInfo &MFI = MF->getFrameInfo(); + for (MachineBasicBlock &MBB : *MF) { + unsigned MemCount = 0; + unsigned CopyCount = 0; + for (MachineInstr &MI : MBB) { + std::pair NonFoldabelRange = + TII->getNonFoldableRange(MI); + if (any_of(MI.operands(), [&](MachineOperand &MO) { + return MO.isFI() && MFI.isSpillSlotObjectIndex(MO.getIndex()) && + MI.getOperandNo(&MO) >= NonFoldabelRange.first && + MI.getOperandNo(&MO) < NonFoldabelRange.second; + })) + ++MemCount; + if (MI.isCopy()) { + MachineOperand &Dest = MI.getOperand(0); + MachineOperand &Src = MI.getOperand(1); + if (Dest.isReg() && Src.isReg() && Dest.getReg().isVirtual() && + Src.getReg().isVirtual()) + ++CopyCount; + } + } + float BlocFreq = MBFI->getBlockFreqRelativeToEntryBlock(&MBB); + TotalMemWeight += MemCount * BlocFreq; + TotalMemCount += MemCount; + TotalCopyWeight += CopyCount * BlocFreq; + TotalCopyCount += CopyCount; + } + + MachineOptimizationRemarkAnalysis R(DEBUG_TYPE, "SpillReloadCost", + DebugLoc(), &MF->front()); + R << NV("Total Spill Count", TotalMemCount); + R << NV("Total Spill Cost", TotalMemWeight); + R << NV("Total Copy Count", TotalCopyCount); + R << NV("Total Copy Cost", TotalCopyWeight); + return R; + }); +} + void RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads, unsigned &FoldedReloads, unsigned &Spills,