HICONT

llvm_lab2

Mar 29th, 2024
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include "llvm/Passes/PassBuilder.h"
  2. #include "llvm/Passes/PassPlugin.h"
  3. #include "llvm/Support/raw_ostream.h"
  4.  
  5. namespace {
  6. struct InstrumentationPass : llvm::PassInfoMixin<InstrumentationPass> {
  7.   llvm::PreservedAnalyses run(llvm::Function &F,
  8.                               llvm::FunctionAnalysisManager &) {
  9.     llvm::LLVMContext &context = F.getContext();
  10.     llvm::IRBuilder<> builder(context);
  11.     llvm::Module *module = F.getParent();
  12.     llvm::FunctionType *type =
  13.         llvm::FunctionType::get(llvm::Type::getVoidTy(context), false);
  14.     llvm::FunctionCallee func_f =
  15.         (*module).getOrInsertFunction("instrument_start", type);
  16.     llvm::FunctionCallee func_l =
  17.         (*module).getOrInsertFunction("instrument_end", type);
  18.  
  19.     bool foundInstrument_start = false;
  20.     bool foundInstrument_end = false;
  21.  
  22.     for (llvm::BasicBlock &BB : F) {
  23.       for (llvm::Instruction &I : BB) {
  24.         if (auto *callInst = llvm::dyn_cast<llvm::CallInst>(&I)) {
  25.           if (callInst->getCalledFunction()->getName() ==
  26.               func_f.getCallee()->getName()) {
  27.             foundInstrument_start = true;
  28.           } else if (callInst->getCalledFunction()->getName() ==
  29.                      func_l.getCallee()->getName()) {
  30.             foundInstrument_end = true;
  31.           }
  32.         }
  33.       }
  34.       if (foundInstrument_start && foundInstrument_end)
  35.         break;
  36.     }
  37.  
  38.     if (!foundInstrument_start) {
  39.       builder.SetInsertPoint(&F.getEntryBlock().front());
  40.       builder.CreateCall(func_f);
  41.     }
  42.  
  43.     if (!foundInstrument_end) {
  44.       builder.SetInsertPoint(&F.getEntryBlock().back());
  45.       builder.CreateCall(func_l);
  46.     }
  47.  
  48.     return llvm::PreservedAnalyses::all();
  49.   }
  50.  
  51.   static bool isRequired() { return true; }
  52. };
  53. } // namespace
  54.  
  55. extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
  56. llvmGetPassPluginInfo() {
  57.   return {LLVM_PLUGIN_API_VERSION, "instrumentation", "0.1",
  58.           [](llvm::PassBuilder &PB) {
  59.             PB.registerPipelineParsingCallback(
  60.                 [](llvm::StringRef name, llvm::FunctionPassManager &FPM,
  61.                    llvm::ArrayRef<llvm::PassBuilder::PipelineElement>) -> bool {
  62.                   if (name == "instrumentation") {
  63.                     FPM.addPass(InstrumentationPass{});
  64.                     return true;
  65.                   }
  66.                   return false;
  67.                 });
  68.           }};
  69. }
Advertisement
Add Comment
Please, Sign In to add comment