LowerInvoke.cpp 3.3 KB

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