EscapeEnumerator.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===- EscapeEnumerator.cpp -----------------------------------------------===//
  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. // Defines a helper class that enumerates all possible exits from a function,
  10. // including exception handling.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/EscapeEnumerator.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/Analysis/EHPersonalities.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Transforms/Utils/Local.h"
  18. using namespace llvm;
  19. static FunctionCallee getDefaultPersonalityFn(Module *M) {
  20. LLVMContext &C = M->getContext();
  21. Triple T(M->getTargetTriple());
  22. EHPersonality Pers = getDefaultEHPersonality(T);
  23. return M->getOrInsertFunction(getEHPersonalityName(Pers),
  24. FunctionType::get(Type::getInt32Ty(C), true));
  25. }
  26. IRBuilder<> *EscapeEnumerator::Next() {
  27. if (Done)
  28. return nullptr;
  29. // Find all 'return', 'resume', and 'unwind' instructions.
  30. while (StateBB != StateE) {
  31. BasicBlock *CurBB = &*StateBB++;
  32. // Branches and invokes do not escape, only unwind, resume, and return
  33. // do.
  34. Instruction *TI = CurBB->getTerminator();
  35. if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
  36. continue;
  37. if (CallInst *CI = CurBB->getTerminatingMustTailCall())
  38. TI = CI;
  39. Builder.SetInsertPoint(TI);
  40. return &Builder;
  41. }
  42. Done = true;
  43. if (!HandleExceptions)
  44. return nullptr;
  45. if (F.doesNotThrow())
  46. return nullptr;
  47. // Find all 'call' instructions that may throw.
  48. // We cannot tranform calls with musttail tag.
  49. SmallVector<Instruction *, 16> Calls;
  50. for (BasicBlock &BB : F)
  51. for (Instruction &II : BB)
  52. if (CallInst *CI = dyn_cast<CallInst>(&II))
  53. if (!CI->doesNotThrow() && !CI->isMustTailCall())
  54. Calls.push_back(CI);
  55. if (Calls.empty())
  56. return nullptr;
  57. // Create a cleanup block.
  58. LLVMContext &C = F.getContext();
  59. BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
  60. Type *ExnTy = StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C));
  61. if (!F.hasPersonalityFn()) {
  62. FunctionCallee PersFn = getDefaultPersonalityFn(F.getParent());
  63. F.setPersonalityFn(cast<Constant>(PersFn.getCallee()));
  64. }
  65. if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) {
  66. report_fatal_error("Scoped EH not supported");
  67. }
  68. LandingPadInst *LPad =
  69. LandingPadInst::Create(ExnTy, 1, "cleanup.lpad", CleanupBB);
  70. LPad->setCleanup(true);
  71. ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
  72. // Transform the 'call' instructions into 'invoke's branching to the
  73. // cleanup block. Go in reverse order to make prettier BB names.
  74. SmallVector<Value *, 16> Args;
  75. for (unsigned I = Calls.size(); I != 0;) {
  76. CallInst *CI = cast<CallInst>(Calls[--I]);
  77. changeToInvokeAndSplitBasicBlock(CI, CleanupBB, DTU);
  78. }
  79. Builder.SetInsertPoint(RI);
  80. return &Builder;
  81. }