]> granicus.if.org Git - clang/commitdiff
CodeGen: Avoid instrumenting implicit Decls more effectively
authorJustin Bogner <mail@justinbogner.com>
Fri, 25 Apr 2014 07:20:05 +0000 (07:20 +0000)
committerJustin Bogner <mail@justinbogner.com>
Fri, 25 Apr 2014 07:20:05 +0000 (07:20 +0000)
We don't assign counters for implicit Decls, but we were emitting code
to increment the (non-existent) counters and adding empty counter
lists in the output. This fixes the checks in assignRegionCounters and
emitInstrumentationData to do the right thing, and adds an assert for
the pathological case of emitting zero counters.

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

lib/CodeGen/CodeGenPGO.cpp
test/Profile/cxx-implicit.cpp [new file with mode: 0644]

index 6991e40f97cc81506f061e9793ee3805c57e41f8..9d4aaff8888ef1c7610a63481eb67952a26da7d6 100644 (file)
@@ -146,7 +146,7 @@ llvm::GlobalVariable *CodeGenPGO::buildDataVar() {
 }
 
 void CodeGenPGO::emitInstrumentationData() {
-  if (!CGM.getCodeGenOpts().ProfileInstrGenerate)
+  if (!RegionCounters)
     return;
 
   // Build the data.
@@ -803,7 +803,7 @@ void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) {
   llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
   if (!InstrumentRegions && !PGOReader)
     return;
-  if (!D)
+  if (D->isImplicit())
     return;
   setFuncName(Fn);
 
@@ -845,6 +845,7 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
     Walker.TraverseDecl(const_cast<BlockDecl *>(BD));
   else if (const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
     Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
+  assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
   NumRegionCounters = Walker.NextCounter;
   FunctionHash = Walker.Hash.finalize();
 }
@@ -920,6 +921,7 @@ void CodeGenPGO::destroyRegionCounters() {
   RegionCounterMap.reset();
   StmtCountMap.reset();
   RegionCounts.reset();
+  RegionCounters = nullptr;
 }
 
 /// \brief Calculate what to divide by to scale weights.
diff --git a/test/Profile/cxx-implicit.cpp b/test/Profile/cxx-implicit.cpp
new file mode 100644 (file)
index 0000000..79840ad
--- /dev/null
@@ -0,0 +1,17 @@
+// Ensure that implicit methods aren't instrumented.
+
+// RUN: %clang_cc1 -x c++ %s -triple %itanium_abi_triple -main-file-name cxx-implicit.cpp -o - -emit-llvm -fprofile-instr-generate | FileCheck %s
+
+// An implicit constructor is generated for Base. We should not emit counters
+// for it.
+// CHECK-NOT: @__llvm_profile_counters__ZN4BaseC2Ev =
+
+struct Base {
+  virtual void foo();
+};
+
+struct Derived : public Base {
+  Derived();
+};
+
+Derived::Derived() {}