xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/EHContGuardTargets.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "ehcontguard-catchret"
29 
30 STATISTIC(EHContGuardTargetsFound, "Number of EHCont Guard targets");
31 
32 namespace {
33 
34 /// MachineFunction pass to insert a symbol before each valid catchret target
35 /// and store these in the MachineFunction's CatchRetTargets vector.
36 class EHContGuardTargets : public MachineFunctionPass {
37 public:
38   static char ID;
39 
EHContGuardTargets()40   EHContGuardTargets() : MachineFunctionPass(ID) {
41     initializeEHContGuardTargetsPass(*PassRegistry::getPassRegistry());
42   }
43 
getPassName() const44   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 
53 char EHContGuardTargets::ID = 0;
54 
55 INITIALIZE_PASS(EHContGuardTargets, "EHContGuardTargets",
56                 "Insert symbols at valid targets for /guard:ehcont", false,
57                 false)
createEHContGuardTargetsPass()58 FunctionPass *llvm::createEHContGuardTargetsPass() {
59   return new EHContGuardTargets();
60 }
61 
runOnMachineFunction(MachineFunction & MF)62 bool EHContGuardTargets::runOnMachineFunction(MachineFunction &MF) {
63 
64   // Skip modules for which the ehcontguard flag is not set.
65   if (!MF.getFunction().getParent()->getModuleFlag("ehcontguard"))
66     return false;
67 
68   // Skip functions that do not have targets
69   if (!MF.hasEHContTarget())
70     return false;
71 
72   bool Result = false;
73 
74   for (MachineBasicBlock &MBB : MF) {
75     if (MBB.isEHContTarget()) {
76       MF.addEHContTarget(MBB.getEHContSymbol());
77       EHContGuardTargetsFound++;
78       Result = true;
79     }
80   }
81 
82   return Result;
83 }
84