[llvm] r316031 - [MachineOutliner][NFC] Clean up prune logic a bit

Jessica Paquette via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 14:11:58 PDT 2017


Author: paquette
Date: Tue Oct 17 14:11:58 2017
New Revision: 316031

URL: http://llvm.org/viewvc/llvm-project?rev=316031&view=rev
Log:
[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.


Modified:
    llvm/trunk/lib/CodeGen/MachineOutliner.cpp

Modified: llvm/trunk/lib/CodeGen/MachineOutliner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineOutliner.cpp?rev=316031&r1=316030&r2=316031&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineOutliner.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineOutliner.cpp Tue Oct 17 14:11:58 2017
@@ -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 ModulePa
                               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<OutlinedFunction> &FunctionList);
+
   /// \brief Remove any overlapping candidates that weren't handled by the
   /// suffix tree's pruning method.
   ///
@@ -1017,6 +1019,25 @@ MachineOutliner::findCandidates(SuffixTr
   return MaxLen;
 }
 
+// Remove C from the candidate space, and update its OutlinedFunction.
+void MachineOutliner::prune(Candidate &C,
+                            std::vector<OutlinedFunction> &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<Candidate> &CandidateList,
                                     std::vector<OutlinedFunction> &FunctionList,
                                     InstructionMapper &Mapper,
@@ -1025,18 +1046,15 @@ void MachineOutliner::pruneOverlaps(std:
 
   // 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:
     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:
       //
       // 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);
     }
   }
 }




More information about the llvm-commits mailing list