Skip to content

Commit 60543c0

Browse files
committed
Use std::optional in the MachineVerifier to distinguish zero-sized from no call frames.
1 parent 79c40aa commit 60543c0

File tree

1 file changed

+41
-54
lines changed

1 file changed

+41
-54
lines changed

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,42 +3786,39 @@ void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
37863786

37873787
namespace {
37883788

3789-
// FrameSetup and FrameDestroy can have zero adjustment, so using a single
3790-
// integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
3791-
// value is zero.
3792-
// We use a bool plus an integer to capture the stack state.
3793-
struct StackStateOfBB {
3794-
StackStateOfBB() = default;
3795-
StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
3796-
EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
3797-
ExitIsSetup(ExitSetup) {}
3798-
3799-
// Can be negative, which means we are setting up a frame.
3800-
int EntryValue = 0;
3801-
int ExitValue = 0;
3802-
bool EntryIsSetup = false;
3803-
bool ExitIsSetup = false;
3804-
};
3789+
/// Store for each MachineBasicBlock the call frame size at its entry and its
3790+
/// exit. No value means that no call frame is open, zero means that a
3791+
/// zero-sized call frame is open.
3792+
struct StackStateOfBB {
3793+
StackStateOfBB() = default;
3794+
StackStateOfBB(std::optional<unsigned> EntryVal,
3795+
std::optional<unsigned> ExitVal)
3796+
: Entry(EntryVal), Exit(ExitVal) {}
3797+
3798+
std::optional<unsigned> Entry;
3799+
std::optional<unsigned> Exit;
3800+
};
38053801

38063802
} // end anonymous namespace
38073803

38083804
/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
38093805
/// by a FrameDestroy <n>, stack adjustments are identical on all
38103806
/// CFG edges to a merge point, and frame is destroyed at end of a return block.
38113807
void MachineVerifier::verifyStackFrame() {
3812-
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
3808+
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
38133809
unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
38143810
if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
38153811
return;
38163812

38173813
SmallVector<StackStateOfBB, 8> SPState;
38183814
SPState.resize(MF->getNumBlockIDs());
3819-
df_iterator_default_set<const MachineBasicBlock*> Reachable;
3815+
df_iterator_default_set<const MachineBasicBlock *> Reachable;
38203816

38213817
// Visit the MBBs in DFS order.
38223818
for (df_ext_iterator<const MachineFunction *,
38233819
df_iterator_default_set<const MachineBasicBlock *>>
3824-
DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
3820+
DFI = df_ext_begin(MF, Reachable),
3821+
DFE = df_ext_end(MF, Reachable);
38253822
DFI != DFE; ++DFI) {
38263823
const MachineBasicBlock *MBB = *DFI;
38273824

@@ -3831,49 +3828,45 @@ void MachineVerifier::verifyStackFrame() {
38313828
const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
38323829
assert(Reachable.count(StackPred) &&
38333830
"DFS stack predecessor is already visited.\n");
3834-
BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
3835-
BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
3836-
BBState.ExitValue = BBState.EntryValue;
3837-
BBState.ExitIsSetup = BBState.EntryIsSetup;
3831+
BBState.Entry = SPState[StackPred->getNumber()].Exit;
3832+
BBState.Exit = BBState.Entry;
38383833
}
38393834

3840-
if ((int)MBB->getCallFrameSizeOrZero() != -BBState.EntryValue) {
3835+
if (MBB->getCallFrameSize() != BBState.Entry) {
38413836
report("Call frame size on entry does not match value computed from "
38423837
"predecessor",
38433838
MBB);
3844-
errs() << "Call frame size on entry " << MBB->getCallFrameSizeOrZero()
3839+
errs() << "Call frame size on entry " << MBB->getCallFrameSize()
38453840
<< " does not match value computed from predecessor "
3846-
<< -BBState.EntryValue << '\n';
3841+
<< BBState.Entry << '\n';
38473842
}
38483843

38493844
// Update stack state by checking contents of MBB.
38503845
for (const auto &I : *MBB) {
38513846
if (I.getOpcode() == FrameSetupOpcode) {
3852-
if (BBState.ExitIsSetup)
3847+
if (BBState.Exit.has_value())
38533848
report("FrameSetup is after another FrameSetup", &I);
38543849
if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack())
38553850
report("AdjustsStack not set in presence of a frame pseudo "
3856-
"instruction.", &I);
3857-
BBState.ExitValue -= TII->getFrameTotalSize(I);
3858-
BBState.ExitIsSetup = true;
3851+
"instruction.",
3852+
&I);
3853+
BBState.Exit = TII->getFrameTotalSize(I);
38593854
}
38603855

38613856
if (I.getOpcode() == FrameDestroyOpcode) {
3862-
int Size = TII->getFrameTotalSize(I);
3863-
if (!BBState.ExitIsSetup)
3857+
int64_t Size = TII->getFrameTotalSize(I);
3858+
if (!BBState.Exit.has_value())
38643859
report("FrameDestroy is not after a FrameSetup", &I);
3865-
int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
3866-
BBState.ExitValue;
3867-
if (BBState.ExitIsSetup && AbsSPAdj != Size) {
3860+
else if ((int64_t)BBState.Exit.value() != Size) {
38683861
report("FrameDestroy <n> is after FrameSetup <m>", &I);
38693862
errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
3870-
<< AbsSPAdj << ">.\n";
3863+
<< BBState.Exit.value() << ">.\n";
38713864
}
38723865
if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack())
38733866
report("AdjustsStack not set in presence of a frame pseudo "
3874-
"instruction.", &I);
3875-
BBState.ExitValue += Size;
3876-
BBState.ExitIsSetup = false;
3867+
"instruction.",
3868+
&I);
3869+
BBState.Exit.reset();
38773870
}
38783871
}
38793872
SPState[MBB->getNumber()] = BBState;
@@ -3882,38 +3875,32 @@ void MachineVerifier::verifyStackFrame() {
38823875
// state.
38833876
for (const MachineBasicBlock *Pred : MBB->predecessors()) {
38843877
if (Reachable.count(Pred) &&
3885-
(SPState[Pred->getNumber()].ExitValue != BBState.EntryValue ||
3886-
SPState[Pred->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
3878+
SPState[Pred->getNumber()].Exit != BBState.Entry) {
38873879
report("The exit stack state of a predecessor is inconsistent.", MBB);
38883880
errs() << "Predecessor " << printMBBReference(*Pred)
3889-
<< " has exit state (" << SPState[Pred->getNumber()].ExitValue
3890-
<< ", " << SPState[Pred->getNumber()].ExitIsSetup << "), while "
3891-
<< printMBBReference(*MBB) << " has entry state ("
3892-
<< BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
3881+
<< " has exit state " << SPState[Pred->getNumber()].Exit
3882+
<< ", while " << printMBBReference(*MBB) << " has entry state "
3883+
<< BBState.Entry << ".\n";
38933884
}
38943885
}
38953886

38963887
// Make sure the entry state of any successor is consistent with the exit
38973888
// state.
38983889
for (const MachineBasicBlock *Succ : MBB->successors()) {
38993890
if (Reachable.count(Succ) &&
3900-
(SPState[Succ->getNumber()].EntryValue != BBState.ExitValue ||
3901-
SPState[Succ->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
3891+
SPState[Succ->getNumber()].Entry != BBState.Exit) {
39023892
report("The entry stack state of a successor is inconsistent.", MBB);
39033893
errs() << "Successor " << printMBBReference(*Succ)
3904-
<< " has entry state (" << SPState[Succ->getNumber()].EntryValue
3905-
<< ", " << SPState[Succ->getNumber()].EntryIsSetup << "), while "
3906-
<< printMBBReference(*MBB) << " has exit state ("
3907-
<< BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
3894+
<< " has entry state " << SPState[Succ->getNumber()].Entry
3895+
<< ", while " << printMBBReference(*MBB) << " has exit state "
3896+
<< BBState.Exit << ".\n";
39083897
}
39093898
}
39103899

39113900
// Make sure a basic block with return ends with zero stack adjustment.
39123901
if (!MBB->empty() && MBB->back().isReturn()) {
3913-
if (BBState.ExitIsSetup)
3902+
if (BBState.Exit.has_value())
39143903
report("A return block ends with a FrameSetup.", MBB);
3915-
if (BBState.ExitValue)
3916-
report("A return block ends with a nonzero stack adjustment.", MBB);
39173904
}
39183905
}
39193906
}

0 commit comments

Comments
 (0)