Skip to content

Commit 1c7f71c

Browse files
committed
Revert "Cleanup unwind table emission code a bit."
This reverts commit 648ce3d. rdar://113994760 llvm#64826
1 parent 72d3f2a commit 1c7f71c

13 files changed

+171
-45
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,10 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
11581158
/// Find or create an LandingPadInfo for the specified MachineBasicBlock.
11591159
LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
11601160

1161+
/// Remap landing pad labels and remove any deleted landing pads.
1162+
void tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap = nullptr,
1163+
bool TidyIfNoBeginLabels = true);
1164+
11611165
/// Return a reference to the landing pad info for the current function.
11621166
const std::vector<LandingPadInfo> &getLandingPads() const {
11631167
return LandingPads;
@@ -1173,11 +1177,22 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
11731177
/// entry.
11741178
MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
11751179

1180+
/// Provide the catch typeinfo for a landing pad.
1181+
void addCatchTypeInfo(MachineBasicBlock *LandingPad,
1182+
ArrayRef<const GlobalValue *> TyInfo);
1183+
1184+
/// Provide the filter typeinfo for a landing pad.
1185+
void addFilterTypeInfo(MachineBasicBlock *LandingPad,
1186+
ArrayRef<const GlobalValue *> TyInfo);
1187+
1188+
/// Add a cleanup action for a landing pad.
1189+
void addCleanup(MachineBasicBlock *LandingPad);
1190+
11761191
/// Return the type id for the specified typeinfo. This is function wide.
11771192
unsigned getTypeIDFor(const GlobalValue *TI);
11781193

11791194
/// Return the id of the filter encoded by TyIds. This is function wide.
1180-
int getFilterIDFor(ArrayRef<unsigned> TyIds);
1195+
int getFilterIDFor(std::vector<unsigned> &TyIds);
11811196

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

llvm/lib/CodeGen/AsmPrinter/AIXException.cpp

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

2222
namespace llvm {
2323

24-
AIXException::AIXException(AsmPrinter *A) : EHStreamer(A) {}
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() {}
2534

2635
void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
2736
const MCSymbol *PerSym) {

llvm/lib/CodeGen/AsmPrinter/ARMException.cpp

Lines changed: 2 additions & 1 deletion
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) : EHStreamer(A) {}
22+
ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
2323

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

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

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

llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp

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

26-
DwarfCFIException::DwarfCFIException(AsmPrinter *A) : EHStreamer(A) {}
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) {}
2738

2839
DwarfCFIException::~DwarfCFIException() = default;
2940

llvm/lib/CodeGen/AsmPrinter/DwarfException.h

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

24-
class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public EHStreamer {
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 {
2537
/// Per-function flag to indicate if .cfi_personality should be emitted.
2638
bool shouldEmitPersonality = false;
2739

@@ -63,13 +75,7 @@ class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public EHStreamer {
6375
void endBasicBlockSection(const MachineBasicBlock &MBB) override;
6476
};
6577

66-
class LLVM_LIBRARY_VISIBILITY ARMException : public EHStreamer {
67-
/// Per-function flag to indicate if frame CFI info should be emitted.
68-
bool shouldEmitCFI = false;
69-
70-
/// Per-module flag to indicate if .cfi_section has beeen emitted.
71-
bool hasEmittedCFISections = false;
72-
78+
class LLVM_LIBRARY_VISIBILITY ARMException : public DwarfCFIExceptionBase {
7379
void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) override;
7480
ARMTargetStreamer &getTargetStreamer();
7581

@@ -93,14 +99,16 @@ class LLVM_LIBRARY_VISIBILITY ARMException : public EHStreamer {
9399
void markFunctionEnd() override;
94100
};
95101

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

101107
public:
102108
AIXException(AsmPrinter *A);
103109

110+
void markFunctionEnd() override;
111+
104112
void endModule() override {}
105113
void beginFunction(const MachineFunction *MF) override {}
106114
void endFunction(const MachineFunction *MF) override;

llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,6 @@ 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;
204198
assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
205199
PadRange P = { i, j };
206200
PadMap[BeginLabel] = P;
@@ -389,14 +383,8 @@ MCSymbol *EHStreamer::emitExceptionTable() {
389383
SmallVector<const LandingPadInfo *, 64> LandingPads;
390384
LandingPads.reserve(PadInfos.size());
391385

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;
386+
for (const LandingPadInfo &LPI : PadInfos)
398387
LandingPads.push_back(&LPI);
399-
}
400388

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

llvm/lib/CodeGen/AsmPrinter/WasmException.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ 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+
4555
void WasmException::endFunction(const MachineFunction *MF) {
4656
bool ShouldEmitExceptionTable = false;
4757
for (const LandingPadInfo &Info : MF->getLandingPads()) {

llvm/lib/CodeGen/AsmPrinter/WasmException.h

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

2929
void endModule() override;
3030
void beginFunction(const MachineFunction *MF) override {}
31+
void markFunctionEnd() override;
3132
void endFunction(const MachineFunction *MF) override;
3233

3334
protected:

llvm/lib/CodeGen/AsmPrinter/WinException.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ 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+
133141
endFuncletImpl();
134142

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

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -780,31 +780,32 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
780780
if (LPI->isCleanup() && LPI->getNumClauses() != 0)
781781
LP.TypeIds.push_back(0);
782782

783+
if (LPI->isCleanup())
784+
addCleanup(LandingPad);
785+
783786
// FIXME: New EH - Add the clauses in reverse order. This isn't 100%
784787
// correct, but we need to do it this way because of how the DWARF EH
785788
// emitter processes the clauses.
786789
for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
787790
Value *Val = LPI->getClause(I - 1);
788791
if (LPI->isCatch(I - 1)) {
789-
LP.TypeIds.push_back(
790-
getTypeIDFor(dyn_cast<GlobalValue>(Val->stripPointerCasts())));
792+
addCatchTypeInfo(LandingPad,
793+
dyn_cast<GlobalValue>(Val->stripPointerCasts()));
791794
} else {
792795
// Add filters in a list.
793796
auto *CVal = cast<Constant>(Val);
794-
SmallVector<unsigned, 4> FilterList;
797+
SmallVector<const GlobalValue *, 4> FilterList;
795798
for (const Use &U : CVal->operands())
796-
FilterList.push_back(
797-
getTypeIDFor(cast<GlobalValue>(U->stripPointerCasts())));
799+
FilterList.push_back(cast<GlobalValue>(U->stripPointerCasts()));
798800

799-
LP.TypeIds.push_back(getFilterIDFor(FilterList));
801+
addFilterTypeInfo(LandingPad, FilterList);
800802
}
801803
}
802804

803805
} else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
804806
for (unsigned I = CPI->arg_size(); I != 0; --I) {
805-
auto *TypeInfo =
806-
dyn_cast<GlobalValue>(CPI->getArgOperand(I - 1)->stripPointerCasts());
807-
LP.TypeIds.push_back(getTypeIDFor(TypeInfo));
807+
Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
808+
addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
808809
}
809810

810811
} else {
@@ -814,6 +815,73 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
814815
return LandingPadLabel;
815816
}
816817

818+
void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
819+
ArrayRef<const GlobalValue *> TyInfo) {
820+
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
821+
for (const GlobalValue *GV : llvm::reverse(TyInfo))
822+
LP.TypeIds.push_back(getTypeIDFor(GV));
823+
}
824+
825+
void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
826+
ArrayRef<const GlobalValue *> TyInfo) {
827+
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
828+
std::vector<unsigned> IdsInFilter(TyInfo.size());
829+
for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
830+
IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
831+
LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
832+
}
833+
834+
void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
835+
bool TidyIfNoBeginLabels) {
836+
for (unsigned i = 0; i != LandingPads.size(); ) {
837+
LandingPadInfo &LandingPad = LandingPads[i];
838+
if (LandingPad.LandingPadLabel &&
839+
!LandingPad.LandingPadLabel->isDefined() &&
840+
(!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
841+
LandingPad.LandingPadLabel = nullptr;
842+
843+
// Special case: we *should* emit LPs with null LP MBB. This indicates
844+
// "nounwind" case.
845+
if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
846+
LandingPads.erase(LandingPads.begin() + i);
847+
continue;
848+
}
849+
850+
if (TidyIfNoBeginLabels) {
851+
for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
852+
MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
853+
MCSymbol *EndLabel = LandingPad.EndLabels[j];
854+
if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
855+
(EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
856+
continue;
857+
858+
LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
859+
LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
860+
--j;
861+
--e;
862+
}
863+
864+
// Remove landing pads with no try-ranges.
865+
if (LandingPads[i].BeginLabels.empty()) {
866+
LandingPads.erase(LandingPads.begin() + i);
867+
continue;
868+
}
869+
}
870+
871+
// If there is no landing pad, ensure that the list of typeids is empty.
872+
// If the only typeid is a cleanup, this is the same as having no typeids.
873+
if (!LandingPad.LandingPadBlock ||
874+
(LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
875+
LandingPad.TypeIds.clear();
876+
++i;
877+
}
878+
}
879+
880+
void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
881+
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
882+
LP.TypeIds.push_back(0);
883+
}
884+
817885
void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
818886
ArrayRef<unsigned> Sites) {
819887
LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
@@ -827,7 +895,7 @@ unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
827895
return TypeInfos.size();
828896
}
829897

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

llvm/test/CodeGen/X86/gcc_except_table_bb_sections.ll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ declare i32 @__gxx_personality_v0(...)
148148
; CHECK-NEXT: .uleb128 .Ltmp0-.Lfunc_begin0 # >> Call Site 1 <<
149149
; CHECK-NEXT: .uleb128 .Ltmp1-.Ltmp0 # Call between .Ltmp0 and .Ltmp1
150150
; CHECK-NEXT: .uleb128 .Ltmp2-main.__part.2 # jumps to .Ltmp2
151-
; CHECK-NEXT: .byte 3 # On action: 2
151+
; CHECK-NEXT: .byte 5 # On action: 3
152152
; CHECK-NEXT: .p2align 2
153153
; CHECK-NEXT: .Lexception1:
154154

@@ -207,9 +207,12 @@ declare i32 @__gxx_personality_v0(...)
207207
; CHECK-NEXT: .byte 0 # >> Action Record 1 <<
208208
; CHECK-NEXT: # Cleanup
209209
; CHECK-NEXT: .byte 0 # No further actions
210-
; CHECK-NEXT: .byte 1 # >> Action Record 2 <<
211-
; CHECK-NEXT: # Catch TypeInfo 1
212-
; CHECK-NEXT: .byte 125 # Continue to action 1
210+
; CHECK-NEXT: .byte 0 # >> Action Record 2 <<
211+
; CHECK-NEXT: # Cleanup
212+
; CHECK-NEXT: .byte 125 # Continue to action 1
213+
; CHECK-NEXT: .byte 1 # >> Action Record 3 <<
214+
; CHECK-NEXT: # Catch TypeInfo 1
215+
; CHECK-NEXT: .byte 125 # Continue to action 2
213216
; CHECK-NEXT: .p2align 2
214217
; CHECK-NEXT: # >> Catch TypeInfos <<
215218

llvm/test/CodeGen/X86/gcc_except_table_bb_sections_ehpad_groups_with_cold.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ declare i32 @__gxx_personality_v0(...)
6666
; CHECK-NEXT: .uleb128 .Ltmp0-.Lfunc_begin0 # >> Call Site 1 <<
6767
; CHECK-NEXT: .uleb128 .Ltmp1-.Ltmp0 # Call between .Ltmp0 and .Ltmp1
6868
; CHECK-NEXT: .uleb128 .Ltmp2-main.cold # jumps to .Ltmp2
69-
; CHECK-NEXT: .byte 3 # On action: 2
69+
; CHECK-NEXT: .byte 5 # On action: 3
7070
; CHECK-NEXT: .p2align 2
7171
; CHECK-NEXT: .Lexception1:
7272
; CHECK-NEXT: .byte 0 # @LPStart Encoding = absptr
@@ -85,9 +85,12 @@ declare i32 @__gxx_personality_v0(...)
8585
; CHECK-NEXT: .byte 0 # >> Action Record 1 <<
8686
; CHECK-NEXT: # Cleanup
8787
; CHECK-NEXT: .byte 0 # No further actions
88-
; CHECK-NEXT: .byte 1 # >> Action Record 2 <<
89-
; CHECK-NEXT: # Catch TypeInfo 1
88+
; CHECK-NEXT: .byte 0 # >> Action Record 2 <<
89+
; CHECK-NEXT: # Cleanup
9090
; CHECK-NEXT: .byte 125 # Continue to action 1
91+
; CHECK-NEXT: .byte 1 # >> Action Record 3 <<
92+
; CHECK-NEXT: # Catch TypeInfo 1
93+
; CHECK-NEXT: .byte 125 # Continue to action 2
9194
; CHECK-NEXT: .p2align 2
9295
; CHECK-NEXT: # >> Catch TypeInfos <<
9396
; CHECK-NEXT: .long _ZTIi # TypeInfo 1

0 commit comments

Comments
 (0)