Skip to content

Commit ce214c2

Browse files
committed
Insert state restore before SEH scope end
1 parent e4a56b8 commit ce214c2

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

llvm/lib/Target/X86/X86WinEHState.cpp

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,28 @@ int WinEHStatePass::getBaseStateForBB(
517517
return BaseState;
518518
}
519519

520+
static bool isIntrinsic(const CallBase &Call, Intrinsic::ID ID) {
521+
const Function *CF = Call.getCalledFunction();
522+
return CF && CF->isIntrinsic() && CF->getIntrinsicID() == ID;
523+
}
524+
525+
static bool isSehScopeEnd(const CallBase &Call) {
526+
return isIntrinsic(Call, Intrinsic::seh_scope_end);
527+
}
528+
529+
static bool isSehScopeBegin(const CallBase &Call) {
530+
return isIntrinsic(Call, Intrinsic::seh_scope_begin);
531+
}
532+
520533
// Calculate the state a call-site is in.
521534
int WinEHStatePass::getStateForCall(
522535
DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo,
523536
CallBase &Call) {
524537
if (auto *II = dyn_cast<InvokeInst>(&Call)) {
538+
if (isSehScopeEnd(*II)) {
539+
return getBaseStateForBB(BlockColors, FuncInfo, II->getNormalDest());
540+
}
541+
525542
// Look up the state number of the EH pad this unwinds to.
526543
assert(FuncInfo.InvokeStateMap.count(II) && "invoke has no state!");
527544
return FuncInfo.InvokeStateMap[II];
@@ -608,22 +625,9 @@ static int getSuccState(DenseMap<BasicBlock *, int> &InitialStates, Function &F,
608625
return CommonState;
609626
}
610627

611-
static bool isIntrinsic(const CallBase &Call, Intrinsic::ID ID) {
612-
const Function *CF = Call.getCalledFunction();
613-
return CF && CF->isIntrinsic() && CF->getIntrinsicID() == ID;
614-
}
615-
616-
static bool isSehScopeEnd(const CallBase &Call) {
617-
return isIntrinsic(Call, Intrinsic::seh_scope_end);
618-
}
619-
620-
static bool isSehScopeBegin(const CallBase &Call) {
621-
return isIntrinsic(Call, Intrinsic::seh_scope_begin);
622-
}
623-
624628
bool WinEHStatePass::isStateStoreNeeded(EHPersonality Personality,
625629
CallBase &Call) {
626-
if (isSehScopeBegin(Call)) {
630+
if (isSehScopeBegin(Call) || isSehScopeEnd(Call)) {
627631
return true;
628632
}
629633

@@ -663,8 +667,6 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
663667
DenseMap<BasicBlock *, int> InitialStates;
664668
// FinalStates yields the state of the last call-site for a BasicBlock.
665669
DenseMap<BasicBlock *, int> FinalStates;
666-
// SEH scope end target blocks
667-
SmallPtrSet<BasicBlock *, 4> ScopeEndBlocks;
668670
// Worklist used to revisit BasicBlocks with indeterminate
669671
// Initial/Final-States.
670672
std::deque<BasicBlock *> Worklist;
@@ -676,15 +678,7 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
676678
InitialState = FinalState = ParentBaseState;
677679
for (Instruction &I : *BB) {
678680
auto *Call = dyn_cast<CallBase>(&I);
679-
if (!Call)
680-
continue;
681-
682-
if (isSehScopeEnd(*Call)) {
683-
auto *Invoke = cast<InvokeInst>(Call);
684-
ScopeEndBlocks.insert(Invoke->getNormalDest());
685-
}
686-
687-
if (!isStateStoreNeeded(Personality, *Call))
681+
if (!Call || !isStateStoreNeeded(Personality, *Call))
688682
continue;
689683

690684
int State = getStateForCall(BlockColors, FuncInfo, *Call);
@@ -737,12 +731,6 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
737731
FinalStates.insert({BB, SuccState});
738732
}
739733

740-
// Insert state restores after SEH scope ends
741-
for (BasicBlock *BB : ScopeEndBlocks) {
742-
int state = getBaseStateForBB(BlockColors, FuncInfo, BB);
743-
insertStateNumberStore(BB->getFirstNonPHI(), state);
744-
}
745-
746734
// Finally, insert state stores before call-sites which transition us to a new
747735
// state.
748736
for (BasicBlock *BB : RPOT) {

llvm/test/CodeGen/WinEH/wineh-scope-statenumbering.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ entry:
1919

2020
invoke.cont:
2121
store i32 1, ptr inttoptr (i32 1 to ptr), align 4
22+
; CHECK: store i32 -1, ptr %10, align 4
23+
; CHECK-NEXT: invoke void @llvm.seh.scope.end()
2224
invoke void @llvm.seh.scope.end()
2325
to label %invoke.cont1 unwind label %ehcleanup
2426

2527
invoke.cont1:
26-
; CHECK: invoke.cont1:
27-
; CHECK-NEXT:%10 = getelementptr inbounds nuw %CXXExceptionRegistration, ptr %0, i32 0, i32 2
28-
; CHECK-NEXT: store i32 -1, ptr %10, align 4
2928
call x86_thiscallcc void @"??1Destructor@@QAE@XZ"(ptr noundef nonnull align 4 dereferenceable(4) %x) #1
3029
ret void
3130

0 commit comments

Comments
 (0)