Skip to content

Commit e79cba6

Browse files
committed
Use std::optional in the MachineVerifier to distinguish zero-sized from no call frames.
1 parent 4f9d1e7 commit e79cba6

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
@@ -3843,21 +3843,17 @@ void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
38433843

38443844
namespace {
38453845

3846-
// FrameSetup and FrameDestroy can have zero adjustment, so using a single
3847-
// integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
3848-
// value is zero.
3849-
// We use a bool plus an integer to capture the stack state.
3846+
/// Store for each MachineBasicBlock the call frame size at its entry and its
3847+
/// exit. No value means that no call frame is open, zero means that a
3848+
/// zero-sized call frame is open.
38503849
struct StackStateOfBB {
38513850
StackStateOfBB() = default;
3852-
StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup)
3853-
: EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
3854-
ExitIsSetup(ExitSetup) {}
3855-
3856-
// Can be negative, which means we are setting up a frame.
3857-
int EntryValue = 0;
3858-
int ExitValue = 0;
3859-
bool EntryIsSetup = false;
3860-
bool ExitIsSetup = false;
3851+
StackStateOfBB(std::optional<unsigned> EntryVal,
3852+
std::optional<unsigned> ExitVal)
3853+
: Entry(EntryVal), Exit(ExitVal) {}
3854+
3855+
std::optional<unsigned> Entry;
3856+
std::optional<unsigned> Exit;
38613857
};
38623858

38633859
} // end anonymous namespace
@@ -3866,19 +3862,20 @@ struct StackStateOfBB {
38663862
/// by a FrameDestroy <n>, stack adjustments are identical on all
38673863
/// CFG edges to a merge point, and frame is destroyed at end of a return block.
38683864
void MachineVerifier::verifyStackFrame() {
3869-
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
3865+
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
38703866
unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
38713867
if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
38723868
return;
38733869

38743870
SmallVector<StackStateOfBB, 8> SPState;
38753871
SPState.resize(MF->getNumBlockIDs());
3876-
df_iterator_default_set<const MachineBasicBlock*> Reachable;
3872+
df_iterator_default_set<const MachineBasicBlock *> Reachable;
38773873

38783874
// Visit the MBBs in DFS order.
38793875
for (df_ext_iterator<const MachineFunction *,
38803876
df_iterator_default_set<const MachineBasicBlock *>>
3881-
DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
3877+
DFI = df_ext_begin(MF, Reachable),
3878+
DFE = df_ext_end(MF, Reachable);
38823879
DFI != DFE; ++DFI) {
38833880
const MachineBasicBlock *MBB = *DFI;
38843881

@@ -3888,49 +3885,45 @@ void MachineVerifier::verifyStackFrame() {
38883885
const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
38893886
assert(Reachable.count(StackPred) &&
38903887
"DFS stack predecessor is already visited.\n");
3891-
BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
3892-
BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
3893-
BBState.ExitValue = BBState.EntryValue;
3894-
BBState.ExitIsSetup = BBState.EntryIsSetup;
3888+
BBState.Entry = SPState[StackPred->getNumber()].Exit;
3889+
BBState.Exit = BBState.Entry;
38953890
}
38963891

3897-
if ((int)MBB->getCallFrameSizeOrZero() != -BBState.EntryValue) {
3892+
if (MBB->getCallFrameSize() != BBState.Entry) {
38983893
report("Call frame size on entry does not match value computed from "
38993894
"predecessor",
39003895
MBB);
3901-
OS << "Call frame size on entry " << MBB->getCallFrameSizeOrZero()
3902-
<< " does not match value computed from predecessor "
3903-
<< -BBState.EntryValue << '\n';
3896+
OS << "Call frame size on entry " << MBB->getCallFrameSize()
3897+
<< " does not match value computed from predecessor " << BBState.Entry
3898+
<< '\n';
39043899
}
39053900

39063901
// Update stack state by checking contents of MBB.
39073902
for (const auto &I : *MBB) {
39083903
if (I.getOpcode() == FrameSetupOpcode) {
3909-
if (BBState.ExitIsSetup)
3904+
if (BBState.Exit.has_value())
39103905
report("FrameSetup is after another FrameSetup", &I);
39113906
if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack())
39123907
report("AdjustsStack not set in presence of a frame pseudo "
3913-
"instruction.", &I);
3914-
BBState.ExitValue -= TII->getFrameTotalSize(I);
3915-
BBState.ExitIsSetup = true;
3908+
"instruction.",
3909+
&I);
3910+
BBState.Exit = TII->getFrameTotalSize(I);
39163911
}
39173912

39183913
if (I.getOpcode() == FrameDestroyOpcode) {
3919-
int Size = TII->getFrameTotalSize(I);
3920-
if (!BBState.ExitIsSetup)
3914+
int64_t Size = TII->getFrameTotalSize(I);
3915+
if (!BBState.Exit.has_value())
39213916
report("FrameDestroy is not after a FrameSetup", &I);
3922-
int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
3923-
BBState.ExitValue;
3924-
if (BBState.ExitIsSetup && AbsSPAdj != Size) {
3917+
else if ((int64_t)BBState.Exit.value() != Size) {
39253918
report("FrameDestroy <n> is after FrameSetup <m>", &I);
39263919
OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
3927-
<< AbsSPAdj << ">.\n";
3920+
<< BBState.Exit.value() << ">.\n";
39283921
}
39293922
if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack())
39303923
report("AdjustsStack not set in presence of a frame pseudo "
3931-
"instruction.", &I);
3932-
BBState.ExitValue += Size;
3933-
BBState.ExitIsSetup = false;
3924+
"instruction.",
3925+
&I);
3926+
BBState.Exit.reset();
39343927
}
39353928
}
39363929
SPState[MBB->getNumber()] = BBState;
@@ -3939,38 +3932,32 @@ void MachineVerifier::verifyStackFrame() {
39393932
// state.
39403933
for (const MachineBasicBlock *Pred : MBB->predecessors()) {
39413934
if (Reachable.count(Pred) &&
3942-
(SPState[Pred->getNumber()].ExitValue != BBState.EntryValue ||
3943-
SPState[Pred->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
3935+
SPState[Pred->getNumber()].Exit != BBState.Entry) {
39443936
report("The exit stack state of a predecessor is inconsistent.", MBB);
3945-
OS << "Predecessor " << printMBBReference(*Pred) << " has exit state ("
3946-
<< SPState[Pred->getNumber()].ExitValue << ", "
3947-
<< SPState[Pred->getNumber()].ExitIsSetup << "), while "
3948-
<< printMBBReference(*MBB) << " has entry state ("
3949-
<< BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
3937+
OS << "Predecessor " << printMBBReference(*Pred) << " has exit state "
3938+
<< SPState[Pred->getNumber()].Exit << ", while "
3939+
<< printMBBReference(*MBB) << " has entry state " << BBState.Entry
3940+
<< ".\n";
39503941
}
39513942
}
39523943

39533944
// Make sure the entry state of any successor is consistent with the exit
39543945
// state.
39553946
for (const MachineBasicBlock *Succ : MBB->successors()) {
39563947
if (Reachable.count(Succ) &&
3957-
(SPState[Succ->getNumber()].EntryValue != BBState.ExitValue ||
3958-
SPState[Succ->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
3948+
SPState[Succ->getNumber()].Entry != BBState.Exit) {
39593949
report("The entry stack state of a successor is inconsistent.", MBB);
3960-
OS << "Successor " << printMBBReference(*Succ) << " has entry state ("
3961-
<< SPState[Succ->getNumber()].EntryValue << ", "
3962-
<< SPState[Succ->getNumber()].EntryIsSetup << "), while "
3963-
<< printMBBReference(*MBB) << " has exit state ("
3964-
<< BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
3950+
OS << "Successor " << printMBBReference(*Succ) << " has entry state "
3951+
<< SPState[Succ->getNumber()].Entry << ", while "
3952+
<< printMBBReference(*MBB) << " has exit state " << BBState.Exit
3953+
<< ".\n";
39653954
}
39663955
}
39673956

39683957
// Make sure a basic block with return ends with zero stack adjustment.
39693958
if (!MBB->empty() && MBB->back().isReturn()) {
3970-
if (BBState.ExitIsSetup)
3959+
if (BBState.Exit.has_value())
39713960
report("A return block ends with a FrameSetup.", MBB);
3972-
if (BBState.ExitValue)
3973-
report("A return block ends with a nonzero stack adjustment.", MBB);
39743961
}
39753962
}
39763963
}

0 commit comments

Comments
 (0)