Skip to content

Commit b694030

Browse files
author
Sanjin Sijaric
committed
[ARM64][Windows] Share unwind codes between epilogues
There are cases where we have multiple epilogues that have the exact same unwind code sequence. In that case, the epilogues can share the same unwind codes in the .xdata section. This should get us past the assert "SEH unwind data splitting not yet implemented" in many cases. We still need to add support for generating multiple .pdata/.xdata sections for those functions that need to be split into fragments. Differential Revision: https://reviews.llvm.org/D56813 llvm-svn: 351421
1 parent ce5cafe commit b694030

File tree

3 files changed

+279
-5
lines changed

3 files changed

+279
-5
lines changed

llvm/lib/MC/MCWin64EH.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,38 @@ static void ARM64EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
453453
}
454454
}
455455

456+
// Returns the epilog symbol of an epilog with the exact same unwind code
457+
// sequence, if it exists. Otherwise, returns nulltpr.
458+
// EpilogInstrs - Unwind codes for the current epilog.
459+
// Epilogs - Epilogs that potentialy match the current epilog.
460+
static MCSymbol*
461+
FindMatchingEpilog(const std::vector<WinEH::Instruction>& EpilogInstrs,
462+
const std::vector<MCSymbol *>& Epilogs,
463+
const WinEH::FrameInfo *info) {
464+
for (auto *EpilogStart : Epilogs) {
465+
auto InstrsIter = info->EpilogMap.find(EpilogStart);
466+
assert(InstrsIter != info->EpilogMap.end() &&
467+
"Epilog not found in EpilogMap");
468+
const auto &Instrs = InstrsIter->second;
469+
470+
if (Instrs.size() != EpilogInstrs.size())
471+
continue;
472+
473+
bool Match = true;
474+
for (unsigned i = 0; i < Instrs.size(); ++i)
475+
if (Instrs[i].Operation != EpilogInstrs[i].Operation ||
476+
Instrs[i].Offset != EpilogInstrs[i].Offset ||
477+
Instrs[i].Register != EpilogInstrs[i].Register) {
478+
Match = false;
479+
break;
480+
}
481+
482+
if (Match)
483+
return EpilogStart;
484+
}
485+
return nullptr;
486+
}
487+
456488
// Populate the .xdata section. The format of .xdata on ARM64 is documented at
457489
// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
458490
static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
@@ -477,12 +509,29 @@ static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
477509

478510
// Process epilogs.
479511
MapVector<MCSymbol *, uint32_t> EpilogInfo;
512+
// Epilogs processed so far.
513+
std::vector<MCSymbol *> AddedEpilogs;
514+
480515
for (auto &I : info->EpilogMap) {
481516
MCSymbol *EpilogStart = I.first;
482517
auto &EpilogInstrs = I.second;
483518
uint32_t CodeBytes = ARM64CountOfUnwindCodes(EpilogInstrs);
484-
EpilogInfo[EpilogStart] = TotalCodeBytes;
485-
TotalCodeBytes += CodeBytes;
519+
520+
uint32_t NumUnwindCodes = EpilogInstrs.size();
521+
MCSymbol* MatchingEpilog =
522+
FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info);
523+
if (MatchingEpilog) {
524+
assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() &&
525+
"Duplicate epilog not found");
526+
EpilogInfo[EpilogStart] = EpilogInfo[MatchingEpilog];
527+
// Clear the unwind codes in the EpilogMap, so that they don't get output
528+
// in the logic below.
529+
EpilogInstrs.clear();
530+
} else {
531+
EpilogInfo[EpilogStart] = TotalCodeBytes;
532+
TotalCodeBytes += CodeBytes;
533+
AddedEpilogs.push_back(EpilogStart);
534+
}
486535
}
487536

488537
// Code Words, Epilog count, E, X, Vers, Function Length

llvm/test/CodeGen/AArch64/wineh4.mir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# RUN: llc -o - %s -mtriple=aarch64-windows -start-after=prologepilog \
22
# RUN: -disable-branch-fold -filetype=obj \
33
# RUN: | llvm-readobj -unwind | FileCheck %s
4-
# Check that multiple epilgoues are correctly placed in .xdata.
4+
# Check that identical multiple epilgoues are correctly shared in .xdata.
55

66
# CHECK: ExceptionData {
77
# CHECK-NEXT: FunctionLength: 164
88
# CHECK-NEXT: Version: 0
99
# CHECK-NEXT: ExceptionData: No
1010
# CHECK-NEXT: EpiloguePacked: No
1111
# CHECK-NEXT: EpilogueScopes: 2
12-
# CHECK-NEXT: ByteCodeLength: 48
12+
# CHECK-NEXT: ByteCodeLength: 32
1313
# CHECK-NEXT: Prologue [
1414
# CHECK-NEXT: 0xc80c ; stp x19, x20, [sp, #96]
1515
# CHECK-NEXT: 0xc88a ; stp x21, x22, [sp, #80]
@@ -37,7 +37,7 @@
3737
# CHECK-NEXT: }
3838
# CHECK-NEXT: EpilogueScope {
3939
# CHECK-NEXT: StartOffset: 33
40-
# CHECK-NEXT: EpilogueStartIndex: 30
40+
# CHECK-NEXT: EpilogueStartIndex: 15
4141
# CHECK-NEXT: Opcodes [
4242
# CHECK-NEXT: 0xc80c ; ldp x19, x20, [sp, #96]
4343
# CHECK-NEXT: 0xc88a ; ldp x21, x22, [sp, #80]

llvm/test/CodeGen/AArch64/wineh8.mir

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# RUN: llc -o - %s -mtriple=aarch64-windows -start-after=prologepilog \
2+
# RUN: -disable-branch-fold -filetype=obj \
3+
# RUN: | llvm-readobj -unwind | FileCheck %s
4+
# Check that non-identical multiple epilgoues are correctly shared in .xdata.
5+
6+
# CHECK: ExceptionData {
7+
# CHECK-NEXT: FunctionLength: 160
8+
# CHECK-NEXT: Version: 0
9+
# CHECK-NEXT: ExceptionData: No
10+
# CHECK-NEXT: EpiloguePacked: No
11+
# CHECK-NEXT: EpilogueScopes: 2
12+
# CHECK-NEXT: ByteCodeLength: 44
13+
# CHECK-NEXT: Prologue [
14+
# CHECK-NEXT: 0xc80c ; stp x19, x20, [sp, #96]
15+
# CHECK-NEXT: 0xc88a ; stp x21, x22, [sp, #80]
16+
# CHECK-NEXT: 0xc908 ; stp x23, x24, [sp, #64]
17+
# CHECK-NEXT: 0xc986 ; stp x25, x26, [sp, #48]
18+
# CHECK-NEXT: 0xca04 ; stp x27, x28, [sp, #32]
19+
# CHECK-NEXT: 0xd802 ; stp d8, d9, [sp, #16]
20+
# CHECK-NEXT: 0xda8d ; stp d10, d11, [sp, #-112]!
21+
# CHECK-NEXT: 0xe4 ; end
22+
# CHECK-NEXT: ]
23+
# CHECK-NEXT: EpilogueScopes [
24+
# CHECK-NEXT: EpilogueScope {
25+
# CHECK-NEXT: StartOffset: 16
26+
# CHECK-NEXT: EpilogueStartIndex: 15
27+
# CHECK-NEXT: Opcodes [
28+
# CHECK-NEXT: 0xc80c ; ldp x19, x20, [sp, #96]
29+
# CHECK-NEXT: 0xc88a ; ldp x21, x22, [sp, #80]
30+
# CHECK-NEXT: 0xc908 ; ldp x23, x24, [sp, #64]
31+
# CHECK-NEXT: 0xc986 ; ldp x25, x26, [sp, #48]
32+
# CHECK-NEXT: 0xd802 ; ldp d8, d9, [sp, #16]
33+
# CHECK-NEXT: 0xda8d ; ldp d10, d11, [sp], #112
34+
# CHECK-NEXT: 0xe4 ; end
35+
# CHECK-NEXT: ]
36+
# CHECK-NEXT: }
37+
# CHECK-NEXT: EpilogueScope {
38+
# CHECK-NEXT: StartOffset: 32
39+
# CHECK-NEXT: EpilogueStartIndex: 28
40+
# CHECK-NEXT: Opcodes [
41+
# CHECK-NEXT: 0xc80c ; ldp x19, x20, [sp, #96]
42+
# CHECK-NEXT: 0xc88a ; ldp x21, x22, [sp, #80]
43+
# CHECK-NEXT: 0xc908 ; ldp x23, x24, [sp, #64]
44+
# CHECK-NEXT: 0xc986 ; ldp x25, x26, [sp, #48]
45+
# CHECK-NEXT: 0xca04 ; ldp x27, x28, [sp, #32]
46+
# CHECK-NEXT: 0xd802 ; ldp d8, d9, [sp, #16]
47+
# CHECK-NEXT: 0xda8d ; ldp d10, d11, [sp], #112
48+
# CHECK-NEXT: 0xe4 ; end
49+
# CHECK-NEXT: ]
50+
# CHECK-NEXT: }
51+
# CHECK-NEXT: ]
52+
# CHECK-NEXT: }
53+
...
54+
---
55+
name: test
56+
alignment: 2
57+
exposesReturnsTwice: false
58+
legalized: false
59+
regBankSelected: false
60+
selected: false
61+
failedISel: false
62+
tracksRegLiveness: true
63+
hasWinCFI: true
64+
registers:
65+
liveins:
66+
- { reg: '$w0', virtual-reg: '' }
67+
frameInfo:
68+
isFrameAddressTaken: false
69+
isReturnAddressTaken: false
70+
hasStackMap: false
71+
hasPatchPoint: false
72+
stackSize: 112
73+
offsetAdjustment: 0
74+
maxAlignment: 8
75+
adjustsStack: false
76+
hasCalls: false
77+
stackProtector: ''
78+
maxCallFrameSize: 0
79+
hasOpaqueSPAdjustment: true
80+
hasVAStart: false
81+
hasMustTailInVarArgFunc: false
82+
localFrameSize: 0
83+
savePoint: ''
84+
restorePoint: ''
85+
fixedStack:
86+
stack:
87+
- { id: 0, name: '', type: spill-slot, offset: -8, size: 8, alignment: 8,
88+
stack-id: 0, callee-saved-register: '$x19', callee-saved-restored: true,
89+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
90+
- { id: 1, name: '', type: spill-slot, offset: -16, size: 8, alignment: 8,
91+
stack-id: 0, callee-saved-register: '$x20', callee-saved-restored: true,
92+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
93+
- { id: 2, name: '', type: spill-slot, offset: -24, size: 8, alignment: 8,
94+
stack-id: 0, callee-saved-register: '$x21', callee-saved-restored: true,
95+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
96+
- { id: 3, name: '', type: spill-slot, offset: -32, size: 8, alignment: 8,
97+
stack-id: 0, callee-saved-register: '$x22', callee-saved-restored: true,
98+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
99+
- { id: 4, name: '', type: spill-slot, offset: -40, size: 8, alignment: 8,
100+
stack-id: 0, callee-saved-register: '$x23', callee-saved-restored: true,
101+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
102+
- { id: 5, name: '', type: spill-slot, offset: -48, size: 8, alignment: 8,
103+
stack-id: 0, callee-saved-register: '$x24', callee-saved-restored: true,
104+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
105+
- { id: 6, name: '', type: spill-slot, offset: -56, size: 8, alignment: 8,
106+
stack-id: 0, callee-saved-register: '$x25', callee-saved-restored: true,
107+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
108+
- { id: 7, name: '', type: spill-slot, offset: -64, size: 8, alignment: 8,
109+
stack-id: 0, callee-saved-register: '$x26', callee-saved-restored: true,
110+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
111+
- { id: 8, name: '', type: spill-slot, offset: -72, size: 8, alignment: 8,
112+
stack-id: 0, callee-saved-register: '$x27', callee-saved-restored: true,
113+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
114+
- { id: 9, name: '', type: spill-slot, offset: -80, size: 8, alignment: 8,
115+
stack-id: 0, callee-saved-register: '$x28', callee-saved-restored: true,
116+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
117+
- { id: 10, name: '', type: spill-slot, offset: -88, size: 8, alignment: 8,
118+
stack-id: 0, callee-saved-register: '$d8', callee-saved-restored: true,
119+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
120+
- { id: 11, name: '', type: spill-slot, offset: -96, size: 8, alignment: 8,
121+
stack-id: 0, callee-saved-register: '$d9', callee-saved-restored: true,
122+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
123+
- { id: 12, name: '', type: spill-slot, offset: -104, size: 8, alignment: 8,
124+
stack-id: 0, callee-saved-register: '$d10', callee-saved-restored: true,
125+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
126+
- { id: 13, name: '', type: spill-slot, offset: -112, size: 8, alignment: 8,
127+
stack-id: 0, callee-saved-register: '$d11', callee-saved-restored: true,
128+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
129+
constants:
130+
body: |
131+
bb.0.entry:
132+
successors: %bb.2(0x40000000), %bb.1(0x40000000)
133+
liveins: $x0, $x1, $d0, $d1, $d10, $d11, $d8, $d9, $x27, $x28, $x25, $x26, $x23, $x24, $x21, $x22, $x19, $x20
134+
135+
early-clobber $sp = frame-setup STPDpre killed $d10, killed $d11, $sp, -14 :: (store 8 into %stack.12), (store 8 into %stack.13)
136+
frame-setup SEH_SaveFRegP_X 10, 11, -112
137+
frame-setup STPDi killed $d8, killed $d9, $sp, 2 :: (store 8 into %stack.10), (store 8 into %stack.11)
138+
frame-setup SEH_SaveFRegP 8, 9, 16
139+
frame-setup STPXi killed $x27, killed $x28, $sp, 4 :: (store 8 into %stack.8), (store 8 into %stack.9)
140+
frame-setup SEH_SaveRegP 27, 28, 32
141+
frame-setup STPXi killed $x25, killed $x26, $sp, 6 :: (store 8 into %stack.6), (store 8 into %stack.7)
142+
frame-setup SEH_SaveRegP 25, 26, 48
143+
frame-setup STPXi killed $x23, killed $x24, $sp, 8 :: (store 8 into %stack.4), (store 8 into %stack.5)
144+
frame-setup SEH_SaveRegP 23, 24, 64
145+
frame-setup STPXi killed $x21, killed $x22, $sp, 10 :: (store 8 into %stack.2), (store 8 into %stack.3)
146+
frame-setup SEH_SaveRegP 21, 22, 80
147+
frame-setup STPXi killed $x19, killed $x20, $sp, 12 :: (store 8 into %stack.0), (store 8 into %stack.1)
148+
frame-setup SEH_SaveRegP 19, 20, 96
149+
frame-setup SEH_PrologEnd
150+
frame-setup CFI_INSTRUCTION def_cfa_offset 112
151+
frame-setup CFI_INSTRUCTION offset $w19, -8
152+
frame-setup CFI_INSTRUCTION offset $w20, -16
153+
frame-setup CFI_INSTRUCTION offset $w21, -24
154+
frame-setup CFI_INSTRUCTION offset $w22, -32
155+
frame-setup CFI_INSTRUCTION offset $w23, -40
156+
frame-setup CFI_INSTRUCTION offset $w24, -48
157+
frame-setup CFI_INSTRUCTION offset $w25, -56
158+
frame-setup CFI_INSTRUCTION offset $w26, -64
159+
frame-setup CFI_INSTRUCTION offset $w27, -72
160+
frame-setup CFI_INSTRUCTION offset $w28, -80
161+
frame-setup CFI_INSTRUCTION offset $b8, -88
162+
frame-setup CFI_INSTRUCTION offset $b9, -96
163+
frame-setup CFI_INSTRUCTION offset $b10, -104
164+
frame-setup CFI_INSTRUCTION offset $b11, -112
165+
$x19 = ADDXrr $x0, killed $x1
166+
$d8 = FADDDrr killed $d0, $d1
167+
$d9 = FADDDrr $d8, $d1
168+
$d10 = FADDDrr $d9, $d8
169+
$d11 = FADDDrr killed $d9, $d10
170+
$x20 = SUBSXrr $x19, killed $x0, implicit-def $nzcv
171+
Bcc 1, %bb.2, implicit killed $nzcv
172+
B %bb.1
173+
174+
bb.1:
175+
liveins: $x19, $x20
176+
177+
$x21 = ADDXrr $x20, killed $x19
178+
$x22 = ADDXrr $x21, killed $x20
179+
$x23 = ADDXrr $x22, killed $x21
180+
$x24 = ADDXrr $x23, killed $x22
181+
$x25 = ADDXrr $x24, killed $x23
182+
$x26 = ADDXrr $x25, killed $x24
183+
$x27 = ADDXrr $x26, killed $x25
184+
$x28 = ADDXrr $x27, killed $x26
185+
$x0 = COPY $x28
186+
frame-destroy SEH_EpilogStart
187+
$x19, $x20 = frame-destroy LDPXi $sp, 12 :: (load 8 from %stack.0), (load 8 from %stack.1)
188+
frame-destroy SEH_SaveRegP 19, 20, 96
189+
$x21, $x22 = frame-destroy LDPXi $sp, 10 :: (load 8 from %stack.2), (load 8 from %stack.3)
190+
frame-destroy SEH_SaveRegP 21, 22, 80
191+
$x23, $x24 = frame-destroy LDPXi $sp, 8 :: (load 8 from %stack.4), (load 8 from %stack.5)
192+
frame-destroy SEH_SaveRegP 23, 24, 64
193+
$x25, $x26 = frame-destroy LDPXi $sp, 6 :: (load 8 from %stack.6), (load 8 from %stack.7)
194+
frame-destroy SEH_SaveRegP 25, 26, 48
195+
$x27, $x28 = frame-destroy LDPXi $sp, 4 :: (load 8 from %stack.8), (load 8 from %stack.9)
196+
frame-destroy SEH_SaveRegP 27, 28, 32
197+
$d8, $d9 = frame-destroy LDPDi $sp, 2 :: (load 8 from %stack.10), (load 8 from %stack.11)
198+
frame-destroy SEH_SaveFRegP 8, 9, 16
199+
early-clobber $sp, $d10, $d11 = frame-destroy LDPDpost $sp, 14 :: (load 8 from %stack.12), (load 8 from %stack.13)
200+
frame-destroy SEH_SaveFRegP_X 10, 11, -112
201+
frame-destroy SEH_EpilogEnd
202+
RET_ReallyLR implicit $x0
203+
204+
bb.2:
205+
liveins: $x28, $d11
206+
207+
$x0 = COPY $d11
208+
$x0 = ADDXrr $x0, killed $x28
209+
frame-destroy SEH_EpilogStart
210+
$x19, $x20 = frame-destroy LDPXi $sp, 12 :: (load 8 from %stack.0), (load 8 from %stack.1)
211+
frame-destroy SEH_SaveRegP 19, 20, 96
212+
$x21, $x22 = frame-destroy LDPXi $sp, 10 :: (load 8 from %stack.2), (load 8 from %stack.3)
213+
frame-destroy SEH_SaveRegP 21, 22, 80
214+
$x23, $x24 = frame-destroy LDPXi $sp, 8 :: (load 8 from %stack.4), (load 8 from %stack.5)
215+
frame-destroy SEH_SaveRegP 23, 24, 64
216+
$x25, $x26 = frame-destroy LDPXi $sp, 6 :: (load 8 from %stack.6), (load 8 from %stack.7)
217+
frame-destroy SEH_SaveRegP 25, 26, 48
218+
$d8, $d9 = frame-destroy LDPDi $sp, 2 :: (load 8 from %stack.10), (load 8 from %stack.11)
219+
frame-destroy SEH_SaveFRegP 8, 9, 16
220+
early-clobber $sp, $d10, $d11 = frame-destroy LDPDpost $sp, 14 :: (load 8 from %stack.12), (load 8 from %stack.13)
221+
frame-destroy SEH_SaveFRegP_X 10, 11, -112
222+
frame-destroy SEH_EpilogEnd
223+
RET_ReallyLR implicit $x0
224+
225+
...

0 commit comments

Comments
 (0)