EHPersonalities.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===- EHPersonalities.cpp - Compute EH-related information ---------------===//
  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. #include "llvm/Analysis/EHPersonalities.h"
  9. #include "llvm/ADT/StringSwitch.h"
  10. #include "llvm/IR/CFG.h"
  11. #include "llvm/IR/Constants.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. using namespace llvm;
  17. /// See if the given exception handling personality function is one that we
  18. /// understand. If so, return a description of it; otherwise return Unknown.
  19. EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
  20. const GlobalValue *F =
  21. Pers ? dyn_cast<GlobalValue>(Pers->stripPointerCasts()) : nullptr;
  22. if (!F || !F->getValueType() || !F->getValueType()->isFunctionTy())
  23. return EHPersonality::Unknown;
  24. return StringSwitch<EHPersonality>(F->getName())
  25. .Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
  26. .Case("__gxx_personality_v0", EHPersonality::GNU_CXX)
  27. .Case("__gxx_personality_seh0", EHPersonality::GNU_CXX)
  28. .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
  29. .Case("__gcc_personality_v0", EHPersonality::GNU_C)
  30. .Case("__gcc_personality_seh0", EHPersonality::GNU_C)
  31. .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
  32. .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
  33. .Case("_except_handler3", EHPersonality::MSVC_X86SEH)
  34. .Case("_except_handler4", EHPersonality::MSVC_X86SEH)
  35. .Case("__C_specific_handler", EHPersonality::MSVC_TableSEH)
  36. .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
  37. .Case("ProcessCLRException", EHPersonality::CoreCLR)
  38. .Case("rust_eh_personality", EHPersonality::Rust)
  39. .Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX)
  40. .Case("__xlcxx_personality_v1", EHPersonality::XL_CXX)
  41. .Default(EHPersonality::Unknown);
  42. }
  43. StringRef llvm::getEHPersonalityName(EHPersonality Pers) {
  44. switch (Pers) {
  45. case EHPersonality::GNU_Ada: return "__gnat_eh_personality";
  46. case EHPersonality::GNU_CXX: return "__gxx_personality_v0";
  47. case EHPersonality::GNU_CXX_SjLj: return "__gxx_personality_sj0";
  48. case EHPersonality::GNU_C: return "__gcc_personality_v0";
  49. case EHPersonality::GNU_C_SjLj: return "__gcc_personality_sj0";
  50. case EHPersonality::GNU_ObjC: return "__objc_personality_v0";
  51. case EHPersonality::MSVC_X86SEH: return "_except_handler3";
  52. case EHPersonality::MSVC_TableSEH:
  53. return "__C_specific_handler";
  54. case EHPersonality::MSVC_CXX: return "__CxxFrameHandler3";
  55. case EHPersonality::CoreCLR: return "ProcessCLRException";
  56. case EHPersonality::Rust: return "rust_eh_personality";
  57. case EHPersonality::Wasm_CXX: return "__gxx_wasm_personality_v0";
  58. case EHPersonality::XL_CXX:
  59. return "__xlcxx_personality_v1";
  60. case EHPersonality::Unknown: llvm_unreachable("Unknown EHPersonality!");
  61. }
  62. llvm_unreachable("Invalid EHPersonality!");
  63. }
  64. EHPersonality llvm::getDefaultEHPersonality(const Triple &T) {
  65. return EHPersonality::GNU_C;
  66. }
  67. bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
  68. EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
  69. // We can't simplify any invokes to nounwind functions if the personality
  70. // function wants to catch asynch exceptions. The nounwind attribute only
  71. // implies that the function does not throw synchronous exceptions.
  72. return !isAsynchronousEHPersonality(Personality);
  73. }
  74. DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
  75. SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
  76. BasicBlock *EntryBlock = &F.getEntryBlock();
  77. DenseMap<BasicBlock *, ColorVector> BlockColors;
  78. // Build up the color map, which maps each block to its set of 'colors'.
  79. // For any block B the "colors" of B are the set of funclets F (possibly
  80. // including a root "funclet" representing the main function) such that
  81. // F will need to directly contain B or a copy of B (where the term "directly
  82. // contain" is used to distinguish from being "transitively contained" in
  83. // a nested funclet).
  84. //
  85. // Note: Despite not being a funclet in the truest sense, a catchswitch is
  86. // considered to belong to its own funclet for the purposes of coloring.
  87. DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
  88. << F.getName() << "\n");
  89. Worklist.push_back({EntryBlock, EntryBlock});
  90. while (!Worklist.empty()) {
  91. BasicBlock *Visiting;
  92. BasicBlock *Color;
  93. std::tie(Visiting, Color) = Worklist.pop_back_val();
  94. DEBUG_WITH_TYPE("winehprepare-coloring",
  95. dbgs() << "Visiting " << Visiting->getName() << ", "
  96. << Color->getName() << "\n");
  97. Instruction *VisitingHead = Visiting->getFirstNonPHI();
  98. if (VisitingHead->isEHPad()) {
  99. // Mark this funclet head as a member of itself.
  100. Color = Visiting;
  101. }
  102. // Note that this is a member of the given color.
  103. ColorVector &Colors = BlockColors[Visiting];
  104. if (!is_contained(Colors, Color))
  105. Colors.push_back(Color);
  106. else
  107. continue;
  108. DEBUG_WITH_TYPE("winehprepare-coloring",
  109. dbgs() << " Assigned color \'" << Color->getName()
  110. << "\' to block \'" << Visiting->getName()
  111. << "\'.\n");
  112. BasicBlock *SuccColor = Color;
  113. Instruction *Terminator = Visiting->getTerminator();
  114. if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
  115. Value *ParentPad = CatchRet->getCatchSwitchParentPad();
  116. if (isa<ConstantTokenNone>(ParentPad))
  117. SuccColor = EntryBlock;
  118. else
  119. SuccColor = cast<Instruction>(ParentPad)->getParent();
  120. }
  121. for (BasicBlock *Succ : successors(Visiting))
  122. Worklist.push_back({Succ, SuccColor});
  123. }
  124. return BlockColors;
  125. }