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/ADT/Statistic.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/IR/Module.h"
24#include "llvm/InitializePasses.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "ehcontguard-catchret"
29
30STATISTIC(EHContGuardTargetsFound, "Number of EHCont Guard 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 EHContGuardTargets : public MachineFunctionPass {
37public:
38 static char ID;
39
40 EHContGuardTargets() : MachineFunctionPass(ID) {}
41
42 StringRef getPassName() const override {
43 return "EH Cont Guard catchret targets";
44 }
45
46 bool runOnMachineFunction(MachineFunction &MF) override;
47};
48
49} // end anonymous namespace
50
51char EHContGuardTargets::ID = 0;
52
53INITIALIZE_PASS(EHContGuardTargets, "EHContGuardTargets",
54 "Insert symbols at valid targets for /guard:ehcont", false,
55 false)
56FunctionPass *llvm::createEHContGuardTargetsPass() {
57 return new EHContGuardTargets();
58}
59
60bool EHContGuardTargets::runOnMachineFunction(MachineFunction &MF) {
61
62 // Skip modules for which the ehcontguard flag is not set.
63 if (!MF.getFunction().getParent()->getModuleFlag(Key: "ehcontguard"))
64 return false;
65
66 // Skip functions that do not have targets
67 if (!MF.hasEHContTarget())
68 return false;
69
70 bool Result = false;
71
72 for (MachineBasicBlock &MBB : MF) {
73 if (MBB.isEHContTarget()) {
74 MF.addEHContTarget(Target: MBB.getEHContSymbol());
75 EHContGuardTargetsFound++;
76 Result = true;
77 }
78 }
79
80 return Result;
81}
82