FEntryInserter.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. using namespace llvm;
  22. namespace {
  23. struct FEntryInserter : public MachineFunctionPass {
  24. static char ID; // Pass identification, replacement for typeid
  25. FEntryInserter() : MachineFunctionPass(ID) {
  26. initializeFEntryInserterPass(*PassRegistry::getPassRegistry());
  27. }
  28. bool runOnMachineFunction(MachineFunction &F) override;
  29. };
  30. }
  31. bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
  32. const std::string FEntryName =
  33. MF.getFunction().getFnAttribute("fentry-call").getValueAsString();
  34. if (FEntryName != "true")
  35. return false;
  36. auto &FirstMBB = *MF.begin();
  37. auto *TII = MF.getSubtarget().getInstrInfo();
  38. BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
  39. TII->get(TargetOpcode::FENTRY_CALL));
  40. return true;
  41. }
  42. char FEntryInserter::ID = 0;
  43. char &llvm::FEntryInserterID = FEntryInserter::ID;
  44. INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
  45. false)