]> granicus.if.org Git - clang/commitdiff
CodeGen: Move hot/cold logic out of PGOProfileData
authorJustin Bogner <mail@justinbogner.com>
Wed, 12 Mar 2014 18:14:32 +0000 (18:14 +0000)
committerJustin Bogner <mail@justinbogner.com>
Wed, 12 Mar 2014 18:14:32 +0000 (18:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203689 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenPGO.cpp
lib/CodeGen/CodeGenPGO.h

index ed92de6e6dcddfd8a0905e93b4f68d261de7c64b..3206daa76a6cc686c45769a134a25062d505fa38 100644 (file)
@@ -99,32 +99,6 @@ PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path)
   MaxFunctionCount = MaxCount;
 }
 
-/// Return true if a function is hot. If we know nothing about the function,
-/// return false.
-bool PGOProfileData::isHotFunction(StringRef FuncName) {
-  llvm::StringMap<uint64_t>::const_iterator CountIter =
-    FunctionCounts.find(FuncName);
-  // If we know nothing about the function, return false.
-  if (CountIter == FunctionCounts.end())
-    return false;
-  // FIXME: functions with >= 30% of the maximal function count are
-  // treated as hot. This number is from preliminary tuning on SPEC.
-  return CountIter->getValue() >= (uint64_t)(0.3 * (double)MaxFunctionCount);
-}
-
-/// Return true if a function is cold. If we know nothing about the function,
-/// return false.
-bool PGOProfileData::isColdFunction(StringRef FuncName) {
-  llvm::StringMap<uint64_t>::const_iterator CountIter =
-    FunctionCounts.find(FuncName);
-  // If we know nothing about the function, return false.
-  if (CountIter == FunctionCounts.end())
-    return false;
-  // FIXME: functions with <= 1% of the maximal function count are treated as
-  // cold. This number is from preliminary tuning on SPEC.
-  return CountIter->getValue() <= (uint64_t)(0.01 * (double)MaxFunctionCount);
-}
-
 bool PGOProfileData::getFunctionCounts(StringRef FuncName,
                                        std::vector<uint64_t> &Counts) {
   // Find the relevant section of the pgo-data file.
@@ -796,13 +770,7 @@ void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
   if (PGOData) {
     loadRegionCounts(PGOData);
     computeRegionCounts(D);
-
-    // Turn on InlineHint attribute for hot functions.
-    if (PGOData->isHotFunction(getFuncName()))
-      Fn->addFnAttr(llvm::Attribute::InlineHint);
-    // Turn on Cold attribute for cold functions.
-    else if (PGOData->isColdFunction(getFuncName()))
-      Fn->addFnAttr(llvm::Attribute::Cold);
+    applyFunctionAttributes(PGOData, Fn);
   }
 }
 
@@ -829,6 +797,23 @@ void CodeGenPGO::computeRegionCounts(const Decl *D) {
     Walker.VisitBlockDecl(BD);
 }
 
+void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData,
+                                         llvm::Function *Fn) {
+  if (!haveRegionCounts())
+    return;
+
+  uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount();
+  uint64_t FunctionCount = getRegionCount(0);
+  if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount))
+    // Turn on InlineHint attribute for hot functions.
+    // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal.
+    Fn->addFnAttr(llvm::Attribute::InlineHint);
+  else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount))
+    // Turn on Cold attribute for cold functions.
+    // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal.
+    Fn->addFnAttr(llvm::Attribute::Cold);
+}
+
 void CodeGenPGO::emitCounterVariables() {
   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
   llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx),
index 7ee1b27573b01d83e28900230ed13565f1a9551a..51d59cf9a9b1c2856deb6606e11dcb52e5902ddc 100644 (file)
@@ -43,12 +43,8 @@ public:
   /// Fill Counts with the profile data for the given function name. Returns
   /// false on success.
   bool getFunctionCounts(StringRef FuncName, std::vector<uint64_t> &Counts);
-  /// Return true if a function is hot. If we know nothing about the function,
-  /// return false.
-  bool isHotFunction(StringRef FuncName);
-  /// Return true if a function is cold. If we know nothing about the function,
-  /// return false.
-  bool isColdFunction(StringRef FuncName);
+  /// Return the maximum of all known function counts.
+  uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
 };
 
 /// Per-function PGO state. This class should generally not be used directly,
@@ -140,6 +136,7 @@ private:
   void setFuncName(llvm::Function *Fn);
   void mapRegionCounters(const Decl *D);
   void computeRegionCounts(const Decl *D);
+  void applyFunctionAttributes(PGOProfileData *PGOData, llvm::Function *Fn);
   void loadRegionCounts(PGOProfileData *PGOData);
   void emitCounterVariables();