Line data Source code
1 : //===- CountingFunctionInserter.cpp - Insert mcount-like function calls ---===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // Insert calls to counter functions, such as mcount, intended to be called
11 : // once per function, at the beginning of each function.
12 : //
13 : //===----------------------------------------------------------------------===//
14 :
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"
22 : using namespace llvm;
23 :
24 : namespace {
25 33642 : struct CountingFunctionInserter : public FunctionPass {
26 : static char ID; // Pass identification, replacement for typeid
27 33848 : CountingFunctionInserter() : FunctionPass(ID) {
28 16924 : initializeCountingFunctionInserterPass(*PassRegistry::getPassRegistry());
29 : }
30 :
31 16887 : void getAnalysisUsage(AnalysisUsage &AU) const override {
32 16887 : AU.addPreserved<GlobalsAAWrapperPass>();
33 16887 : }
34 :
35 143178 : bool runOnFunction(Function &F) override {
36 : std::string CountingFunctionName =
37 572712 : F.getFnAttribute("counting-function").getValueAsString();
38 143178 : if (CountingFunctionName.empty())
39 : return false;
40 :
41 5 : Type *VoidTy = Type::getVoidTy(F.getContext());
42 : Constant *CountingFn =
43 15 : F.getParent()->getOrInsertFunction(CountingFunctionName,
44 5 : VoidTy);
45 25 : CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
46 5 : return true;
47 : }
48 : };
49 :
50 : char CountingFunctionInserter::ID = 0;
51 : }
52 :
53 291852 : INITIALIZE_PASS(CountingFunctionInserter, "cfinserter",
54 : "Inserts calls to mcount-like functions", false, false)
55 :
56 : //===----------------------------------------------------------------------===//
57 : //
58 : // CountingFunctionInserter - Give any unnamed non-void instructions "tmp" names.
59 : //
60 16923 : FunctionPass *llvm::createCountingFunctionInserterPass() {
61 33846 : return new CountingFunctionInserter();
62 : }
|