From 6c9a284ff3957d7f9f3957f01351f17500b01825 Mon Sep 17 00:00:00 2001 From: Jessica Paquette Date: Tue, 17 Oct 2017 21:11:58 +0000 Subject: [PATCH] [MachineOutliner][NFC] Clean up prune logic a bit Move the prune logic in pruneOverlaps to a new function, prune. This lets us reuse the prune functionality. Makes the code a bit more readable. It'll also make it easier to emit remarks/debug statements for pruned functions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316031 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/MachineOutliner.cpp | 69 +++++++++++++++++---------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/lib/CodeGen/MachineOutliner.cpp b/lib/CodeGen/MachineOutliner.cpp index b60740320b8..8c268cf32d8 100644 --- a/lib/CodeGen/MachineOutliner.cpp +++ b/lib/CodeGen/MachineOutliner.cpp @@ -164,9 +164,7 @@ public: TargetInstrInfo::MachineOutlinerInfo MInfo; /// Return the number of candidates for this \p OutlinedFunction. - unsigned getOccurrenceCount() { - return OccurrenceCount; - } + unsigned getOccurrenceCount() { return OccurrenceCount; } /// Decrement the occurrence count of this OutlinedFunction and return the /// new count. @@ -848,6 +846,10 @@ struct MachineOutliner : public ModulePass { SuffixTree &ST, InstructionMapper &Mapper, const TargetInstrInfo &TII); + /// Helper function for pruneOverlaps. + /// Removes \p C from the candidate list, and updates its \p OutlinedFunction. + void prune(Candidate &C, std::vector &FunctionList); + /// \brief Remove any overlapping candidates that weren't handled by the /// suffix tree's pruning method. /// @@ -1017,6 +1019,25 @@ MachineOutliner::findCandidates(SuffixTree &ST, const TargetInstrInfo &TII, return MaxLen; } +// Remove C from the candidate space, and update its OutlinedFunction. +void MachineOutliner::prune(Candidate &C, + std::vector &FunctionList) { + // Get the OutlinedFunction associated with this Candidate. + OutlinedFunction &F = FunctionList[C.FunctionIdx]; + + // Update C's associated function's occurrence count. + F.decrement(); + + // Remove C from the CandidateList. + C.InCandidateList = false; + + DEBUG(dbgs() << "- Removed a Candidate \n"; + dbgs() << "--- Num fns left for candidate: " << F.getOccurrenceCount() + << "\n"; + dbgs() << "--- Candidate's functions's benefit: " << F.getBenefit() + << "\n";); +} + void MachineOutliner::pruneOverlaps(std::vector &CandidateList, std::vector &FunctionList, InstructionMapper &Mapper, @@ -1025,18 +1046,15 @@ void MachineOutliner::pruneOverlaps(std::vector &CandidateList, // Return true if this candidate became unbeneficial for outlining in a // previous step. - auto ShouldSkipCandidate = [&FunctionList](Candidate &C) { + auto ShouldSkipCandidate = [&FunctionList, this](Candidate &C) { // Check if the candidate was removed in a previous step. if (!C.InCandidateList) return true; // C must be alive. Check if we should remove it. - OutlinedFunction &F = FunctionList[C.FunctionIdx]; - - if (F.getBenefit() < 1) { - F.decrement(); - C.InCandidateList = false; + if (FunctionList[C.FunctionIdx].getBenefit() < 1) { + prune(C, FunctionList); return true; } @@ -1044,25 +1062,6 @@ void MachineOutliner::pruneOverlaps(std::vector &CandidateList, return false; }; - // Remove C from the candidate space, and update its OutlinedFunction. - auto Prune = [&FunctionList](Candidate &C) { - - // Get the OutlinedFunction associated with this Candidate. - OutlinedFunction &F = FunctionList[C.FunctionIdx]; - - // Update C's associated function's occurrence count. - F.decrement(); - - // Remove C from the CandidateList. - C.InCandidateList = false; - - DEBUG(dbgs() << "- Removed a Candidate \n"; - dbgs() << "--- Num fns left for candidate: " << F.getOccurrenceCount() - << "\n"; - dbgs() << "--- Candidate's functions's benefit: " << F.getBenefit() - << "\n";); - }; - // TODO: Experiment with interval trees or other interval-checking structures // to lower the time complexity of this function. // TODO: Can we do better than the simple greedy choice? @@ -1116,13 +1115,17 @@ void MachineOutliner::pruneOverlaps(std::vector &CandidateList, // // Approximate this by picking the one which would have saved us the // most instructions before any pruning. - if (C1.Benefit >= C2.Benefit) { - Prune(C2); - } else { - Prune(C1); - // C1 is out, so we don't have to compare it against anyone else. + + // Is C2 a better candidate? + if (C2.Benefit > C1.Benefit) { + // Yes, so prune C1. Since C1 is dead, we don't have to compare it + // against anything anymore, so break. + prune(C1, FunctionList); break; } + + // Prune C2 and move on to the next candidate. + prune(C2, FunctionList); } } } -- 2.40.0