Hal Finkel | 40d7f5c | 2016-09-01 09:42:39 +0000 | [diff] [blame] | 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 | 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 Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 43 | F.getParent()->getOrInsertFunction(CountingFunctionName, |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 44 | VoidTy); |
Hal Finkel | 40d7f5c | 2016-09-01 09:42:39 +0000 | [diff] [blame] | 45 | CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt()); |
| 46 | return true; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | char CountingFunctionInserter::ID = 0; |
| 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 | // |
| 60 | FunctionPass *llvm::createCountingFunctionInserterPass() { |
| 61 | return new CountingFunctionInserter(); |
| 62 | } |