1 //===--- llvm/CodeGen/WasmEHFuncInfo.h --------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Data structures for Wasm exception handling schemes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_WASMEHFUNCINFO_H 15 #define LLVM_CODEGEN_WASMEHFUNCINFO_H 16 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/PointerUnion.h" 19 #include "llvm/CodeGen/MachineBasicBlock.h" 20 #include "llvm/IR/BasicBlock.h" 21 22 namespace llvm { 23 24 enum EventTag { CPP_EXCEPTION = 0, C_LONGJMP = 1 }; 25 26 using BBOrMBB = PointerUnion<const BasicBlock *, MachineBasicBlock *>; 27 28 struct WasmEHFuncInfo { 29 // When there is an entry <A, B>, if an exception is not caught by A, it 30 // should next unwind to the EH pad B. 31 DenseMap<BBOrMBB, BBOrMBB> EHPadUnwindMap; 32 // For entry <A, B>, A is a BB with an instruction that may throw 33 // (invoke/cleanupret in LLVM IR, call/rethrow in the backend) and B is an EH 34 // pad that A unwinds to. 35 DenseMap<BBOrMBB, BBOrMBB> ThrowUnwindMap; 36 37 // Helper functions getEHPadUnwindDestWasmEHFuncInfo38 const BasicBlock *getEHPadUnwindDest(const BasicBlock *BB) const { 39 return EHPadUnwindMap.lookup(BB).get<const BasicBlock *>(); 40 } setEHPadUnwindDestWasmEHFuncInfo41 void setEHPadUnwindDest(const BasicBlock *BB, const BasicBlock *Dest) { 42 EHPadUnwindMap[BB] = Dest; 43 } getThrowUnwindDestWasmEHFuncInfo44 const BasicBlock *getThrowUnwindDest(BasicBlock *BB) const { 45 return ThrowUnwindMap.lookup(BB).get<const BasicBlock *>(); 46 } setThrowUnwindDestWasmEHFuncInfo47 void setThrowUnwindDest(const BasicBlock *BB, const BasicBlock *Dest) { 48 ThrowUnwindMap[BB] = Dest; 49 } hasEHPadUnwindDestWasmEHFuncInfo50 bool hasEHPadUnwindDest(const BasicBlock *BB) const { 51 return EHPadUnwindMap.count(BB); 52 } hasThrowUnwindDestWasmEHFuncInfo53 bool hasThrowUnwindDest(const BasicBlock *BB) const { 54 return ThrowUnwindMap.count(BB); 55 } 56 getEHPadUnwindDestWasmEHFuncInfo57 MachineBasicBlock *getEHPadUnwindDest(MachineBasicBlock *MBB) const { 58 return EHPadUnwindMap.lookup(MBB).get<MachineBasicBlock *>(); 59 } setEHPadUnwindDestWasmEHFuncInfo60 void setEHPadUnwindDest(MachineBasicBlock *MBB, MachineBasicBlock *Dest) { 61 EHPadUnwindMap[MBB] = Dest; 62 } getThrowUnwindDestWasmEHFuncInfo63 MachineBasicBlock *getThrowUnwindDest(MachineBasicBlock *MBB) const { 64 return ThrowUnwindMap.lookup(MBB).get<MachineBasicBlock *>(); 65 } setThrowUnwindDestWasmEHFuncInfo66 void setThrowUnwindDest(MachineBasicBlock *MBB, MachineBasicBlock *Dest) { 67 ThrowUnwindMap[MBB] = Dest; 68 } hasEHPadUnwindDestWasmEHFuncInfo69 bool hasEHPadUnwindDest(MachineBasicBlock *MBB) const { 70 return EHPadUnwindMap.count(MBB); 71 } hasThrowUnwindDestWasmEHFuncInfo72 bool hasThrowUnwindDest(MachineBasicBlock *MBB) const { 73 return ThrowUnwindMap.count(MBB); 74 } 75 }; 76 77 // Analyze the IR in the given function to build WasmEHFuncInfo. 78 void calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo); 79 80 } // namespace llvm 81 82 #endif // LLVM_CODEGEN_WASMEHFUNCINFO_H 83