Skip to content

Commit 648ce3d

Browse files
committed
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.
1 parent 1ae36b1 commit 648ce3d

10 files changed

+46
-154
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,6 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
11051105
/// Find or create an LandingPadInfo for the specified MachineBasicBlock.
11061106
LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
11071107

1108-
/// Remap landing pad labels and remove any deleted landing pads.
1109-
void tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap = nullptr,
1110-
bool TidyIfNoBeginLabels = true);
1111-
11121108
/// Return a reference to the landing pad info for the current function.
11131109
const std::vector<LandingPadInfo> &getLandingPads() const {
11141110
return LandingPads;
@@ -1124,22 +1120,11 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
11241120
/// entry.
11251121
MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
11261122

1127-
/// Provide the catch typeinfo for a landing pad.
1128-
void addCatchTypeInfo(MachineBasicBlock *LandingPad,
1129-
ArrayRef<const GlobalValue *> TyInfo);
1130-
1131-
/// Provide the filter typeinfo for a landing pad.
1132-
void addFilterTypeInfo(MachineBasicBlock *LandingPad,
1133-
ArrayRef<const GlobalValue *> TyInfo);
1134-
1135-
/// Add a cleanup action for a landing pad.
1136-
void addCleanup(MachineBasicBlock *LandingPad);
1137-
11381123
/// Return the type id for the specified typeinfo. This is function wide.
11391124
unsigned getTypeIDFor(const GlobalValue *TI);
11401125

11411126
/// Return the id of the filter encoded by TyIds. This is function wide.
1142-
int getFilterIDFor(std::vector<unsigned> &TyIds);
1127+
int getFilterIDFor(ArrayRef<unsigned> TyIds);
11431128

11441129
/// Map the landing pad's EH symbol to the call site indexes.
11451130
void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);

llvm/lib/CodeGen/AsmPrinter/AIXException.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@
2121

2222
namespace llvm {
2323

24-
AIXException::AIXException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
25-
26-
// This overrides 'DwarfCFIExceptionBase::markFunctionEnd', to avoid the call to
27-
// tidyLandingPads. This is necessary, because the
28-
// 'PPCAIXAsmPrinter::emitFunctionBodyEnd' function already checked whether we
29-
// need ehinfo, and emitted a traceback table with the bits set to indicate that
30-
// we will be emitting it, if so. Thus, if we remove it now -- so late in the
31-
// process -- we'll end up having emitted a reference to __ehinfo.N symbol, but
32-
// not emitting a definition for said symbol.
33-
void AIXException::markFunctionEnd() {}
24+
AIXException::AIXException(AsmPrinter *A) : EHStreamer(A) {}
3425

3526
void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
3627
const MCSymbol *PerSym) {

llvm/lib/CodeGen/AsmPrinter/ARMException.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "llvm/MC/MCStreamer.h"
2020
using namespace llvm;
2121

22-
ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
22+
ARMException::ARMException(AsmPrinter *A) : EHStreamer(A) {}
2323

2424
ARMException::~ARMException() = default;
2525

@@ -51,7 +51,6 @@ void ARMException::beginFunction(const MachineFunction *MF) {
5151
void ARMException::markFunctionEnd() {
5252
if (shouldEmitCFI)
5353
Asm->OutStreamer->emitCFIEndProc();
54-
DwarfCFIExceptionBase::markFunctionEnd();
5554
}
5655

5756
/// endFunction - Gather and emit post-function exception information.

llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,7 @@
2323
#include "llvm/Target/TargetOptions.h"
2424
using namespace llvm;
2525

26-
DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A) : EHStreamer(A) {}
27-
28-
void DwarfCFIExceptionBase::markFunctionEnd() {
29-
// Map all labels and get rid of any dead landing pads.
30-
if (!Asm->MF->getLandingPads().empty()) {
31-
MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
32-
NonConstMF->tidyLandingPads();
33-
}
34-
}
35-
36-
DwarfCFIException::DwarfCFIException(AsmPrinter *A)
37-
: DwarfCFIExceptionBase(A) {}
26+
DwarfCFIException::DwarfCFIException(AsmPrinter *A) : EHStreamer(A) {}
3827

3928
DwarfCFIException::~DwarfCFIException() = default;
4029

llvm/lib/CodeGen/AsmPrinter/DwarfException.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,7 @@ namespace llvm {
2121
class MachineFunction;
2222
class ARMTargetStreamer;
2323

24-
class LLVM_LIBRARY_VISIBILITY DwarfCFIExceptionBase : public EHStreamer {
25-
protected:
26-
DwarfCFIExceptionBase(AsmPrinter *A);
27-
28-
/// Per-function flag to indicate if frame CFI info should be emitted.
29-
bool shouldEmitCFI = false;
30-
/// Per-module flag to indicate if .cfi_section has beeen emitted.
31-
bool hasEmittedCFISections = false;
32-
33-
void markFunctionEnd() override;
34-
};
35-
36-
class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public DwarfCFIExceptionBase {
24+
class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public EHStreamer {
3725
/// Per-function flag to indicate if .cfi_personality should be emitted.
3826
bool shouldEmitPersonality = false;
3927

@@ -43,6 +31,12 @@ class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public DwarfCFIExceptionBase {
4331
/// Per-function flag to indicate if .cfi_lsda should be emitted.
4432
bool shouldEmitLSDA = false;
4533

34+
/// Per-function flag to indicate if frame CFI info should be emitted.
35+
bool shouldEmitCFI = false;
36+
37+
/// Per-module flag to indicate if .cfi_section has beeen emitted.
38+
bool hasEmittedCFISections = false;
39+
4640
public:
4741
//===--------------------------------------------------------------------===//
4842
// Main entry points.
@@ -64,7 +58,13 @@ class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public DwarfCFIExceptionBase {
6458
void endBasicBlockSection(const MachineBasicBlock &MBB) override;
6559
};
6660

67-
class LLVM_LIBRARY_VISIBILITY ARMException : public DwarfCFIExceptionBase {
61+
class LLVM_LIBRARY_VISIBILITY ARMException : public EHStreamer {
62+
/// Per-function flag to indicate if frame CFI info should be emitted.
63+
bool shouldEmitCFI = false;
64+
65+
/// Per-module flag to indicate if .cfi_section has beeen emitted.
66+
bool hasEmittedCFISections = false;
67+
6868
void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) override;
6969
ARMTargetStreamer &getTargetStreamer();
7070

@@ -88,16 +88,14 @@ class LLVM_LIBRARY_VISIBILITY ARMException : public DwarfCFIExceptionBase {
8888
void markFunctionEnd() override;
8989
};
9090

91-
class LLVM_LIBRARY_VISIBILITY AIXException : public DwarfCFIExceptionBase {
91+
class LLVM_LIBRARY_VISIBILITY AIXException : public EHStreamer {
9292
/// This is AIX's compat unwind section, which unwinder would use
9393
/// to find the location of LSDA area and personality rountine.
9494
void emitExceptionInfoTable(const MCSymbol *LSDA, const MCSymbol *PerSym);
9595

9696
public:
9797
AIXException(AsmPrinter *A);
9898

99-
void markFunctionEnd() override;
100-
10199
void endModule() override {}
102100
void beginFunction(const MachineFunction *MF) override {}
103101
void endFunction(const MachineFunction *MF) override;

llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ void EHStreamer::computePadMap(
195195
const LandingPadInfo *LandingPad = LandingPads[i];
196196
for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
197197
MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
198+
MCSymbol *EndLabel = LandingPad->BeginLabels[j];
199+
// If we have deleted the code for a given invoke after registering it in
200+
// the LandingPad label list, the associated symbols will not have been
201+
// emitted. In that case, ignore this callsite entry.
202+
if (!BeginLabel->isDefined() || !EndLabel->isDefined())
203+
continue;
198204
assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
199205
PadRange P = { i, j };
200206
PadMap[BeginLabel] = P;
@@ -383,8 +389,14 @@ MCSymbol *EHStreamer::emitExceptionTable() {
383389
SmallVector<const LandingPadInfo *, 64> LandingPads;
384390
LandingPads.reserve(PadInfos.size());
385391

386-
for (const LandingPadInfo &LPI : PadInfos)
392+
for (const LandingPadInfo &LPI : PadInfos) {
393+
// If a landing-pad has an associated label, but the label wasn't ever
394+
// emitted, then skip it. (This can occur if the landingpad's MBB was
395+
// deleted).
396+
if (LPI.LandingPadLabel && !LPI.LandingPadLabel->isDefined())
397+
continue;
387398
LandingPads.push_back(&LPI);
399+
}
388400

389401
// Order landing pads lexicographically by type id.
390402
llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) {

llvm/lib/CodeGen/AsmPrinter/WasmException.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ void WasmException::endModule() {
4242
}
4343
}
4444

45-
void WasmException::markFunctionEnd() {
46-
// Get rid of any dead landing pads.
47-
if (!Asm->MF->getLandingPads().empty()) {
48-
auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
49-
// Wasm does not set BeginLabel and EndLabel information for landing pads,
50-
// so we should set the second argument false.
51-
NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
52-
}
53-
}
54-
5545
void WasmException::endFunction(const MachineFunction *MF) {
5646
bool ShouldEmitExceptionTable = false;
5747
for (const LandingPadInfo &Info : MF->getLandingPads()) {

llvm/lib/CodeGen/AsmPrinter/WasmException.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class LLVM_LIBRARY_VISIBILITY WasmException : public EHStreamer {
2828

2929
void endModule() override;
3030
void beginFunction(const MachineFunction *MF) override {}
31-
void markFunctionEnd() override;
3231
void endFunction(const MachineFunction *MF) override;
3332

3433
protected:

llvm/lib/CodeGen/AsmPrinter/WinException.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,6 @@ void WinException::endFunction(const MachineFunction *MF) {
130130
if (F.hasPersonalityFn())
131131
Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
132132

133-
// Get rid of any dead landing pads if we're not using funclets. In funclet
134-
// schemes, the landing pad is not actually reachable. It only exists so
135-
// that we can emit the right table data.
136-
if (!isFuncletEHPersonality(Per)) {
137-
MachineFunction *NonConstMF = const_cast<MachineFunction*>(MF);
138-
NonConstMF->tidyLandingPads();
139-
}
140-
141133
endFuncletImpl();
142134

143135
// endFunclet will emit the necessary .xdata tables for table-based SEH.

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -763,32 +763,36 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
763763
dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
764764
getMMI().addPersonality(PF);
765765

766-
if (LPI->isCleanup())
767-
addCleanup(LandingPad);
766+
// If there's no typeid list specified, then "cleanup" is implicit.
767+
// Otherwise, id 0 is reserved for the cleanup action.
768+
if (LPI->isCleanup() && LPI->getNumClauses() != 0)
769+
LP.TypeIds.push_back(0);
768770

769771
// FIXME: New EH - Add the clauses in reverse order. This isn't 100%
770772
// correct, but we need to do it this way because of how the DWARF EH
771773
// emitter processes the clauses.
772774
for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
773775
Value *Val = LPI->getClause(I - 1);
774776
if (LPI->isCatch(I - 1)) {
775-
addCatchTypeInfo(LandingPad,
776-
dyn_cast<GlobalValue>(Val->stripPointerCasts()));
777+
LP.TypeIds.push_back(
778+
getTypeIDFor(dyn_cast<GlobalValue>(Val->stripPointerCasts())));
777779
} else {
778780
// Add filters in a list.
779781
auto *CVal = cast<Constant>(Val);
780-
SmallVector<const GlobalValue *, 4> FilterList;
782+
SmallVector<unsigned, 4> FilterList;
781783
for (const Use &U : CVal->operands())
782-
FilterList.push_back(cast<GlobalValue>(U->stripPointerCasts()));
784+
FilterList.push_back(
785+
getTypeIDFor(cast<GlobalValue>(U->stripPointerCasts())));
783786

784-
addFilterTypeInfo(LandingPad, FilterList);
787+
LP.TypeIds.push_back(getFilterIDFor(FilterList));
785788
}
786789
}
787790

788791
} else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
789792
for (unsigned I = CPI->arg_size(); I != 0; --I) {
790-
Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
791-
addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
793+
auto *TypeInfo =
794+
dyn_cast<GlobalValue>(CPI->getArgOperand(I - 1)->stripPointerCasts());
795+
LP.TypeIds.push_back(getTypeIDFor(TypeInfo));
792796
}
793797

794798
} else {
@@ -798,73 +802,6 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
798802
return LandingPadLabel;
799803
}
800804

801-
void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
802-
ArrayRef<const GlobalValue *> TyInfo) {
803-
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
804-
for (const GlobalValue *GV : llvm::reverse(TyInfo))
805-
LP.TypeIds.push_back(getTypeIDFor(GV));
806-
}
807-
808-
void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
809-
ArrayRef<const GlobalValue *> TyInfo) {
810-
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
811-
std::vector<unsigned> IdsInFilter(TyInfo.size());
812-
for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
813-
IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
814-
LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
815-
}
816-
817-
void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
818-
bool TidyIfNoBeginLabels) {
819-
for (unsigned i = 0; i != LandingPads.size(); ) {
820-
LandingPadInfo &LandingPad = LandingPads[i];
821-
if (LandingPad.LandingPadLabel &&
822-
!LandingPad.LandingPadLabel->isDefined() &&
823-
(!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
824-
LandingPad.LandingPadLabel = nullptr;
825-
826-
// Special case: we *should* emit LPs with null LP MBB. This indicates
827-
// "nounwind" case.
828-
if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
829-
LandingPads.erase(LandingPads.begin() + i);
830-
continue;
831-
}
832-
833-
if (TidyIfNoBeginLabels) {
834-
for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
835-
MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
836-
MCSymbol *EndLabel = LandingPad.EndLabels[j];
837-
if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
838-
(EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
839-
continue;
840-
841-
LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
842-
LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
843-
--j;
844-
--e;
845-
}
846-
847-
// Remove landing pads with no try-ranges.
848-
if (LandingPads[i].BeginLabels.empty()) {
849-
LandingPads.erase(LandingPads.begin() + i);
850-
continue;
851-
}
852-
}
853-
854-
// If there is no landing pad, ensure that the list of typeids is empty.
855-
// If the only typeid is a cleanup, this is the same as having no typeids.
856-
if (!LandingPad.LandingPadBlock ||
857-
(LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
858-
LandingPad.TypeIds.clear();
859-
++i;
860-
}
861-
}
862-
863-
void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
864-
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
865-
LP.TypeIds.push_back(0);
866-
}
867-
868805
void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
869806
ArrayRef<unsigned> Sites) {
870807
LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
@@ -878,7 +815,7 @@ unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
878815
return TypeInfos.size();
879816
}
880817

881-
int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
818+
int MachineFunction::getFilterIDFor(ArrayRef<unsigned> TyIds) {
882819
// If the new filter coincides with the tail of an existing filter, then
883820
// re-use the existing filter. Folding filters more than this requires
884821
// re-ordering filters and/or their elements - probably not worth it.

0 commit comments

Comments
 (0)