EHContGuardCatchret.cpp 2.5 KB

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