[PGO] Simplify coverage mapping lowering
authorXinliang David Li <davidxl@google.com>
Thu, 7 Jan 2016 20:05:49 +0000 (20:05 +0000)
committerXinliang David Li <davidxl@google.com>
Thu, 7 Jan 2016 20:05:49 +0000 (20:05 +0000)
Coverage mapping data may reference names of functions
that are skipped by FE (e.g, unused inline functions). Since
those functions are skipped, normal instr-prof function lowering
pass won't put those names in the right section, so special
handling is needed to walk through coverage mapping structure
and recollect the references.

With this patch, only names that are skipped are processed. This
simplifies the lowering code and it no longer needs to make
assumptions coverage mapping data layout. It should also be
more efficient.

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

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

index 49569d89507b482cd187e4be47977d6d41930c47..adf62fb56c6bcacde79f0cbd9c006e022b0b93fc 100644 (file)
@@ -89,6 +89,12 @@ inline StringRef getCoverageMappingVarName() {
   return "__llvm_coverage_mapping";
 }
 
   return "__llvm_coverage_mapping";
 }
 
+/// Return the name of the internal variable recording the array
+/// of PGO name vars referenced by the coverage mapping, The owning
+/// functions of those names are not emitted by FE (e.g, unused inline
+/// functions.)
+inline StringRef getCoverageNamesVarName() { return "__llvm_coverage_names"; }
+
 /// Return the name of function that registers all the per-function control
 /// data at program startup time by calling __llvm_register_function. This
 /// function has internal linkage and is called by  __llvm_profile_init
 /// Return the name of function that registers all the per-function control
 /// data at program startup time by calling __llvm_register_function. This
 /// function has internal linkage and is called by  __llvm_profile_init
index 51ff95d9a74ca3f9d90e6151f889a444c05f4f79..28483e7e9b692788bd510abbd38ebe0873954cf2 100644 (file)
@@ -93,8 +93,8 @@ private:
   /// Replace instrprof_increment with an increment of the appropriate value.
   void lowerIncrement(InstrProfIncrementInst *Inc);
 
   /// Replace instrprof_increment with an increment of the appropriate value.
   void lowerIncrement(InstrProfIncrementInst *Inc);
 
-  /// Set up the section and uses for coverage data and its references.
-  void lowerCoverageData(GlobalVariable *CoverageData);
+  /// Force emitting of name vars for unused functions.
+  void lowerCoverageData(GlobalVariable *CoverageNamesVar);
 
   /// Get the region counters for an increment, creating them if necessary.
   ///
 
   /// Get the region counters for an increment, creating them if necessary.
   ///
@@ -156,9 +156,9 @@ bool InstrProfiling::runOnModule(Module &M) {
         }
       }
 
         }
       }
 
-  if (GlobalVariable *Coverage =
-          M.getNamedGlobal(getCoverageMappingVarName())) {
-    lowerCoverageData(Coverage);
+  if (GlobalVariable *CoverageNamesVar =
+          M.getNamedGlobal(getCoverageNamesVarName())) {
+    lowerCoverageData(CoverageNamesVar);
     MadeChange = true;
   }
 
     MadeChange = true;
   }
 
@@ -233,28 +233,16 @@ void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
   Inc->eraseFromParent();
 }
 
   Inc->eraseFromParent();
 }
 
-void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageData) {
-
-  Constant *Init = CoverageData->getInitializer();
-  // We're expecting { [4 x 32], [n x { i8*, i32, i32 }], [m x i8] }
-  // for some C. If not, the frontend's given us something broken.
-  assert(Init->getNumOperands() == 3 && "bad number of fields in coverage map");
-  assert(isa<ConstantArray>(Init->getAggregateElement(1)) &&
-         "invalid function list in coverage map");
-  ConstantArray *Records = cast<ConstantArray>(Init->getAggregateElement(1));
-  for (unsigned I = 0, E = Records->getNumOperands(); I < E; ++I) {
-    Constant *Record = Records->getOperand(I);
-    Value *V = const_cast<Value *>(Record->getOperand(0))->stripPointerCasts();
+void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) {
 
 
+  ConstantArray *Names =
+      cast<ConstantArray>(CoverageNamesVar->getInitializer());
+  for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) {
+    Constant *NC = Names->getOperand(I);
+    Value *V = NC->stripPointerCasts();
     assert(isa<GlobalVariable>(V) && "Missing reference to function name");
     GlobalVariable *Name = cast<GlobalVariable>(V);
 
     assert(isa<GlobalVariable>(V) && "Missing reference to function name");
     GlobalVariable *Name = cast<GlobalVariable>(V);
 
-    // If we have region counters for this name, we've already handled it.
-    auto It = ProfileDataMap.find(Name);
-    if (It != ProfileDataMap.end())
-      if (It->second.RegionCounters)
-        continue;
-
     // Move the name variable to the right section.
     Name->setSection(getNameSection());
     Name->setAlignment(1);
     // Move the name variable to the right section.
     Name->setSection(getNameSection());
     Name->setAlignment(1);