[llvm] 4e069d9 - [NFC][BlockFrequency] Move operator definitions into header

Dhruv Chawla via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 13 04:25:42 PDT 2023


Author: Dhruv Chawla
Date: 2023-06-13T16:53:56+05:30
New Revision: 4e069d9cf910967c16fa6929cd15ccc20082533e

URL: https://github.com/llvm/llvm-project/commit/4e069d9cf910967c16fa6929cd15ccc20082533e
DIFF: https://github.com/llvm/llvm-project/commit/4e069d9cf910967c16fa6929cd15ccc20082533e.diff

LOG: [NFC][BlockFrequency] Move operator definitions into header

While BlockFrequency::operator+= is a very simple operation, it's
definition is present in another TU which means that it doesn't get
inlined in non-LTO builds. This means that there is some performance
left on the table in those builds, as this operator is called many
times.

This patch moves that operator (and a few others) into the
BlockFrequency.h header which gives a small speedup (~0.1%):
https://llvm-compile-time-tracker.com/compare.php?from=6ee594be53e7efaa12086ad20f0d0268092a4c73&to=6ac6cd99e211fae5ae5de41ad608604aa22f1882&stat=instructions%3Au

Differential Revision: https://reviews.llvm.org/D152781

Added: 
    

Modified: 
    llvm/include/llvm/Support/BlockFrequency.h
    llvm/lib/Support/BlockFrequency.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/BlockFrequency.h b/llvm/include/llvm/Support/BlockFrequency.h
index 175713c09e029..6c624d7dad7d8 100644
--- a/llvm/include/llvm/Support/BlockFrequency.h
+++ b/llvm/include/llvm/Support/BlockFrequency.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_SUPPORT_BLOCKFREQUENCY_H
 #define LLVM_SUPPORT_BLOCKFREQUENCY_H
 
+#include <cassert>
 #include <cstdint>
 
 namespace llvm {
@@ -44,15 +45,49 @@ class BlockFrequency {
   BlockFrequency operator/(BranchProbability Prob) const;
 
   /// Adds another block frequency using saturating arithmetic.
-  BlockFrequency &operator+=(BlockFrequency Freq);
-  BlockFrequency operator+(BlockFrequency Freq) const;
+  BlockFrequency &operator+=(BlockFrequency Freq) {
+    uint64_t Before = Freq.Frequency;
+    Frequency += Freq.Frequency;
+
+    // If overflow, set frequency to the maximum value.
+    if (Frequency < Before)
+      Frequency = UINT64_MAX;
+
+    return *this;
+  }
+  BlockFrequency operator+(BlockFrequency Freq) const {
+    BlockFrequency NewFreq(Frequency);
+    NewFreq += Freq;
+    return NewFreq;
+  }
 
   /// Subtracts another block frequency using saturating arithmetic.
-  BlockFrequency &operator-=(BlockFrequency Freq);
-  BlockFrequency operator-(BlockFrequency Freq) const;
+  BlockFrequency &operator-=(BlockFrequency Freq) {
+    // If underflow, set frequency to 0.
+    if (Frequency <= Freq.Frequency)
+      Frequency = 0;
+    else
+      Frequency -= Freq.Frequency;
+    return *this;
+  }
+  BlockFrequency operator-(BlockFrequency Freq) const {
+    BlockFrequency NewFreq(Frequency);
+    NewFreq -= Freq;
+    return NewFreq;
+  }
 
   /// Shift block frequency to the right by count digits saturating to 1.
-  BlockFrequency &operator>>=(const unsigned count);
+  BlockFrequency &operator>>=(const unsigned count) {
+    // Frequency can never be 0 by design.
+    assert(Frequency != 0);
+
+    // Shift right by count.
+    Frequency >>= count;
+
+    // Saturate to 1 if we are 0.
+    Frequency |= Frequency == 0;
+    return *this;
+  }
 
   bool operator<(BlockFrequency RHS) const {
     return Frequency < RHS.Frequency;

diff  --git a/llvm/lib/Support/BlockFrequency.cpp b/llvm/lib/Support/BlockFrequency.cpp
index 702165ac480b4..a4a1e477d9403 100644
--- a/llvm/lib/Support/BlockFrequency.cpp
+++ b/llvm/lib/Support/BlockFrequency.cpp
@@ -12,7 +12,6 @@
 
 #include "llvm/Support/BlockFrequency.h"
 #include "llvm/Support/BranchProbability.h"
-#include <cassert>
 
 using namespace llvm;
 
@@ -37,47 +36,3 @@ BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
   Freq /= Prob;
   return Freq;
 }
-
-BlockFrequency &BlockFrequency::operator+=(BlockFrequency Freq) {
-  uint64_t Before = Freq.Frequency;
-  Frequency += Freq.Frequency;
-
-  // If overflow, set frequency to the maximum value.
-  if (Frequency < Before)
-    Frequency = UINT64_MAX;
-
-  return *this;
-}
-
-BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const {
-  BlockFrequency NewFreq(Frequency);
-  NewFreq += Freq;
-  return NewFreq;
-}
-
-BlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) {
-  // If underflow, set frequency to 0.
-  if (Frequency <= Freq.Frequency)
-    Frequency = 0;
-  else
-    Frequency -= Freq.Frequency;
-  return *this;
-}
-
-BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const {
-  BlockFrequency NewFreq(Frequency);
-  NewFreq -= Freq;
-  return NewFreq;
-}
-
-BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
-  // Frequency can never be 0 by design.
-  assert(Frequency != 0);
-
-  // Shift right by count.
-  Frequency >>= count;
-
-  // Saturate to 1 if we are 0.
-  Frequency |= Frequency == 0;
-  return *this;
-}


        


More information about the llvm-commits mailing list