Skip to content

Commit b30b9eb

Browse files
authored
LiveInterval: Make verify functions return bool (#109672)
This will allow the MachineVerifier to check these properties instead of just asserting
1 parent 0206181 commit b30b9eb

File tree

7 files changed

+73
-32
lines changed

7 files changed

+73
-32
lines changed

llvm/include/llvm/CodeGen/LiveInterval.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,9 @@ namespace llvm {
662662
///
663663
/// Note that this is a no-op when asserts are disabled.
664664
#ifdef NDEBUG
665-
void verify() const {}
665+
[[nodiscard]] bool verify() const { return true; }
666666
#else
667-
void verify() const;
667+
[[nodiscard]] bool verify() const;
668668
#endif
669669

670670
protected:
@@ -893,9 +893,11 @@ namespace llvm {
893893
///
894894
/// Note that this is a no-op when asserts are disabled.
895895
#ifdef NDEBUG
896-
void verify(const MachineRegisterInfo *MRI = nullptr) const {}
896+
[[nodiscard]] bool verify(const MachineRegisterInfo *MRI = nullptr) const {
897+
return true;
898+
}
897899
#else
898-
void verify(const MachineRegisterInfo *MRI = nullptr) const;
900+
[[nodiscard]] bool verify(const MachineRegisterInfo *MRI = nullptr) const;
899901
#endif
900902

901903
private:

llvm/lib/CodeGen/LiveInterval.cpp

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ void LiveRange::join(LiveRange &Other,
630630
const int *LHSValNoAssignments,
631631
const int *RHSValNoAssignments,
632632
SmallVectorImpl<VNInfo *> &NewVNInfo) {
633-
verify();
634-
Other.verify();
633+
assert(verify());
634+
assert(Other.verify());
635635

636636
// Determine if any of our values are mapped. This is uncommon, so we want
637637
// to avoid the range scan if not.
@@ -797,7 +797,7 @@ void LiveRange::flushSegmentSet() {
797797
"segment set can be used only initially before switching to the array");
798798
segments.append(segmentSet->begin(), segmentSet->end());
799799
segmentSet = nullptr;
800-
verify();
800+
assert(verify());
801801
}
802802

803803
bool LiveRange::isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const {
@@ -1055,43 +1055,65 @@ LLVM_DUMP_METHOD void LiveInterval::dump() const {
10551055
#endif
10561056

10571057
#ifndef NDEBUG
1058-
void LiveRange::verify() const {
1058+
bool LiveRange::verify() const {
10591059
for (const_iterator I = begin(), E = end(); I != E; ++I) {
1060-
assert(I->start.isValid());
1061-
assert(I->end.isValid());
1062-
assert(I->start < I->end);
1063-
assert(I->valno != nullptr);
1064-
assert(I->valno->id < valnos.size());
1065-
assert(I->valno == valnos[I->valno->id]);
1060+
if (!I->start.isValid())
1061+
return false;
1062+
if (!I->end.isValid())
1063+
return false;
1064+
if (I->start >= I->end)
1065+
return false;
1066+
if (I->valno == nullptr)
1067+
return false;
1068+
if (I->valno->id >= valnos.size())
1069+
return false;
1070+
if (I->valno != valnos[I->valno->id])
1071+
return false;
10661072
if (std::next(I) != E) {
1067-
assert(I->end <= std::next(I)->start);
1068-
if (I->end == std::next(I)->start)
1069-
assert(I->valno != std::next(I)->valno);
1073+
if (I->end > std::next(I)->start)
1074+
return false;
1075+
if (I->end == std::next(I)->start) {
1076+
if (I->valno == std::next(I)->valno)
1077+
return false;
1078+
}
10701079
}
10711080
}
1081+
1082+
return true;
10721083
}
10731084

1074-
void LiveInterval::verify(const MachineRegisterInfo *MRI) const {
1075-
super::verify();
1085+
bool LiveInterval::verify(const MachineRegisterInfo *MRI) const {
1086+
if (!super::verify())
1087+
return false;
10761088

10771089
// Make sure SubRanges are fine and LaneMasks are disjunct.
10781090
LaneBitmask Mask;
10791091
LaneBitmask MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg())
10801092
: LaneBitmask::getAll();
10811093
for (const SubRange &SR : subranges()) {
10821094
// Subrange lanemask should be disjunct to any previous subrange masks.
1083-
assert((Mask & SR.LaneMask).none());
1095+
if ((Mask & SR.LaneMask).any())
1096+
return false;
1097+
10841098
Mask |= SR.LaneMask;
10851099

10861100
// subrange mask should not contained in maximum lane mask for the vreg.
1087-
assert((Mask & ~MaxMask).none());
1101+
if ((Mask & ~MaxMask).any())
1102+
return false;
1103+
10881104
// empty subranges must be removed.
1089-
assert(!SR.empty());
1105+
if (SR.empty())
1106+
return false;
1107+
1108+
if (!SR.verify())
1109+
return false;
10901110

1091-
SR.verify();
10921111
// Main liverange should cover subrange.
1093-
assert(covers(SR));
1112+
if (!covers(SR))
1113+
return false;
10941114
}
1115+
1116+
return true;
10951117
}
10961118
#endif
10971119

@@ -1283,7 +1305,7 @@ void LiveRangeUpdater::flush() {
12831305
// Nothing to merge?
12841306
if (Spills.empty()) {
12851307
LR->segments.erase(WriteI, ReadI);
1286-
LR->verify();
1308+
assert(LR->verify());
12871309
return;
12881310
}
12891311

@@ -1301,7 +1323,7 @@ void LiveRangeUpdater::flush() {
13011323
}
13021324
ReadI = WriteI + Spills.size();
13031325
mergeSpills();
1304-
LR->verify();
1326+
assert(LR->verify());
13051327
}
13061328

13071329
unsigned ConnectedVNInfoEqClasses::Classify(const LiveRange &LR) {

llvm/lib/CodeGen/LiveIntervals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ class LiveIntervals::HMEditor {
11051105
else
11061106
handleMoveUp(LR, Reg, LaneMask);
11071107
LLVM_DEBUG(dbgs() << " -->\t" << LR << '\n');
1108-
LR.verify();
1108+
assert(LR.verify());
11091109
}
11101110

11111111
/// Update LR to reflect an instruction has been moved downwards from OldIdx

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,15 @@ void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
27582758
Register VRegOrUnit,
27592759
LaneBitmask LaneMask) {
27602760
const MachineInstr *MI = MO->getParent();
2761+
2762+
if (!LR.verify()) {
2763+
report("invalid live range", MO, MONum);
2764+
report_context_liverange(LR);
2765+
report_context_vreg_regunit(VRegOrUnit);
2766+
report_context(UseIdx);
2767+
return;
2768+
}
2769+
27612770
LiveQueryResult LRQ = LR.Query(UseIdx);
27622771
bool HasValue = LRQ.valueIn() || (MI->isPHI() && LRQ.valueOut());
27632772
// Check if we have a segment at the use, note however that we only need one
@@ -2784,6 +2793,15 @@ void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
27842793
Register VRegOrUnit,
27852794
bool SubRangeCheck,
27862795
LaneBitmask LaneMask) {
2796+
if (!LR.verify()) {
2797+
report("invalid live range", MO, MONum);
2798+
report_context_liverange(LR);
2799+
report_context_vreg_regunit(VRegOrUnit);
2800+
if (LaneMask.any())
2801+
report_context_lanemask(LaneMask);
2802+
report_context(DefIdx);
2803+
}
2804+
27872805
if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
27882806
// The LR can correspond to the whole reg and its def slot is not obliged
27892807
// to be the same as the MO' def slot. E.g. when we check here "normal"

llvm/lib/CodeGen/RegisterCoalescer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,8 +3548,7 @@ void RegisterCoalescer::joinSubRegRanges(LiveRange &LRange, LiveRange &RRange,
35483548
LHSVals.removeImplicitDefs();
35493549
RHSVals.removeImplicitDefs();
35503550

3551-
LRange.verify();
3552-
RRange.verify();
3551+
assert(LRange.verify() && RRange.verify());
35533552

35543553
// Join RRange into LHS.
35553554
LRange.join(RRange, LHSVals.getAssignments(), RHSVals.getAssignments(),

llvm/lib/Target/AMDGPU/GCNRewritePartialRegUses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ void GCNRewritePartialRegUses::updateLiveIntervals(Register OldReg,
397397
}
398398
if (NewLI.empty())
399399
NewLI.assign(OldLI, Allocator);
400-
NewLI.verify(MRI);
400+
assert(NewLI.verify(MRI));
401401
LIS->removeInterval(OldReg);
402402
}
403403

llvm/lib/Target/Hexagon/HexagonExpandCondsets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ void HexagonExpandCondsets::updateLiveness(const std::set<Register> &RegSet,
568568
// after that.
569569
if (UpdateKills)
570570
updateKillFlags(R);
571-
LIS->getInterval(R).verify();
571+
assert(LIS->getInterval(R).verify());
572572
}
573573
}
574574

@@ -1197,7 +1197,7 @@ bool HexagonExpandCondsets::coalesceRegisters(RegisterRef R1, RegisterRef R2) {
11971197

11981198
updateKillFlags(R1.Reg);
11991199
LLVM_DEBUG(dbgs() << "coalesced: " << L1 << "\n");
1200-
L1.verify();
1200+
assert(L1.verify());
12011201

12021202
return true;
12031203
}

0 commit comments

Comments
 (0)