xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/EHContGuardCatchret.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1*fe6060f1SDimitry Andric //===-- EHContGuardCatchret.cpp - Catchret target symbols -------*- C++ -*-===//
2*fe6060f1SDimitry Andric //
3*fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe6060f1SDimitry Andric //
7*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8*fe6060f1SDimitry Andric ///
9*fe6060f1SDimitry Andric /// \file
10*fe6060f1SDimitry Andric /// This file contains a machine function pass to insert a symbol before each
11*fe6060f1SDimitry Andric /// valid catchret target and store this in the MachineFunction's
12*fe6060f1SDimitry Andric /// CatchRetTargets vector. This will be used to emit the table of valid targets
13*fe6060f1SDimitry Andric /// used by EHCont Guard.
14*fe6060f1SDimitry Andric ///
15*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
16*fe6060f1SDimitry Andric 
17*fe6060f1SDimitry Andric #include "llvm/ADT/Statistic.h"
18*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
19*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
20*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
21*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
22*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
23*fe6060f1SDimitry Andric #include "llvm/CodeGen/Passes.h"
24*fe6060f1SDimitry Andric #include "llvm/InitializePasses.h"
25*fe6060f1SDimitry Andric 
26*fe6060f1SDimitry Andric using namespace llvm;
27*fe6060f1SDimitry Andric 
28*fe6060f1SDimitry Andric #define DEBUG_TYPE "ehcontguard-catchret"
29*fe6060f1SDimitry Andric 
30*fe6060f1SDimitry Andric STATISTIC(EHContGuardCatchretTargets,
31*fe6060f1SDimitry Andric           "Number of EHCont Guard catchret targets");
32*fe6060f1SDimitry Andric 
33*fe6060f1SDimitry Andric namespace {
34*fe6060f1SDimitry Andric 
35*fe6060f1SDimitry Andric /// MachineFunction pass to insert a symbol before each valid catchret target
36*fe6060f1SDimitry Andric /// and store these in the MachineFunction's CatchRetTargets vector.
37*fe6060f1SDimitry Andric class EHContGuardCatchret : public MachineFunctionPass {
38*fe6060f1SDimitry Andric public:
39*fe6060f1SDimitry Andric   static char ID;
40*fe6060f1SDimitry Andric 
41*fe6060f1SDimitry Andric   EHContGuardCatchret() : MachineFunctionPass(ID) {
42*fe6060f1SDimitry Andric     initializeEHContGuardCatchretPass(*PassRegistry::getPassRegistry());
43*fe6060f1SDimitry Andric   }
44*fe6060f1SDimitry Andric 
45*fe6060f1SDimitry Andric   StringRef getPassName() const override {
46*fe6060f1SDimitry Andric     return "EH Cont Guard catchret targets";
47*fe6060f1SDimitry Andric   }
48*fe6060f1SDimitry Andric 
49*fe6060f1SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
50*fe6060f1SDimitry Andric };
51*fe6060f1SDimitry Andric 
52*fe6060f1SDimitry Andric } // end anonymous namespace
53*fe6060f1SDimitry Andric 
54*fe6060f1SDimitry Andric char EHContGuardCatchret::ID = 0;
55*fe6060f1SDimitry Andric 
56*fe6060f1SDimitry Andric INITIALIZE_PASS(EHContGuardCatchret, "EHContGuardCatchret",
57*fe6060f1SDimitry Andric                 "Insert symbols at valid catchret targets for /guard:ehcont",
58*fe6060f1SDimitry Andric                 false, false)
59*fe6060f1SDimitry Andric FunctionPass *llvm::createEHContGuardCatchretPass() {
60*fe6060f1SDimitry Andric   return new EHContGuardCatchret();
61*fe6060f1SDimitry Andric }
62*fe6060f1SDimitry Andric 
63*fe6060f1SDimitry Andric bool EHContGuardCatchret::runOnMachineFunction(MachineFunction &MF) {
64*fe6060f1SDimitry Andric 
65*fe6060f1SDimitry Andric   // Skip modules for which the ehcontguard flag is not set.
66*fe6060f1SDimitry Andric   if (!MF.getMMI().getModule()->getModuleFlag("ehcontguard"))
67*fe6060f1SDimitry Andric     return false;
68*fe6060f1SDimitry Andric 
69*fe6060f1SDimitry Andric   // Skip functions that do not have catchret
70*fe6060f1SDimitry Andric   if (!MF.hasEHCatchret())
71*fe6060f1SDimitry Andric     return false;
72*fe6060f1SDimitry Andric 
73*fe6060f1SDimitry Andric   bool Result = false;
74*fe6060f1SDimitry Andric 
75*fe6060f1SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
76*fe6060f1SDimitry Andric     if (MBB.isEHCatchretTarget()) {
77*fe6060f1SDimitry Andric       MF.addCatchretTarget(MBB.getEHCatchretSymbol());
78*fe6060f1SDimitry Andric       EHContGuardCatchretTargets++;
79*fe6060f1SDimitry Andric       Result = true;
80*fe6060f1SDimitry Andric     }
81*fe6060f1SDimitry Andric   }
82*fe6060f1SDimitry Andric 
83*fe6060f1SDimitry Andric   return Result;
84*fe6060f1SDimitry Andric }
85