FEntryInserter.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //===-- FEntryInsertion.cpp - Patchable prologues for LLVM -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file edits function bodies to insert fentry calls.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachineFunction.h"
  13. #include "llvm/CodeGen/MachineFunctionPass.h"
  14. #include "llvm/CodeGen/MachineInstrBuilder.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/CodeGen/TargetFrameLowering.h"
  17. #include "llvm/CodeGen/TargetInstrInfo.h"
  18. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/InitializePasses.h"
  22. using namespace llvm;
  23. namespace {
  24. struct FEntryInserter : public MachineFunctionPass {
  25. static char ID; // Pass identification, replacement for typeid
  26. FEntryInserter() : MachineFunctionPass(ID) {
  27. initializeFEntryInserterPass(*PassRegistry::getPassRegistry());
  28. }
  29. bool runOnMachineFunction(MachineFunction &F) override;
  30. };
  31. }
  32. bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
  33. const std::string FEntryName = std::string(
  34. MF.getFunction().getFnAttribute("fentry-call").getValueAsString());
  35. if (FEntryName != "true")
  36. return false;
  37. auto &FirstMBB = *MF.begin();
  38. auto *TII = MF.getSubtarget().getInstrInfo();
  39. BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
  40. TII->get(TargetOpcode::FENTRY_CALL));
  41. return true;
  42. }
  43. char FEntryInserter::ID = 0;
  44. char &llvm::FEntryInserterID = FEntryInserter::ID;
  45. INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
  46. false)