LLVM  6.0.0svn
CountingFunctionInserter.cpp
Go to the documentation of this file.
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 
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  struct CountingFunctionInserter : public FunctionPass {
26  static char ID; // Pass identification, replacement for typeid
27  CountingFunctionInserter() : FunctionPass(ID) {
29  }
30 
31  void getAnalysisUsage(AnalysisUsage &AU) const override {
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 =
43  F.getParent()->getOrInsertFunction(CountingFunctionName,
44  VoidTy);
45  CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
46  return true;
47  }
48  };
49 
51 }
52 
53 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 //
61  return new CountingFunctionInserter();
62 }
Legacy wrapper pass to provide the GlobalsAAResult object.
INITIALIZE_PASS(CountingFunctionInserter, "cfinserter", "Inserts calls to mcount-like functions", false, false) FunctionPass *llvm
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This is the interface for a simple mod/ref and alias analysis over globals.
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:142
F(f)
static CallInst * Create(Value *Func, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
FunctionPass * createCountingFunctionInserterPass()
Insert mcount-like function calls.
void initializeCountingFunctionInserterPass(PassRegistry &)
iterator begin()
Definition: Function.h:588
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important base class in LLVM.
Definition: Constant.h:42
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
Module.h This file contains the declarations for the Module class.
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:270