7777//
7878// ===----------------------------------------------------------------------===//
7979
80+ #include " llvm/CodeGen/WasmEHPrepare.h"
8081#include " llvm/CodeGen/MachineBasicBlock.h"
8182#include " llvm/CodeGen/Passes.h"
8283#include " llvm/CodeGen/WasmEHFuncInfo.h"
8889
8990using namespace llvm ;
9091
91- #define DEBUG_TYPE " wasmehprepare "
92+ #define DEBUG_TYPE " wasm-eh-prepare "
9293
9394namespace {
94- class WasmEHPrepare : public FunctionPass {
95+ class WasmEHPrepareImpl {
96+ friend class WasmEHPrepare ;
97+
9598 Type *LPadContextTy = nullptr ; // type of 'struct _Unwind_LandingPadContext'
9699 GlobalVariable *LPadContextGV = nullptr ; // __wasm_lpad_context
97100
@@ -113,19 +116,41 @@ class WasmEHPrepare : public FunctionPass {
113116 bool prepareEHPads (Function &F);
114117 void prepareEHPad (BasicBlock *BB, bool NeedPersonality, unsigned Index = 0 );
115118
119+ public:
120+ WasmEHPrepareImpl () = default ;
121+ WasmEHPrepareImpl (Type *LPadContextTy_) : LPadContextTy(LPadContextTy_) {}
122+ bool runOnFunction (Function &F);
123+ };
124+
125+ class WasmEHPrepare : public FunctionPass {
126+ WasmEHPrepareImpl P;
127+
116128public:
117129 static char ID; // Pass identification, replacement for typeid
118130
119131 WasmEHPrepare () : FunctionPass(ID) {}
120132 bool doInitialization (Module &M) override ;
121- bool runOnFunction (Function &F) override ;
133+ bool runOnFunction (Function &F) override { return P. runOnFunction (F); }
122134
123135 StringRef getPassName () const override {
124136 return " WebAssembly Exception handling preparation" ;
125137 }
126138};
139+
127140} // end anonymous namespace
128141
142+ PreservedAnalyses WasmEHPreparePass::run (Function &F,
143+ FunctionAnalysisManager &) {
144+ auto &Context = F.getContext ();
145+ auto *I32Ty = Type::getInt32Ty (Context);
146+ auto *PtrTy = PointerType::get (Context, 0 );
147+ auto *LPadContextTy =
148+ StructType::get (I32Ty /* lpad_index*/ , PtrTy /* lsda*/ , I32Ty /* selector*/ );
149+ WasmEHPrepareImpl P (LPadContextTy);
150+ bool Changed = P.runOnFunction (F);
151+ return Changed ? PreservedAnalyses::none () : PreservedAnalyses ::all ();
152+ }
153+
129154char WasmEHPrepare::ID = 0 ;
130155INITIALIZE_PASS_BEGIN (WasmEHPrepare, DEBUG_TYPE,
131156 " Prepare WebAssembly exceptions" , false , false )
@@ -136,9 +161,9 @@ FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); }
136161
137162bool WasmEHPrepare::doInitialization (Module &M) {
138163 IRBuilder<> IRB (M.getContext ());
139- LPadContextTy = StructType::get (IRB.getInt32Ty (), // lpad_index
140- IRB.getPtrTy (), // lsda
141- IRB.getInt32Ty () // selector
164+ P. LPadContextTy = StructType::get (IRB.getInt32Ty (), // lpad_index
165+ IRB.getPtrTy (), // lsda
166+ IRB.getInt32Ty () // selector
142167 );
143168 return false ;
144169}
@@ -157,14 +182,14 @@ static void eraseDeadBBsAndChildren(const Container &BBs) {
157182 }
158183}
159184
160- bool WasmEHPrepare ::runOnFunction (Function &F) {
185+ bool WasmEHPrepareImpl ::runOnFunction (Function &F) {
161186 bool Changed = false ;
162187 Changed |= prepareThrows (F);
163188 Changed |= prepareEHPads (F);
164189 return Changed;
165190}
166191
167- bool WasmEHPrepare ::prepareThrows (Function &F) {
192+ bool WasmEHPrepareImpl ::prepareThrows (Function &F) {
168193 Module &M = *F.getParent ();
169194 IRBuilder<> IRB (F.getContext ());
170195 bool Changed = false ;
@@ -192,7 +217,7 @@ bool WasmEHPrepare::prepareThrows(Function &F) {
192217 return Changed;
193218}
194219
195- bool WasmEHPrepare ::prepareEHPads (Function &F) {
220+ bool WasmEHPrepareImpl ::prepareEHPads (Function &F) {
196221 Module &M = *F.getParent ();
197222 IRBuilder<> IRB (F.getContext ());
198223
@@ -275,8 +300,8 @@ bool WasmEHPrepare::prepareEHPads(Function &F) {
275300
276301// Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is
277302// ignored.
278- void WasmEHPrepare ::prepareEHPad (BasicBlock *BB, bool NeedPersonality,
279- unsigned Index) {
303+ void WasmEHPrepareImpl ::prepareEHPad (BasicBlock *BB, bool NeedPersonality,
304+ unsigned Index) {
280305 assert (BB->isEHPad () && " BB is not an EHPad!" );
281306 IRBuilder<> IRB (BB->getContext ());
282307 IRB.SetInsertPoint (BB, BB->getFirstInsertionPt ());
0 commit comments