diff options
Diffstat (limited to 'llvm/lib/CodeGen/WasmEHPrepare.cpp')
| -rw-r--r-- | llvm/lib/CodeGen/WasmEHPrepare.cpp | 171 |
1 files changed, 46 insertions, 125 deletions
diff --git a/llvm/lib/CodeGen/WasmEHPrepare.cpp b/llvm/lib/CodeGen/WasmEHPrepare.cpp index 53424556682d..c4c84cd921fa 100644 --- a/llvm/lib/CodeGen/WasmEHPrepare.cpp +++ b/llvm/lib/CodeGen/WasmEHPrepare.cpp @@ -77,21 +77,12 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/BreadthFirstIterator.h" -#include "llvm/ADT/SetVector.h" -#include "llvm/ADT/Statistic.h" -#include "llvm/ADT/Triple.h" -#include "llvm/Analysis/DomTreeUpdater.h" -#include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/TargetLowering.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/CodeGen/WasmEHFuncInfo.h" -#include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" -#include "llvm/IR/Intrinsics.h" #include "llvm/IR/IntrinsicsWebAssembly.h" #include "llvm/InitializePasses.h" -#include "llvm/Pass.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" using namespace llvm; @@ -117,19 +108,14 @@ class WasmEHPrepare : public FunctionPass { FunctionCallee CallPersonalityF = nullptr; // _Unwind_CallPersonality() wrapper - bool prepareEHPads(Function &F); bool prepareThrows(Function &F); - - bool IsEHPadFunctionsSetUp = false; - void setupEHPadFunctions(Function &F); - void prepareEHPad(BasicBlock *BB, bool NeedPersonality, bool NeedLSDA = false, - unsigned Index = 0); + bool prepareEHPads(Function &F); + void prepareEHPad(BasicBlock *BB, bool NeedPersonality, unsigned Index = 0); public: static char ID; // Pass identification, replacement for typeid WasmEHPrepare() : FunctionPass(ID) {} - void getAnalysisUsage(AnalysisUsage &AU) const override; bool doInitialization(Module &M) override; bool runOnFunction(Function &F) override; @@ -142,16 +128,11 @@ public: char WasmEHPrepare::ID = 0; INITIALIZE_PASS_BEGIN(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", false, false) -INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_END(WasmEHPrepare, DEBUG_TYPE, "Prepare WebAssembly exceptions", false, false) FunctionPass *llvm::createWasmEHPass() { return new WasmEHPrepare(); } -void WasmEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired<DominatorTreeWrapperPass>(); -} - bool WasmEHPrepare::doInitialization(Module &M) { IRBuilder<> IRB(M.getContext()); LPadContextTy = StructType::get(IRB.getInt32Ty(), // lpad_index @@ -164,19 +145,18 @@ bool WasmEHPrepare::doInitialization(Module &M) { // Erase the specified BBs if the BB does not have any remaining predecessors, // and also all its dead children. template <typename Container> -static void eraseDeadBBsAndChildren(const Container &BBs, DomTreeUpdater *DTU) { +static void eraseDeadBBsAndChildren(const Container &BBs) { SmallVector<BasicBlock *, 8> WL(BBs.begin(), BBs.end()); while (!WL.empty()) { auto *BB = WL.pop_back_val(); if (!pred_empty(BB)) continue; WL.append(succ_begin(BB), succ_end(BB)); - DeleteDeadBlock(BB, DTU); + DeleteDeadBlock(BB); } } bool WasmEHPrepare::runOnFunction(Function &F) { - IsEHPadFunctionsSetUp = false; bool Changed = false; Changed |= prepareThrows(F); Changed |= prepareEHPads(F); @@ -184,9 +164,6 @@ bool WasmEHPrepare::runOnFunction(Function &F) { } bool WasmEHPrepare::prepareThrows(Function &F) { - auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); - DomTreeUpdater DTU(&DT, /*PostDominatorTree*/ nullptr, - DomTreeUpdater::UpdateStrategy::Eager); Module &M = *F.getParent(); IRBuilder<> IRB(F.getContext()); bool Changed = false; @@ -209,102 +186,30 @@ bool WasmEHPrepare::prepareThrows(Function &F) { InstList.erase(std::next(BasicBlock::iterator(ThrowI)), InstList.end()); IRB.SetInsertPoint(BB); IRB.CreateUnreachable(); - eraseDeadBBsAndChildren(Succs, &DTU); + eraseDeadBBsAndChildren(Succs); } return Changed; } bool WasmEHPrepare::prepareEHPads(Function &F) { - auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); - bool Changed = false; + Module &M = *F.getParent(); + IRBuilder<> IRB(F.getContext()); - // There are two things to decide: whether we need a personality function call - // and whether we need a `wasm.lsda()` call and its store. - // - // For the personality function call, catchpads with `catch (...)` and - // cleanuppads don't need it, because exceptions are always caught. Others all - // need it. - // - // For `wasm.lsda()` and its store, in order to minimize the number of them, - // we need a way to figure out whether we have encountered `wasm.lsda()` call - // in any of EH pads that dominates the current EH pad. To figure that out, we - // now visit EH pads in BFS order in the dominator tree so that we visit - // parent BBs first before visiting its child BBs in the domtree. - // - // We keep a set named `ExecutedLSDA`, which basically means "Do we have - // `wasm.lsda() either in the current EH pad or any of its parent EH pads in - // the dominator tree?". This is to prevent scanning the domtree up to the - // root every time we examine an EH pad, in the worst case: each EH pad only - // needs to check its immediate parent EH pad. - // - // - If any of its parent EH pads in the domtree has `wasm.lsda`, this means - // we don't need `wasm.lsda()` in the current EH pad. We also insert the - // current EH pad in `ExecutedLSDA` set. - // - If none of its parent EH pad has `wasm.lsda()`, - // - If the current EH pad is a `catch (...)` or a cleanuppad, done. - // - If the current EH pad is neither a `catch (...)` nor a cleanuppad, - // add `wasm.lsda()` and the store in the current EH pad, and add the - // current EH pad to `ExecutedLSDA` set. - // - // TODO Can we not store LSDA address in user function but make libcxxabi - // compute it? - DenseSet<Value *> ExecutedLSDA; - unsigned Index = 0; - for (auto DomNode : breadth_first(&DT)) { - auto *BB = DomNode->getBlock(); - auto *Pad = BB->getFirstNonPHI(); - if (!Pad || (!isa<CatchPadInst>(Pad) && !isa<CleanupPadInst>(Pad))) + SmallVector<BasicBlock *, 16> CatchPads; + SmallVector<BasicBlock *, 16> CleanupPads; + for (BasicBlock &BB : F) { + if (!BB.isEHPad()) continue; - Changed = true; - - Value *ParentPad = nullptr; - if (CatchPadInst *CPI = dyn_cast<CatchPadInst>(Pad)) { - ParentPad = CPI->getCatchSwitch()->getParentPad(); - if (ExecutedLSDA.count(ParentPad)) { - ExecutedLSDA.insert(CPI); - // We insert its associated catchswitch too, because - // FuncletPadInst::getParentPad() returns a CatchSwitchInst if the child - // FuncletPadInst is a CleanupPadInst. - ExecutedLSDA.insert(CPI->getCatchSwitch()); - } - } else { // CleanupPadInst - ParentPad = cast<CleanupPadInst>(Pad)->getParentPad(); - if (ExecutedLSDA.count(ParentPad)) - ExecutedLSDA.insert(Pad); - } - - if (CatchPadInst *CPI = dyn_cast<CatchPadInst>(Pad)) { - if (CPI->getNumArgOperands() == 1 && - cast<Constant>(CPI->getArgOperand(0))->isNullValue()) - // In case of a single catch (...), we need neither personality call nor - // wasm.lsda() call - prepareEHPad(BB, false); - else { - if (ExecutedLSDA.count(CPI)) - // catch (type), but one of parents already has wasm.lsda() call - prepareEHPad(BB, true, false, Index++); - else { - // catch (type), and none of parents has wasm.lsda() call. We have to - // add the call in this EH pad, and record this EH pad in - // ExecutedLSDA. - ExecutedLSDA.insert(CPI); - ExecutedLSDA.insert(CPI->getCatchSwitch()); - prepareEHPad(BB, true, true, Index++); - } - } - } else if (isa<CleanupPadInst>(Pad)) { - // Cleanup pads need neither personality call nor wasm.lsda() call - prepareEHPad(BB, false); - } + auto *Pad = BB.getFirstNonPHI(); + if (isa<CatchPadInst>(Pad)) + CatchPads.push_back(&BB); + else if (isa<CleanupPadInst>(Pad)) + CleanupPads.push_back(&BB); } + if (CatchPads.empty() && CleanupPads.empty()) + return false; - return Changed; -} - -void WasmEHPrepare::setupEHPadFunctions(Function &F) { - Module &M = *F.getParent(); - IRBuilder<> IRB(F.getContext()); assert(F.hasPersonalityFn() && "Personality function not found"); // __wasm_lpad_context global variable @@ -336,16 +241,30 @@ void WasmEHPrepare::setupEHPadFunctions(Function &F) { "_Unwind_CallPersonality", IRB.getInt32Ty(), IRB.getInt8PtrTy()); if (Function *F = dyn_cast<Function>(CallPersonalityF.getCallee())) F->setDoesNotThrow(); + + unsigned Index = 0; + for (auto *BB : CatchPads) { + auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI()); + // In case of a single catch (...), we don't need to emit a personalify + // function call + if (CPI->getNumArgOperands() == 1 && + cast<Constant>(CPI->getArgOperand(0))->isNullValue()) + prepareEHPad(BB, false); + else + prepareEHPad(BB, true, Index++); + } + + // Cleanup pads don't need a personality function call. + for (auto *BB : CleanupPads) + prepareEHPad(BB, false); + + return true; } // Prepare an EH pad for Wasm EH handling. If NeedPersonality is false, Index is // ignored. void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality, - bool NeedLSDA, unsigned Index) { - if (!IsEHPadFunctionsSetUp) { - IsEHPadFunctionsSetUp = true; - setupEHPadFunctions(*BB->getParent()); - } + unsigned Index) { assert(BB->isEHPad() && "BB is not an EHPad!"); IRBuilder<> IRB(BB->getContext()); IRB.SetInsertPoint(&*BB->getFirstInsertionPt()); @@ -361,8 +280,8 @@ void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality, } } - // Cleanup pads w/o __clang_call_terminate call do not have any of - // wasm.get.exception() or wasm.get.ehselector() calls. We need to do nothing. + // Cleanup pads do not have any of wasm.get.exception() or + // wasm.get.ehselector() calls. We need to do nothing. if (!GetExnCI) { assert(!GetSelectorCI && "wasm.get.ehselector() cannot exist w/o wasm.get.exception()"); @@ -399,9 +318,11 @@ void WasmEHPrepare::prepareEHPad(BasicBlock *BB, bool NeedPersonality, IRB.CreateStore(IRB.getInt32(Index), LPadIndexField); auto *CPI = cast<CatchPadInst>(FPI); - if (NeedLSDA) - // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda(); - IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField); + // TODO Sometimes storing the LSDA address every time is not necessary, in + // case it is already set in a dominating EH pad and there is no function call + // between from that EH pad to here. Consider optimizing those cases. + // Pseudocode: __wasm_lpad_context.lsda = wasm.lsda(); + IRB.CreateStore(IRB.CreateCall(LSDAF), LSDAField); // Pseudocode: _Unwind_CallPersonality(exn); CallInst *PersCI = IRB.CreateCall(CallPersonalityF, CatchCI, @@ -436,9 +357,9 @@ void llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) { const Instruction *UnwindPad = UnwindBB->getFirstNonPHI(); if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad)) // Currently there should be only one handler per a catchswitch. - EHInfo.setEHPadUnwindDest(&BB, *CatchSwitch->handlers().begin()); + EHInfo.setUnwindDest(&BB, *CatchSwitch->handlers().begin()); else // cleanuppad - EHInfo.setEHPadUnwindDest(&BB, UnwindBB); + EHInfo.setUnwindDest(&BB, UnwindBB); } } } |
