WasmEHFuncInfo.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- llvm/CodeGen/WasmEHFuncInfo.h --------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Data structures for Wasm exception handling schemes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_WASMEHFUNCINFO_H
  18. #define LLVM_CODEGEN_WASMEHFUNCINFO_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/PointerUnion.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. namespace llvm {
  23. class BasicBlock;
  24. class Function;
  25. class MachineBasicBlock;
  26. namespace WebAssembly {
  27. enum Tag { CPP_EXCEPTION = 0, C_LONGJMP = 1 };
  28. }
  29. using BBOrMBB = PointerUnion<const BasicBlock *, MachineBasicBlock *>;
  30. struct WasmEHFuncInfo {
  31. // When there is an entry <A, B>, if an exception is not caught by A, it
  32. // should next unwind to the EH pad B.
  33. DenseMap<BBOrMBB, BBOrMBB> SrcToUnwindDest;
  34. DenseMap<BBOrMBB, SmallPtrSet<BBOrMBB, 4>> UnwindDestToSrcs; // reverse map
  35. // Helper functions
  36. const BasicBlock *getUnwindDest(const BasicBlock *BB) const {
  37. assert(hasUnwindDest(BB));
  38. return SrcToUnwindDest.lookup(BB).get<const BasicBlock *>();
  39. }
  40. SmallPtrSet<const BasicBlock *, 4> getUnwindSrcs(const BasicBlock *BB) const {
  41. assert(hasUnwindSrcs(BB));
  42. const auto &Set = UnwindDestToSrcs.lookup(BB);
  43. SmallPtrSet<const BasicBlock *, 4> Ret;
  44. for (const auto P : Set)
  45. Ret.insert(P.get<const BasicBlock *>());
  46. return Ret;
  47. }
  48. void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest) {
  49. SrcToUnwindDest[BB] = Dest;
  50. if (!UnwindDestToSrcs.count(Dest))
  51. UnwindDestToSrcs[Dest] = SmallPtrSet<BBOrMBB, 4>();
  52. UnwindDestToSrcs[Dest].insert(BB);
  53. }
  54. bool hasUnwindDest(const BasicBlock *BB) const {
  55. return SrcToUnwindDest.count(BB);
  56. }
  57. bool hasUnwindSrcs(const BasicBlock *BB) const {
  58. return UnwindDestToSrcs.count(BB);
  59. }
  60. MachineBasicBlock *getUnwindDest(MachineBasicBlock *MBB) const {
  61. assert(hasUnwindDest(MBB));
  62. return SrcToUnwindDest.lookup(MBB).get<MachineBasicBlock *>();
  63. }
  64. SmallPtrSet<MachineBasicBlock *, 4>
  65. getUnwindSrcs(MachineBasicBlock *MBB) const {
  66. assert(hasUnwindSrcs(MBB));
  67. const auto &Set = UnwindDestToSrcs.lookup(MBB);
  68. SmallPtrSet<MachineBasicBlock *, 4> Ret;
  69. for (const auto P : Set)
  70. Ret.insert(P.get<MachineBasicBlock *>());
  71. return Ret;
  72. }
  73. void setUnwindDest(MachineBasicBlock *MBB, MachineBasicBlock *Dest) {
  74. SrcToUnwindDest[MBB] = Dest;
  75. if (!UnwindDestToSrcs.count(Dest))
  76. UnwindDestToSrcs[Dest] = SmallPtrSet<BBOrMBB, 4>();
  77. UnwindDestToSrcs[Dest].insert(MBB);
  78. }
  79. bool hasUnwindDest(MachineBasicBlock *MBB) const {
  80. return SrcToUnwindDest.count(MBB);
  81. }
  82. bool hasUnwindSrcs(MachineBasicBlock *MBB) const {
  83. return UnwindDestToSrcs.count(MBB);
  84. }
  85. };
  86. // Analyze the IR in the given function to build WasmEHFuncInfo.
  87. void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo);
  88. } // namespace llvm
  89. #endif // LLVM_CODEGEN_WASMEHFUNCINFO_H
  90. #ifdef __GNUC__
  91. #pragma GCC diagnostic pop
  92. #endif