WasmEHFuncInfo.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. UnwindDestToSrcs[Dest].insert(BB);
  51. }
  52. bool hasUnwindDest(const BasicBlock *BB) const {
  53. return SrcToUnwindDest.count(BB);
  54. }
  55. bool hasUnwindSrcs(const BasicBlock *BB) const {
  56. return UnwindDestToSrcs.count(BB);
  57. }
  58. MachineBasicBlock *getUnwindDest(MachineBasicBlock *MBB) const {
  59. assert(hasUnwindDest(MBB));
  60. return SrcToUnwindDest.lookup(MBB).get<MachineBasicBlock *>();
  61. }
  62. SmallPtrSet<MachineBasicBlock *, 4>
  63. getUnwindSrcs(MachineBasicBlock *MBB) const {
  64. assert(hasUnwindSrcs(MBB));
  65. const auto &Set = UnwindDestToSrcs.lookup(MBB);
  66. SmallPtrSet<MachineBasicBlock *, 4> Ret;
  67. for (const auto P : Set)
  68. Ret.insert(P.get<MachineBasicBlock *>());
  69. return Ret;
  70. }
  71. void setUnwindDest(MachineBasicBlock *MBB, MachineBasicBlock *Dest) {
  72. SrcToUnwindDest[MBB] = Dest;
  73. UnwindDestToSrcs[Dest].insert(MBB);
  74. }
  75. bool hasUnwindDest(MachineBasicBlock *MBB) const {
  76. return SrcToUnwindDest.count(MBB);
  77. }
  78. bool hasUnwindSrcs(MachineBasicBlock *MBB) const {
  79. return UnwindDestToSrcs.count(MBB);
  80. }
  81. };
  82. // Analyze the IR in the given function to build WasmEHFuncInfo.
  83. void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo);
  84. } // namespace llvm
  85. #endif // LLVM_CODEGEN_WASMEHFUNCINFO_H
  86. #ifdef __GNUC__
  87. #pragma GCC diagnostic pop
  88. #endif