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