From 9f33943ee0155ba05d509e7a3b6999f51d0bff6d Mon Sep 17 00:00:00 2001 From: Serguei Katkov Date: Fri, 9 Apr 2021 16:31:05 +0700 Subject: [PATCH] [GreedyRA ORE] Add stats for copy of virtual registers. Greedy RA adds copies of virtual registers when splitting live interval. This stat might be useful. Reviewers: reames, MatzeB, anemet, thegameg Reviewed By: reames Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D100017 --- llvm/lib/CodeGen/RegAllocGreedy.cpp | 49 +++++++++++++++--------- llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll | 10 ++--- llvm/test/CodeGen/X86/statepoint-ra.ll | 4 +- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp index cede0e3..d377ca6 100644 --- a/llvm/lib/CodeGen/RegAllocGreedy.cpp +++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp @@ -554,10 +554,11 @@ private: unsigned ZeroCostFoldedReloads = 0; unsigned Spills = 0; unsigned FoldedSpills = 0; + unsigned Copies = 0; bool isEmpty() { return !(Reloads || FoldedReloads || Spills || FoldedSpills || - ZeroCostFoldedReloads); + ZeroCostFoldedReloads || Copies); } void add(RAGreedyStats other) { @@ -566,19 +567,20 @@ private: ZeroCostFoldedReloads += other.ZeroCostFoldedReloads; Spills += other.Spills; FoldedSpills += other.FoldedSpills; + Copies += other.Copies; } void report(MachineOptimizationRemarkMissed &R); }; - /// Compute the number of spills and reloads for a basic block. - RAGreedyStats computeNumberOfSplillsReloads(MachineBasicBlock &MBB); + /// Compute statistic for a basic block. + RAGreedyStats computeStats(MachineBasicBlock &MBB); - /// Compute and report the number of spills through a remark. - RAGreedyStats reportNumberOfSplillsReloads(MachineLoop *L); + /// Compute and report statistic through a remark. + RAGreedyStats reportStats(MachineLoop *L); - /// Report the number of spills and reloads for each loop. - void reportNumberOfSplillsReloads(); + /// Report the statistic for each loop. + void reportStats(); }; } // end anonymous namespace @@ -3145,10 +3147,11 @@ void RAGreedy::RAGreedyStats::report(MachineOptimizationRemarkMissed &R) { if (ZeroCostFoldedReloads) R << NV("NumZeroCostFoldedReloads", ZeroCostFoldedReloads) << " zero cost folded reloads "; + if (Copies) + R << NV("NumVRCopies", Copies) << " virtual registers copies "; } -RAGreedy::RAGreedyStats -RAGreedy::computeNumberOfSplillsReloads(MachineBasicBlock &MBB) { +RAGreedy::RAGreedyStats RAGreedy::computeStats(MachineBasicBlock &MBB) { RAGreedyStats Stats; const MachineFrameInfo &MFI = MF->getFrameInfo(); int FI; @@ -3163,8 +3166,16 @@ RAGreedy::computeNumberOfSplillsReloads(MachineBasicBlock &MBB) { MI.getOpcode() == TargetOpcode::STATEPOINT; }; for (MachineInstr &MI : MBB) { - SmallVector Accesses; + if (MI.isCopy()) { + MachineOperand &Dest = MI.getOperand(0); + MachineOperand &Src = MI.getOperand(1); + if (Dest.isReg() && Src.isReg() && Dest.getReg().isVirtual() && + Src.getReg().isVirtual()) + ++Stats.Copies; + continue; + } + SmallVector Accesses; if (TII->isLoadFromStackSlot(MI, FI) && MFI.isSpillSlotObjectIndex(FI)) { ++Stats.Reloads; continue; @@ -3209,23 +3220,23 @@ RAGreedy::computeNumberOfSplillsReloads(MachineBasicBlock &MBB) { return Stats; } -RAGreedy::RAGreedyStats RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L) { +RAGreedy::RAGreedyStats RAGreedy::reportStats(MachineLoop *L) { RAGreedyStats Stats; // Sum up the spill and reloads in subloops. for (MachineLoop *SubLoop : *L) - Stats.add(reportNumberOfSplillsReloads(SubLoop)); + Stats.add(reportStats(SubLoop)); for (MachineBasicBlock *MBB : L->getBlocks()) // Handle blocks that were not included in subloops. if (Loops->getLoopFor(MBB) == L) - Stats.add(computeNumberOfSplillsReloads(*MBB)); + Stats.add(computeStats(*MBB)); if (!Stats.isEmpty()) { using namespace ore; ORE->emit([&]() { - MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReload", + MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReloadCopies", L->getStartLoc(), L->getHeader()); Stats.report(R); R << "generated in loop"; @@ -3235,16 +3246,16 @@ RAGreedy::RAGreedyStats RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L) { return Stats; } -void RAGreedy::reportNumberOfSplillsReloads() { +void RAGreedy::reportStats() { if (!ORE->allowExtraAnalysis(DEBUG_TYPE)) return; RAGreedyStats Stats; for (MachineLoop *L : *Loops) - Stats.add(reportNumberOfSplillsReloads(L)); + Stats.add(reportStats(L)); // Process non-loop blocks. for (MachineBasicBlock &MBB : *MF) if (!Loops->getLoopFor(&MBB)) - Stats.add(computeNumberOfSplillsReloads(MBB)); + Stats.add(computeStats(MBB)); if (!Stats.isEmpty()) { using namespace ore; @@ -3252,7 +3263,7 @@ void RAGreedy::reportNumberOfSplillsReloads() { DebugLoc Loc; if (auto *SP = MF->getFunction().getSubprogram()) Loc = DILocation::get(SP->getContext(), SP->getLine(), 1, SP); - MachineOptimizationRemarkMissed R(DEBUG_TYPE, "SpillReload", Loc, + MachineOptimizationRemarkMissed R(DEBUG_TYPE, "SpillReloadCopies", Loc, &MF->front()); Stats.report(R); R << "generated in function"; @@ -3322,7 +3333,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { if (VerifyEnabled) MF->verify(this, "Before post optimization"); postOptimization(); - reportNumberOfSplillsReloads(); + reportStats(); releaseMemory(); return true; diff --git a/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll b/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll index e7c8408..f17932f 100644 --- a/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll +++ b/llvm/test/CodeGen/AArch64/arm64-spill-remarks.ll @@ -39,7 +39,7 @@ ; YAML: --- !Missed ; YAML: Pass: regalloc -; YAML: Name: LoopSpillReload +; YAML: Name: LoopSpillReloadCopies ; YAML: DebugLoc: { File: '/tmp/kk.c', Line: 3, Column: 20 } ; YAML: Function: fpr128 ; YAML: Hotness: 300 @@ -52,7 +52,7 @@ ; YAML: ... ; YAML: --- !Missed ; YAML: Pass: regalloc -; YAML: Name: LoopSpillReload +; YAML: Name: LoopSpillReloadCopies ; YAML: DebugLoc: { File: '/tmp/kk.c', Line: 2, Column: 20 } ; YAML: Function: fpr128 ; YAML: Hotness: 30000 @@ -65,7 +65,7 @@ ; YAML: ... ; YAML: --- !Missed ; YAML: Pass: regalloc -; YAML: Name: LoopSpillReload +; YAML: Name: LoopSpillReloadCopies ; YAML: DebugLoc: { File: '/tmp/kk.c', Line: 1, Column: 20 } ; YAML: Function: fpr128 ; YAML: Hotness: 300 @@ -78,7 +78,7 @@ ; YAML: ... ; YAML: --- !Missed ; YAML: Pass: regalloc -; YAML: Name: SpillReload +; YAML: Name: SpillReloadCopies ; YAML: DebugLoc: { File: '/tmp/kk.c', Line: 1, Column: 1 } ; YAML: Function: fpr128 ; YAML: Hotness: 3 @@ -93,7 +93,7 @@ ; THRESHOLD_YAML-NOT: Hotness: 300{{$}} ; THRESHOLD_YAML: --- !Missed ; THRESHOLD_YAML: Pass: regalloc -; THRESHOLD_YAML: Name: LoopSpillReload +; THRESHOLD_YAML: Name: LoopSpillReloadCopies ; THRESHOLD_YAML: DebugLoc: { File: '/tmp/kk.c', Line: 2, Column: 20 } ; THRESHOLD_YAML: Function: fpr128 ; THRESHOLD_YAML: Hotness: 30000 diff --git a/llvm/test/CodeGen/X86/statepoint-ra.ll b/llvm/test/CodeGen/X86/statepoint-ra.ll index d044c02..ea460da 100644 --- a/llvm/test/CodeGen/X86/statepoint-ra.ll +++ b/llvm/test/CodeGen/X86/statepoint-ra.ll @@ -7,7 +7,7 @@ target triple = "x86_64-unknown-linux-gnu" ;YAML: --- !Missed ;YAML: Pass: regalloc -;YAML: Name: SpillReload +;YAML: Name: SpillReloadCopies ;YAML: Function: barney ;YAML: Args: ;YAML: - NumSpills: '10' @@ -16,6 +16,8 @@ target triple = "x86_64-unknown-linux-gnu" ;YAML: - String: ' reloads ' ;YAML: - NumZeroCostFoldedReloads: '20' ;YAML: - String: ' zero cost folded reloads ' +;YAML: - NumVRCopies: '2' +;YAML: - String: ' virtual registers copies ' ;YAML: - String: generated in function define void @barney(i8 addrspace(1)* %arg, double %arg1, double %arg2, double %arg3, double %arg4, double %arg5, double %arg6, double %arg7, double %arg8, double %arg9, double %arg10, double %arg11, double %arg12) gc "statepoint-example" personality i32* ()* @widget { -- 2.7.4