[llvm] r316020 - [MachineOutliner][NFC] Move decrement logic to OutlinedFunction

Jessica Paquette via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 12:03:24 PDT 2017


Author: paquette
Date: Tue Oct 17 12:03:23 2017
New Revision: 316020

URL: http://llvm.org/viewvc/llvm-project?rev=316020&view=rev
Log:
[MachineOutliner][NFC] Move decrement logic to OutlinedFunction

This commit moves the decrement logic for outlined functions into the class,
and makes OccurrenceCount private. It can now be accessed via
getOccurrenceCount().

This makes it more difficult to accidentally introduce bugs by incorrectly
decrementing the occurrence count on OutlinedFunctions.


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=316020&r1=316019&r2=316020&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineOutliner.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineOutliner.cpp Tue Oct 17 12:03:23 2017
@@ -144,6 +144,11 @@ public:
 /// class of candidate.
 struct OutlinedFunction {
 
+private:
+  /// The number of candidates for this \p OutlinedFunction.
+  unsigned OccurrenceCount = 0;
+
+public:
   /// The actual outlined function created.
   /// This is initialized after we go through and create the actual function.
   MachineFunction *MF = nullptr;
@@ -151,9 +156,6 @@ struct OutlinedFunction {
   /// A number assigned to this function which appears at the end of its name.
   unsigned Name;
 
-  /// The number of candidates for this OutlinedFunction.
-  unsigned OccurrenceCount = 0;
-
   /// \brief The sequence of integers corresponding to the instructions in this
   /// function.
   std::vector<unsigned> Sequence;
@@ -161,6 +163,19 @@ struct OutlinedFunction {
   /// Contains all target-specific information for this \p OutlinedFunction.
   TargetInstrInfo::MachineOutlinerInfo MInfo;
 
+  /// Return the number of candidates for this \p OutlinedFunction.
+  unsigned getOccurrenceCount() {
+    return OccurrenceCount;
+  }
+
+  /// Decrement the occurrence count of this OutlinedFunction and return the
+  /// new count.
+  unsigned decrement() {
+    assert(OccurrenceCount > 0 && "Can't decrement an empty function!");
+    OccurrenceCount--;
+    return getOccurrenceCount();
+  }
+
   /// \brief Return the number of instructions it would take to outline this
   /// function.
   unsigned getOutliningCost() {
@@ -180,7 +195,7 @@ struct OutlinedFunction {
   OutlinedFunction(unsigned Name, unsigned OccurrenceCount,
                    const std::vector<unsigned> &Sequence,
                    TargetInstrInfo::MachineOutlinerInfo &MInfo)
-      : Name(Name), OccurrenceCount(OccurrenceCount), Sequence(Sequence),
+      : OccurrenceCount(OccurrenceCount), Name(Name), Sequence(Sequence),
         MInfo(MInfo) {}
 };
 
@@ -963,7 +978,7 @@ MachineOutliner::findCandidates(SuffixTr
           << " Instructions from outlining all occurrences ("
           << NV("OutliningCost", OF.getOutliningCost()) << ")"
           << " >= Unoutlined instruction count ("
-          << NV("NotOutliningCost", StringLen * OF.OccurrenceCount) << ")"
+          << NV("NotOutliningCost", StringLen * OF.getOccurrenceCount()) << ")"
           << " (Also found at: ";
 
         // Tell the user the other places the candidate was found.
@@ -1016,14 +1031,11 @@ void MachineOutliner::pruneOverlaps(std:
     if (!C.InCandidateList)
       return true;
 
-    // Check if C's associated function is still beneficial after previous
-    // pruning steps.
+    // C must be alive. Check if we should remove it.
     OutlinedFunction &F = FunctionList[C.FunctionIdx];
 
-    if (F.OccurrenceCount < 2 || F.getBenefit() < 1) {
-      assert(F.OccurrenceCount > 0 &&
-             "Can't remove OutlinedFunction with no occurrences!");
-      F.OccurrenceCount--;
+    if (F.getBenefit() < 1) {
+      F.decrement();
       C.InCandidateList = false;
       return true;
     }
@@ -1039,15 +1051,13 @@ void MachineOutliner::pruneOverlaps(std:
     OutlinedFunction &F = FunctionList[C.FunctionIdx];
 
     // Update C's associated function's occurrence count.
-    assert(F.OccurrenceCount > 0 &&
-           "Can't remove OutlinedFunction with no occurrences!");
-    F.OccurrenceCount--;
+    F.decrement();
 
     // Remove C from the CandidateList.
     C.InCandidateList = false;
 
     DEBUG(dbgs() << "- Removed a Candidate \n";
-          dbgs() << "--- Num fns left for candidate: " << F.OccurrenceCount
+          dbgs() << "--- Num fns left for candidate: " << F.getOccurrenceCount()
                  << "\n";
           dbgs() << "--- Candidate's functions's benefit: " << F.getBenefit()
                  << "\n";);
@@ -1208,7 +1218,7 @@ bool MachineOutliner::outline(Module &M,
     OutlinedFunction &OF = FunctionList[C.FunctionIdx];
 
     // Was its OutlinedFunction made unbeneficial during pruneOverlaps?
-    if (OF.OccurrenceCount < 2 || OF.getBenefit() < 1)
+    if (OF.getBenefit() < 1)
       continue;
 
     // If not, then outline it.




More information about the llvm-commits mailing list