[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / Transforms / Utils / LowerInvoke.cpp
blob59cfa41fb7fd2f7fadc8da7a80609289d915cc5b
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).
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/LowerInvoke.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Utils.h"
23 using namespace llvm;
25 #define DEBUG_TYPE "lowerinvoke"
27 STATISTIC(NumInvokes, "Number of invokes replaced");
29 namespace {
30 class LowerInvokeLegacyPass : public FunctionPass {
31 public:
32 static char ID; // Pass identification, replacement for typeid
33 explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
34 initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
36 bool runOnFunction(Function &F) override;
40 char LowerInvokeLegacyPass::ID = 0;
41 INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
42 "Lower invoke and unwind, for unwindless code generators",
43 false, false)
45 static bool runImpl(Function &F) {
46 bool Changed = false;
47 for (BasicBlock &BB : F)
48 if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
49 SmallVector<Value *, 16> CallArgs(II->args());
50 SmallVector<OperandBundleDef, 1> OpBundles;
51 II->getOperandBundlesAsDefs(OpBundles);
52 // Insert a normal call instruction...
53 CallInst *NewCall =
54 CallInst::Create(II->getFunctionType(), II->getCalledOperand(),
55 CallArgs, OpBundles, "", II);
56 NewCall->takeName(II);
57 NewCall->setCallingConv(II->getCallingConv());
58 NewCall->setAttributes(II->getAttributes());
59 NewCall->setDebugLoc(II->getDebugLoc());
60 II->replaceAllUsesWith(NewCall);
62 // Insert an unconditional branch to the normal destination.
63 BranchInst::Create(II->getNormalDest(), II);
65 // Remove any PHI node entries from the exception destination.
66 II->getUnwindDest()->removePredecessor(&BB);
68 // Remove the invoke instruction now.
69 BB.getInstList().erase(II);
71 ++NumInvokes;
72 Changed = true;
74 return Changed;
77 bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
78 return runImpl(F);
81 namespace llvm {
82 char &LowerInvokePassID = LowerInvokeLegacyPass::ID;
84 // Public Interface To the LowerInvoke pass.
85 FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
87 PreservedAnalyses LowerInvokePass::run(Function &F,
88 FunctionAnalysisManager &AM) {
89 bool Changed = runImpl(F);
90 if (!Changed)
91 return PreservedAnalyses::all();
93 return PreservedAnalyses::none();