EHContGuardCatchret.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===-- EHContGuardCatchret.cpp - Catchret target symbols -------*- C++ -*-===//
  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. /// \file
  10. /// This file contains a machine function pass to insert a symbol before each
  11. /// valid catchret target and store this in the MachineFunction's
  12. /// CatchRetTargets vector. This will be used to emit the table of valid targets
  13. /// used by EHCont Guard.
  14. ///
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/CodeGen/MachineBasicBlock.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachineModuleInfo.h"
  20. #include "llvm/CodeGen/Passes.h"
  21. #include "llvm/InitializePasses.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "ehcontguard-catchret"
  24. STATISTIC(EHContGuardCatchretTargets,
  25. "Number of EHCont Guard catchret targets");
  26. namespace {
  27. /// MachineFunction pass to insert a symbol before each valid catchret target
  28. /// and store these in the MachineFunction's CatchRetTargets vector.
  29. class EHContGuardCatchret : public MachineFunctionPass {
  30. public:
  31. static char ID;
  32. EHContGuardCatchret() : MachineFunctionPass(ID) {
  33. initializeEHContGuardCatchretPass(*PassRegistry::getPassRegistry());
  34. }
  35. StringRef getPassName() const override {
  36. return "EH Cont Guard catchret targets";
  37. }
  38. bool runOnMachineFunction(MachineFunction &MF) override;
  39. };
  40. } // end anonymous namespace
  41. char EHContGuardCatchret::ID = 0;
  42. INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret",
  43. "Insert symbols at valid catchret targets for /guard:ehcont",
  44. false, false)
  45. FunctionPass *llvm::createEHContGuardCatchretPass() {
  46. return new EHContGuardCatchret();
  47. }
  48. bool EHContGuardCatchret::runOnMachineFunction(MachineFunction &MF) {
  49. // Skip modules for which the ehcontguard flag is not set.
  50. if (!MF.getMMI().getModule()->getModuleFlag("ehcontguard"))
  51. return false;
  52. // Skip functions that do not have catchret
  53. if (!MF.hasEHCatchret())
  54. return false;
  55. bool Result = false;
  56. for (MachineBasicBlock &MBB : MF) {
  57. if (MBB.isEHCatchretTarget()) {
  58. MF.addCatchretTarget(MBB.getEHCatchretSymbol());
  59. EHContGuardCatchretTargets++;
  60. Result = true;
  61. }
  62. }
  63. return Result;
  64. }