CountingFunctionInserter.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include "llvm/Analysis/GlobalsModRef.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/IR/Type.h"
  20. #include "llvm/Pass.h"
  21. using namespace llvm;
  22. namespace {
  23. struct CountingFunctionInserter : public FunctionPass {
  24. static char ID; // Pass identification, replacement for typeid
  25. CountingFunctionInserter() : FunctionPass(ID) {
  26. initializeCountingFunctionInserterPass(*PassRegistry::getPassRegistry());
  27. }
  28. void getAnalysisUsage(AnalysisUsage &AU) const override {
  29. AU.addPreserved<GlobalsAAWrapperPass>();
  30. }
  31. bool runOnFunction(Function &F) override {
  32. std::string CountingFunctionName =
  33. F.getFnAttribute("counting-function").getValueAsString();
  34. if (CountingFunctionName.empty())
  35. return false;
  36. Type *VoidTy = Type::getVoidTy(F.getContext());
  37. Constant *CountingFn =
  38. F.getParent()->getOrInsertFunction(CountingFunctionName,
  39. VoidTy);
  40. CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
  41. return true;
  42. }
  43. };
  44. char CountingFunctionInserter::ID = 0;
  45. }
  46. INITIALIZE_PASS(CountingFunctionInserter, "cfinserter",
  47. "Inserts calls to mcount-like functions", false, false)
  48. //===----------------------------------------------------------------------===//
  49. //
  50. // CountingFunctionInserter - Give any unnamed non-void instructions "tmp" names.
  51. //
  52. FunctionPass *llvm::createCountingFunctionInserterPass() {
  53. return new CountingFunctionInserter();
  54. }