LowerInvoke.cpp 3.2 KB

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