Add helper functions and remove hard coded references to instProf related name/name...
authorXinliang David Li <davidxl@google.com>
Thu, 22 Oct 2015 20:32:12 +0000 (20:32 +0000)
committerXinliang David Li <davidxl@google.com>
Thu, 22 Oct 2015 20:32:12 +0000 (20:32 +0000)
This is a clean up patch that defines instr prof section and variable
name prefixes in a common header with access helper functions.
clang FE change will be done as a follow up once this patch is in.

Differential Revision: http://reviews.llvm.org/D13919

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251058 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ProfileData/InstrProf.h
lib/ProfileData/CoverageMappingReader.cpp
lib/Transforms/Instrumentation/InstrProfiling.cpp

index f425c6e13ed30343b4538c936715b50329451539..3dad3f5ccc683a824ca47ce3fd22dd467729d25b 100644 (file)
 #include <vector>
 
 namespace llvm {
 #include <vector>
 
 namespace llvm {
+
+inline StringRef getInstrProfCountersSectionName(bool AddSegment) {
+  return AddSegment ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
+}
+
+inline StringRef getInstrProfNameSectionName(bool AddSegment) {
+  return AddSegment ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
+}
+
+inline StringRef getInstrProfDataSectionName(bool AddSegment) {
+  return AddSegment ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
+}
+
+inline StringRef getInstrProfCoverageSectionName(bool AddSegment) {
+  return AddSegment ? "__DATA,__llvm_covmap" : "__llvm_covmap";
+}
+
+inline StringRef getInstrProfNameVarPrefix() { return "__llvm_profile_names_"; }
+
+inline StringRef getInstrProfDataVarPrefix() { return "__llvm_profile_data_"; }
+
+inline StringRef getInstrProfCountersVarPrefix() {
+  return "__llvm_profile_counters_";
+}
+
+inline StringRef getInstrProfComdatPrefix() { return "__llvm_profile_vars_"; }
+
+inline StringRef getCoverageMappingVarName() {
+  return "__llvm_coverage_mapping";
+}
+
 const std::error_category &instrprof_category();
 
 enum class instrprof_error {
 const std::error_category &instrprof_category();
 
 enum class instrprof_error {
index 334a3f51ec9e5762122890dba46009f7d1ec9a49..7fd5f009a91507fa269189227bf242df2a1a687d 100644 (file)
@@ -477,10 +477,11 @@ static std::error_code loadBinaryFormat(MemoryBufferRef ObjectBuffer,
                                 : support::endianness::big;
 
   // Look for the sections that we are interested in.
                                 : support::endianness::big;
 
   // Look for the sections that we are interested in.
-  auto NamesSection = lookupSection(*OF, "__llvm_prf_names");
+  auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false));
   if (auto EC = NamesSection.getError())
     return EC;
   if (auto EC = NamesSection.getError())
     return EC;
-  auto CoverageSection = lookupSection(*OF, "__llvm_covmap");
+  auto CoverageSection =
+      lookupSection(*OF, getInstrProfCoverageSectionName(false));
   if (auto EC = CoverageSection.getError())
     return EC;
 
   if (auto EC = CoverageSection.getError())
     return EC;
 
index a68fd3db6c2e9238ae04bec683a68c38b73f6d1a..a8c4f8f45ae7bd6fb3d6486971b1fce81f7e6cdf 100644 (file)
@@ -13,6 +13,7 @@
 //
 //===----------------------------------------------------------------------===//
 
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ProfileData/InstrProf.h"
 #include "llvm/Transforms/Instrumentation.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Transforms/Instrumentation.h"
 
 #include "llvm/ADT/Triple.h"
@@ -58,22 +59,22 @@ private:
 
   /// Get the section name for the counter variables.
   StringRef getCountersSection() const {
 
   /// Get the section name for the counter variables.
   StringRef getCountersSection() const {
-    return isMachO() ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
+    return getInstrProfCountersSectionName(isMachO());
   }
 
   /// Get the section name for the name variables.
   StringRef getNameSection() const {
   }
 
   /// Get the section name for the name variables.
   StringRef getNameSection() const {
-    return isMachO() ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
+    return getInstrProfNameSectionName(isMachO());
   }
 
   /// Get the section name for the profile data variables.
   StringRef getDataSection() const {
   }
 
   /// Get the section name for the profile data variables.
   StringRef getDataSection() const {
-    return isMachO() ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
+    return getInstrProfDataSectionName(isMachO());
   }
 
   /// Get the section name for the coverage mapping data.
   StringRef getCoverageSection() const {
   }
 
   /// Get the section name for the coverage mapping data.
   StringRef getCoverageSection() const {
-    return isMachO() ? "__DATA,__llvm_covmap" : "__llvm_covmap";
+    return getInstrProfCoverageSectionName(isMachO());
   }
 
   /// Replace instrprof_increment with an increment of the appropriate value.
   }
 
   /// Replace instrprof_increment with an increment of the appropriate value.
@@ -127,7 +128,8 @@ bool InstrProfiling::runOnModule(Module &M) {
           lowerIncrement(Inc);
           MadeChange = true;
         }
           lowerIncrement(Inc);
           MadeChange = true;
         }
-  if (GlobalVariable *Coverage = M.getNamedGlobal("__llvm_coverage_mapping")) {
+  if (GlobalVariable *Coverage =
+          M.getNamedGlobal(getCoverageMappingVarName())) {
     lowerCoverageData(Coverage);
     MadeChange = true;
   }
     lowerCoverageData(Coverage);
     MadeChange = true;
   }
@@ -183,10 +185,10 @@ void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageData) {
 }
 
 /// Get the name of a profiling variable for a particular function.
 }
 
 /// Get the name of a profiling variable for a particular function.
-static std::string getVarName(InstrProfIncrementInst *Inc, StringRef VarName) {
+static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) {
   auto *Arr = cast<ConstantDataArray>(Inc->getName()->getInitializer());
   StringRef Name = Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
   auto *Arr = cast<ConstantDataArray>(Inc->getName()->getInitializer());
   StringRef Name = Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
-  return ("__llvm_profile_" + VarName + "_" + Name).str();
+  return (Prefix + Name).str();
 }
 
 GlobalVariable *
 }
 
 GlobalVariable *
@@ -203,7 +205,8 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
   Function *Fn = Inc->getParent()->getParent();
   Comdat *ProfileVarsComdat = nullptr;
   if (Fn->hasComdat())
   Function *Fn = Inc->getParent()->getParent();
   Comdat *ProfileVarsComdat = nullptr;
   if (Fn->hasComdat())
-    ProfileVarsComdat = M->getOrInsertComdat(StringRef(getVarName(Inc, "vars")));
+    ProfileVarsComdat = M->getOrInsertComdat(
+        StringRef(getVarName(Inc, getInstrProfComdatPrefix())));
   Name->setSection(getNameSection());
   Name->setAlignment(1);
   Name->setComdat(ProfileVarsComdat);
   Name->setSection(getNameSection());
   Name->setAlignment(1);
   Name->setComdat(ProfileVarsComdat);
@@ -213,9 +216,10 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
 
   // Create the counters variable.
   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
 
   // Create the counters variable.
-  auto *Counters = new GlobalVariable(*M, CounterTy, false, Name->getLinkage(),
-                                      Constant::getNullValue(CounterTy),
-                                      getVarName(Inc, "counters"));
+  auto *Counters =
+      new GlobalVariable(*M, CounterTy, false, Name->getLinkage(),
+                         Constant::getNullValue(CounterTy),
+                         getVarName(Inc, getInstrProfCountersVarPrefix()));
   Counters->setVisibility(Name->getVisibility());
   Counters->setSection(getCountersSection());
   Counters->setAlignment(8);
   Counters->setVisibility(Name->getVisibility());
   Counters->setSection(getCountersSection());
   Counters->setAlignment(8);
@@ -240,7 +244,7 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
       ConstantExpr::getBitCast(Counters, Int64PtrTy)};
   auto *Data = new GlobalVariable(*M, DataTy, true, Name->getLinkage(),
                                   ConstantStruct::get(DataTy, DataVals),
       ConstantExpr::getBitCast(Counters, Int64PtrTy)};
   auto *Data = new GlobalVariable(*M, DataTy, true, Name->getLinkage(),
                                   ConstantStruct::get(DataTy, DataVals),
-                                  getVarName(Inc, "data"));
+                                  getVarName(Inc, getInstrProfDataVarPrefix()));
   Data->setVisibility(Name->getVisibility());
   Data->setSection(getDataSection());
   Data->setAlignment(8);
   Data->setVisibility(Name->getVisibility());
   Data->setSection(getDataSection());
   Data->setAlignment(8);