LLVM 19.0.0git
EHContGuardCatchret.cpp
Go to the documentation of this file.
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
17#include "llvm/ADT/Statistic.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/IR/Module.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "ehcontguard-catchret"
28
29STATISTIC(EHContGuardCatchretTargets,
30 "Number of EHCont Guard catchret targets");
31
32namespace {
33
34/// MachineFunction pass to insert a symbol before each valid catchret target
35/// and store these in the MachineFunction's CatchRetTargets vector.
36class EHContGuardCatchret : public MachineFunctionPass {
37public:
38 static char ID;
39
40 EHContGuardCatchret() : MachineFunctionPass(ID) {
42 }
43
44 StringRef getPassName() const override {
45 return "EH Cont Guard catchret targets";
46 }
47
48 bool runOnMachineFunction(MachineFunction &MF) override;
49};
50
51} // end anonymous namespace
52
53char EHContGuardCatchret::ID = 0;
54
55INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret",
56 "Insert symbols at valid catchret targets for /guard:ehcont",
57 false, false)
59 return new EHContGuardCatchret();
60}
61
62bool EHContGuardCatchret::runOnMachineFunction(MachineFunction &MF) {
63
64 // Skip modules for which the ehcontguard flag is not set.
65 if (!MF.getMMI().getModule()->getModuleFlag("ehcontguard"))
66 return false;
67
68 // Skip functions that do not have catchret
69 if (!MF.hasEHCatchret())
70 return false;
71
72 bool Result = false;
73
74 for (MachineBasicBlock &MBB : MF) {
75 if (MBB.isEHCatchretTarget()) {
76 MF.addCatchretTarget(MBB.getEHCatchretSymbol());
77 EHContGuardCatchretTargets++;
78 Result = true;
79 }
80 }
81
82 return Result;
83}
MachineBasicBlock & MBB
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
MCSymbol * getEHCatchretSymbol() const
Return the EHCatchret Symbol for this basic block.
bool isEHCatchretTarget() const
Returns true if this is a target block of a catchret.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineModuleInfo & getMMI() const
const Module * getModule() const
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:333
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeEHContGuardCatchretPass(PassRegistry &)
FunctionPass * createEHContGuardCatchretPass()
Creates EHContGuard catchret target identification pass.