1 //===--- llvm/CodeGen/WasmEHFuncInfo.h --------------------------*- C++ -*-===//
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 // Data structures for Wasm exception handling schemes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_WASMEHFUNCINFO_H
14 #define LLVM_CODEGEN_WASMEHFUNCINFO_H
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/PointerUnion.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 
20 namespace llvm {
21 
22 class BasicBlock;
23 class Function;
24 class MachineBasicBlock;
25 
26 namespace WebAssembly {
27 enum Tag { CPP_EXCEPTION = 0, C_LONGJMP = 1 };
28 }
29 
30 using BBOrMBB = PointerUnion<const BasicBlock *, MachineBasicBlock *>;
31 
32 struct WasmEHFuncInfo {
33   // When there is an entry <A, B>, if an exception is not caught by A, it
34   // should next unwind to the EH pad B.
35   DenseMap<BBOrMBB, BBOrMBB> SrcToUnwindDest;
36   DenseMap<BBOrMBB, SmallPtrSet<BBOrMBB, 4>> UnwindDestToSrcs; // reverse map
37 
38   // Helper functions
39   const BasicBlock *getUnwindDest(const BasicBlock *BB) const {
40     assert(hasUnwindDest(BB));
41     return SrcToUnwindDest.lookup(BB).get<const BasicBlock *>();
42   }
43   SmallPtrSet<const BasicBlock *, 4> getUnwindSrcs(const BasicBlock *BB) const {
44     assert(hasUnwindSrcs(BB));
45     const auto &Set = UnwindDestToSrcs.lookup(BB);
46     SmallPtrSet<const BasicBlock *, 4> Ret;
47     for (const auto P : Set)
48       Ret.insert(P.get<const BasicBlock *>());
49     return Ret;
50   }
51   void setUnwindDest(const BasicBlock *BB, const BasicBlock *Dest) {
52     SrcToUnwindDest[BB] = Dest;
53     UnwindDestToSrcs[Dest].insert(BB);
54   }
55   bool hasUnwindDest(const BasicBlock *BB) const {
56     return SrcToUnwindDest.count(BB);
57   }
58   bool hasUnwindSrcs(const BasicBlock *BB) const {
59     return UnwindDestToSrcs.count(BB);
60   }
61 
62   MachineBasicBlock *getUnwindDest(MachineBasicBlock *MBB) const {
63     assert(hasUnwindDest(MBB));
64     return SrcToUnwindDest.lookup(MBB).get<MachineBasicBlock *>();
65   }
66   SmallPtrSet<MachineBasicBlock *, 4>
67   getUnwindSrcs(MachineBasicBlock *MBB) const {
68     assert(hasUnwindSrcs(MBB));
69     const auto &Set = UnwindDestToSrcs.lookup(MBB);
70     SmallPtrSet<MachineBasicBlock *, 4> Ret;
71     for (const auto P : Set)
72       Ret.insert(P.get<MachineBasicBlock *>());
73     return Ret;
74   }
75   void setUnwindDest(MachineBasicBlock *MBB, MachineBasicBlock *Dest) {
76     SrcToUnwindDest[MBB] = Dest;
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 
87 // Analyze the IR in the given function to build WasmEHFuncInfo.
88 void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo);
89 
90 } // namespace llvm
91 
92 #endif // LLVM_CODEGEN_WASMEHFUNCINFO_H
93