LowerInvoke.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This transformation is designed for use by code generators which do not yet
  11. // support stack unwinding. This pass converts 'invoke' instructions to 'call'
  12. // instructions, so that any exception-handling 'landingpad' blocks become dead
  13. // code (which can be removed by running the '-simplifycfg' pass afterwards).
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Scalar.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Support/CommandLine.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "lowerinvoke"
  26. STATISTIC(NumInvokes, "Number of invokes replaced");
  27. namespace {
  28. class LowerInvoke : public FunctionPass {
  29. public:
  30. static char ID; // Pass identification, replacement for typeid
  31. explicit LowerInvoke() : FunctionPass(ID) {
  32. initializeLowerInvokePass(*PassRegistry::getPassRegistry());
  33. }
  34. bool runOnFunction(Function &F) override;
  35. };
  36. }
  37. char LowerInvoke::ID = 0;
  38. INITIALIZE_PASS(LowerInvoke, "lowerinvoke",
  39. "Lower invoke and unwind, for unwindless code generators",
  40. false, false)
  41. char &llvm::LowerInvokePassID = LowerInvoke::ID;
  42. // Public Interface To the LowerInvoke pass.
  43. FunctionPass *llvm::createLowerInvokePass() {
  44. return new LowerInvoke();
  45. }
  46. bool LowerInvoke::runOnFunction(Function &F) {
  47. bool Changed = false;
  48. for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
  49. if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
  50. SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
  51. // Insert a normal call instruction...
  52. CallInst *NewCall = CallInst::Create(II->getCalledValue(),
  53. CallArgs, "", II);
  54. NewCall->takeName(II);
  55. NewCall->setCallingConv(II->getCallingConv());
  56. NewCall->setAttributes(II->getAttributes());
  57. NewCall->setDebugLoc(II->getDebugLoc());
  58. II->replaceAllUsesWith(NewCall);
  59. // Insert an unconditional branch to the normal destination.
  60. BranchInst::Create(II->getNormalDest(), II);
  61. // Remove any PHI node entries from the exception destination.
  62. II->getUnwindDest()->removePredecessor(BB);
  63. // Remove the invoke instruction now.
  64. BB->getInstList().erase(II);
  65. ++NumInvokes; Changed = true;
  66. }
  67. return Changed;
  68. }