1 //===- CountingFunctionInserter.cpp - Insert mcount-like function calls ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Insert calls to counter functions, such as mcount, intended to be called
11 // once per function, at the beginning of each function.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/GlobalsModRef.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Pass.h"
25 struct CountingFunctionInserter : public FunctionPass {
26 static char ID; // Pass identification, replacement for typeid
27 CountingFunctionInserter() : FunctionPass(ID) {
28 initializeCountingFunctionInserterPass(*PassRegistry::getPassRegistry());
31 void getAnalysisUsage(AnalysisUsage &AU) const override {
32 AU.addPreserved<GlobalsAAWrapperPass>();
35 bool runOnFunction(Function &F) override {
36 std::string CountingFunctionName =
37 F.getFnAttribute("counting-function").getValueAsString();
38 if (CountingFunctionName.empty())
41 Type *VoidTy = Type::getVoidTy(F.getContext());
42 Constant *CountingFn =
43 F.getParent()->getOrInsertFunction(CountingFunctionName,
45 CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
50 char CountingFunctionInserter::ID = 0;
53 INITIALIZE_PASS(CountingFunctionInserter, "cfinserter",
54 "Inserts calls to mcount-like functions", false, false)
56 //===----------------------------------------------------------------------===//
58 // CountingFunctionInserter - Give any unnamed non-void instructions "tmp" names.
60 FunctionPass *llvm::createCountingFunctionInserterPass() {
61 return new CountingFunctionInserter();