blob: 7f7350f5fb5cd97454e05a0cc1beb7827f9e686d [file] [log] [blame]
Hal Finkel40d7f5c2016-09-01 09:42:39 +00001//===- 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"
22using namespace llvm;
23
24namespace {
25 struct CountingFunctionInserter : public FunctionPass {
26 static char ID; // Pass identification, replacement for typeid
27 CountingFunctionInserter() : FunctionPass(ID) {
28 initializeCountingFunctionInserterPass(*PassRegistry::getPassRegistry());
29 }
30
31 void getAnalysisUsage(AnalysisUsage &AU) const override {
32 AU.addPreserved<GlobalsAAWrapperPass>();
33 }
34
35 bool runOnFunction(Function &F) override {
36 std::string CountingFunctionName =
37 F.getFnAttribute("counting-function").getValueAsString();
38 if (CountingFunctionName.empty())
39 return false;
40
41 Type *VoidTy = Type::getVoidTy(F.getContext());
42 Constant *CountingFn =
Mehdi Aminidb11fdf2017-04-06 20:23:57 +000043 F.getParent()->getOrInsertFunction(CountingFunctionName,
Serge Guelton59a2d7b2017-04-11 15:01:18 +000044 VoidTy);
Hal Finkel40d7f5c2016-09-01 09:42:39 +000045 CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
46 return true;
47 }
48 };
49
50 char CountingFunctionInserter::ID = 0;
51}
52
53INITIALIZE_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//
60FunctionPass *llvm::createCountingFunctionInserterPass() {
61 return new CountingFunctionInserter();
62}