summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/lib/CodeGen/CountingFunctionInserter.cpp
blob: 7f7350f5fb5cd97454e05a0cc1beb7827f9e686d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
      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();
}
OpenPOWER on IntegriCloud