aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/CountingFunctionInserter.cpp
diff options
context:
space:
mode:
authorHal Finkel <hfinkel@anl.gov>2016-09-01 09:42:39 +0000
committerHal Finkel <hfinkel@anl.gov>2016-09-01 09:42:39 +0000
commit40d7f5c27740f2f03e3d6731772aae073a927c7c (patch)
tree7114e24238df2b020f3b8d09acd847d94a334ae6 /llvm/lib/CodeGen/CountingFunctionInserter.cpp
parent1ab16f8c2ddbaa1595002efe129c689e6ef8e75f (diff)
downloadllvm-40d7f5c27740f2f03e3d6731772aae073a927c7c.zip
llvm-40d7f5c27740f2f03e3d6731772aae073a927c7c.tar.gz
llvm-40d7f5c27740f2f03e3d6731772aae073a927c7c.tar.bz2
Add a counter-function insertion pass
As discussed in https://reviews.llvm.org/D22666, our current mechanism to support -pg profiling, where we insert calls to mcount(), or some similar function, is fundamentally broken. We insert these calls in the frontend, which means they get duplicated when inlining, and so the accumulated execution counts for the inlined-into functions are wrong. Because we don't want the presence of these functions to affect optimizaton, they should be inserted in the backend. Here's a pass which would do just that. The knowledge of the name of the counting function lives in the frontend, so we're passing it here as a function attribute. Clang will be updated to use this mechanism. Differential Revision: https://reviews.llvm.org/D22825 llvm-svn: 280347
Diffstat (limited to 'llvm/lib/CodeGen/CountingFunctionInserter.cpp')
-rw-r--r--llvm/lib/CodeGen/CountingFunctionInserter.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/CountingFunctionInserter.cpp b/llvm/lib/CodeGen/CountingFunctionInserter.cpp
new file mode 100644
index 0000000..1e46a7a
--- /dev/null
+++ b/llvm/lib/CodeGen/CountingFunctionInserter.cpp
@@ -0,0 +1,62 @@
+//===- CountingFunctionInserter.cpp - Insert mcount-like function calls ---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Insert calls to counter functions, such as mcount, intended to be called
+// once per function, at the beginning of each function.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/GlobalsModRef.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
+#include "llvm/Pass.h"
+using namespace llvm;
+
+namespace {
+ struct CountingFunctionInserter : public FunctionPass {
+ static char ID; // Pass identification, replacement for typeid
+ CountingFunctionInserter() : FunctionPass(ID) {
+ initializeCountingFunctionInserterPass(*PassRegistry::getPassRegistry());
+ }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.addPreserved<GlobalsAAWrapperPass>();
+ }
+
+ bool runOnFunction(Function &F) override {
+ std::string CountingFunctionName =
+ F.getFnAttribute("counting-function").getValueAsString();
+ if (CountingFunctionName.empty())
+ return false;
+
+ Type *VoidTy = Type::getVoidTy(F.getContext());
+ Constant *CountingFn =
+ F.getParent()->getOrInsertFunction(CountingFunctionName,
+ VoidTy, nullptr);
+ CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
+ return true;
+ }
+ };
+
+ char CountingFunctionInserter::ID = 0;
+}
+
+INITIALIZE_PASS(CountingFunctionInserter, "cfinserter",
+ "Inserts calls to mcount-like functions", false, false)
+
+//===----------------------------------------------------------------------===//
+//
+// CountingFunctionInserter - Give any unnamed non-void instructions "tmp" names.
+//
+FunctionPass *llvm::createCountingFunctionInserterPass() {
+ return new CountingFunctionInserter();
+}