From 648ce3d358560c0f60340fcf28ad2563d213eca2 Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Fri, 6 Jan 2023 13:26:03 -0500 Subject: Cleanup unwind table emission code a bit. This change removes the `tidyLandingPads` function, which previously had a few responsibilities: 1. Dealing with the deletion of an invoke, after MachineFunction lowering. 2. Dealing with the deletion of a landing pad BB, after MachineFunction lowering. 3. Cleaning up the type-id list generated by `MachineFunction::addLandingPad`. Case 3 has been fixed in the generator, and the others are now handled during table emission. This change also removes `MachineFunction`'s `addCatchTypeInfo`, `addFilterTypeInfo`, and `addCleanup` helper fns, as they had a single caller, and being outlined didn't make it simpler. Finally, as calling `tidyLandingPads` was effectively the only thing `DwarfCFIExceptionBase` did, that class has been eliminated. --- llvm/lib/CodeGen/MachineFunction.cpp | 91 ++++++------------------------------ 1 file changed, 14 insertions(+), 77 deletions(-) (limited to 'llvm/lib/CodeGen/MachineFunction.cpp') diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp index a7f0abc50119..fad5981695ab 100644 --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -763,8 +763,10 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { dyn_cast(F.getPersonalityFn()->stripPointerCasts())) getMMI().addPersonality(PF); - if (LPI->isCleanup()) - addCleanup(LandingPad); + // If there's no typeid list specified, then "cleanup" is implicit. + // Otherwise, id 0 is reserved for the cleanup action. + if (LPI->isCleanup() && LPI->getNumClauses() != 0) + LP.TypeIds.push_back(0); // FIXME: New EH - Add the clauses in reverse order. This isn't 100% // correct, but we need to do it this way because of how the DWARF EH @@ -772,23 +774,25 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { for (unsigned I = LPI->getNumClauses(); I != 0; --I) { Value *Val = LPI->getClause(I - 1); if (LPI->isCatch(I - 1)) { - addCatchTypeInfo(LandingPad, - dyn_cast(Val->stripPointerCasts())); + LP.TypeIds.push_back( + getTypeIDFor(dyn_cast(Val->stripPointerCasts()))); } else { // Add filters in a list. auto *CVal = cast(Val); - SmallVector FilterList; + SmallVector FilterList; for (const Use &U : CVal->operands()) - FilterList.push_back(cast(U->stripPointerCasts())); + FilterList.push_back( + getTypeIDFor(cast(U->stripPointerCasts()))); - addFilterTypeInfo(LandingPad, FilterList); + LP.TypeIds.push_back(getFilterIDFor(FilterList)); } } } else if (const auto *CPI = dyn_cast(FirstI)) { for (unsigned I = CPI->arg_size(); I != 0; --I) { - Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); - addCatchTypeInfo(LandingPad, dyn_cast(TypeInfo)); + auto *TypeInfo = + dyn_cast(CPI->getArgOperand(I - 1)->stripPointerCasts()); + LP.TypeIds.push_back(getTypeIDFor(TypeInfo)); } } else { @@ -798,73 +802,6 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { return LandingPadLabel; } -void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, - ArrayRef TyInfo) { - LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); - for (const GlobalValue *GV : llvm::reverse(TyInfo)) - LP.TypeIds.push_back(getTypeIDFor(GV)); -} - -void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, - ArrayRef TyInfo) { - LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); - std::vector IdsInFilter(TyInfo.size()); - for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) - IdsInFilter[I] = getTypeIDFor(TyInfo[I]); - LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); -} - -void MachineFunction::tidyLandingPads(DenseMap *LPMap, - bool TidyIfNoBeginLabels) { - for (unsigned i = 0; i != LandingPads.size(); ) { - LandingPadInfo &LandingPad = LandingPads[i]; - if (LandingPad.LandingPadLabel && - !LandingPad.LandingPadLabel->isDefined() && - (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) - LandingPad.LandingPadLabel = nullptr; - - // Special case: we *should* emit LPs with null LP MBB. This indicates - // "nounwind" case. - if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { - LandingPads.erase(LandingPads.begin() + i); - continue; - } - - if (TidyIfNoBeginLabels) { - for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { - MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; - MCSymbol *EndLabel = LandingPad.EndLabels[j]; - if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && - (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) - continue; - - LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); - LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); - --j; - --e; - } - - // Remove landing pads with no try-ranges. - if (LandingPads[i].BeginLabels.empty()) { - LandingPads.erase(LandingPads.begin() + i); - continue; - } - } - - // If there is no landing pad, ensure that the list of typeids is empty. - // If the only typeid is a cleanup, this is the same as having no typeids. - if (!LandingPad.LandingPadBlock || - (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) - LandingPad.TypeIds.clear(); - ++i; - } -} - -void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { - LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); - LP.TypeIds.push_back(0); -} - void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, ArrayRef Sites) { LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); @@ -878,7 +815,7 @@ unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { return TypeInfos.size(); } -int MachineFunction::getFilterIDFor(std::vector &TyIds) { +int MachineFunction::getFilterIDFor(ArrayRef TyIds) { // If the new filter coincides with the tail of an existing filter, then // re-use the existing filter. Folding filters more than this requires // re-ordering filters and/or their elements - probably not worth it. -- cgit v1.2.3