1//===-- EHContGuardTargets.cpp - EH continuation 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 target where the unwinder in Windows may continue exectution after an
12/// exception is thrown and store this in the MachineFunction's EHContTargets
13/// vector. This will be used to emit the table of valid targets used by Windows
14/// EH Continuation Guard.
15///
16//===----------------------------------------------------------------------===//
17
18#include "llvm/CodeGen/EHContGuardTargets.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/Passes.h"
24#include "llvm/IR/Module.h"
25#include "llvm/InitializePasses.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "ehcontguard-catchret"
30
31STATISTIC(EHContGuardTargetsFound, "Number of EHCont Guard targets");
32
33static bool runEHContGuardTargets(MachineFunction &MF) {
34 // Skip modules for which the ehcontguard flag is not set.
35 if (!MF.getFunction().getParent()->getModuleFlag(Key: "ehcontguard"))
36 return false;
37
38 // Skip functions that do not have targets
39 if (!MF.hasEHContTarget())
40 return false;
41
42 bool Result = false;
43
44 for (MachineBasicBlock &MBB : MF) {
45 if (MBB.isEHContTarget()) {
46 MF.addEHContTarget(Target: MBB.getEHContSymbol());
47 EHContGuardTargetsFound++;
48 Result = true;
49 }
50 }
51
52 return Result;
53}
54
55namespace {
56
57/// MachineFunction pass to insert a symbol before each valid catchret target
58/// and store these in the MachineFunction's CatchRetTargets vector.
59class EHContGuardTargetsLegacy : public MachineFunctionPass {
60public:
61 static char ID;
62
63 EHContGuardTargetsLegacy() : MachineFunctionPass(ID) {}
64
65 StringRef getPassName() const override {
66 return "EH Cont Guard catchret targets";
67 }
68
69 void getAnalysisUsage(AnalysisUsage &AU) const override {
70 AU.setPreservesCFG();
71 MachineFunctionPass::getAnalysisUsage(AU);
72 }
73
74 bool runOnMachineFunction(MachineFunction &MF) override {
75 return runEHContGuardTargets(MF);
76 }
77};
78
79} // end anonymous namespace
80
81char EHContGuardTargetsLegacy::ID = 0;
82
83INITIALIZE_PASS(EHContGuardTargetsLegacy, "EHContGuardTargets",
84 "Insert symbols at valid targets for /guard:ehcont", false,
85 false)
86FunctionPass *llvm::createEHContGuardTargetsLegacy() {
87 return new EHContGuardTargetsLegacy();
88}
89
90PreservedAnalyses
91EHContGuardTargetsPass::run(MachineFunction &MF,
92 MachineFunctionAnalysisManager &MFAM) {
93 if (!runEHContGuardTargets(MF))
94 return PreservedAnalyses::all();
95
96 return getMachineFunctionPassPreservedAnalyses().preserveSet<CFGAnalyses>();
97}
98