1//===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
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// This transformation is designed for use by code generators which do not yet
10// support stack unwinding. This pass converts 'invoke' instructions to 'call'
11// instructions, so that any exception-handling 'landingpad' blocks become dead
12// code (which can be removed by running the '-simplifycfg' pass afterwards).
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Transforms/Utils/LowerInvoke.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/InitializePasses.h"
20#include "llvm/Pass.h"
21#include "llvm/Transforms/Utils.h"
22#include "llvm/Transforms/Utils/Local.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "lower-invoke"
26
27STATISTIC(NumInvokes, "Number of invokes replaced");
28
29namespace {
30class LowerInvokeLegacyPass : public FunctionPass {
31public:
32 static char ID; // Pass identification, replacement for typeid
33 explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
34 initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
35 }
36 bool runOnFunction(Function &F) override;
37};
38} // namespace
39
40char LowerInvokeLegacyPass::ID = 0;
41INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
42 "Lower invoke and unwind, for unwindless code generators",
43 false, false)
44
45static bool runImpl(Function &F) {
46 bool Changed = false;
47 for (BasicBlock &BB : F)
48 if (InvokeInst *II = dyn_cast<InvokeInst>(Val: BB.getTerminator())) {
49 changeToCall(II);
50 ++NumInvokes;
51 Changed = true;
52 }
53 return Changed;
54}
55
56bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
57 return runImpl(F);
58}
59
60char &llvm::LowerInvokePassID = LowerInvokeLegacyPass::ID;
61
62// Public Interface To the LowerInvoke pass.
63FunctionPass *llvm::createLowerInvokePass() {
64 return new LowerInvokeLegacyPass();
65}
66
67PreservedAnalyses LowerInvokePass::run(Function &F,
68 FunctionAnalysisManager &AM) {
69 bool Changed = runImpl(F);
70 if (!Changed)
71 return PreservedAnalyses::all();
72
73 return PreservedAnalyses::none();
74}
75