[WebAssembly] Add new target feature in support of 'extended-const' proposal
[llvm-project.git] / llvm / lib / CodeGen / EHContGuardCatchret.cpp
blobc18532946bf902e5650248af745546c106123dea
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 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineOperand.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/InitializePasses.h"
26 using namespace llvm;
28 #define DEBUG_TYPE "ehcontguard-catchret"
30 STATISTIC(EHContGuardCatchretTargets,
31 "Number of EHCont Guard catchret targets");
33 namespace {
35 /// MachineFunction pass to insert a symbol before each valid catchret target
36 /// and store these in the MachineFunction's CatchRetTargets vector.
37 class EHContGuardCatchret : public MachineFunctionPass {
38 public:
39 static char ID;
41 EHContGuardCatchret() : MachineFunctionPass(ID) {
42 initializeEHContGuardCatchretPass(*PassRegistry::getPassRegistry());
45 StringRef getPassName() const override {
46 return "EH Cont Guard catchret targets";
49 bool runOnMachineFunction(MachineFunction &MF) override;
52 } // end anonymous namespace
54 char EHContGuardCatchret::ID = 0;
56 INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret",
57 "Insert symbols at valid catchret targets for /guard:ehcont",
58 false, false)
59 FunctionPass *llvm::createEHContGuardCatchretPass() {
60 return new EHContGuardCatchret();
63 bool EHContGuardCatchret::runOnMachineFunction(MachineFunction &MF) {
65 // Skip modules for which the ehcontguard flag is not set.
66 if (!MF.getMMI().getModule()->getModuleFlag("ehcontguard"))
67 return false;
69 // Skip functions that do not have catchret
70 if (!MF.hasEHCatchret())
71 return false;
73 bool Result = false;
75 for (MachineBasicBlock &MBB : MF) {
76 if (MBB.isEHCatchretTarget()) {
77 MF.addCatchretTarget(MBB.getEHCatchretSymbol());
78 EHContGuardCatchretTargets++;
79 Result = true;
83 return Result;