Skip to content

Commit a58d67a

Browse files
committed
Add an MRI::verifyUseLists() function.
This checks the sanity of the register use lists in the MI intermediate representation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179895 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 03494e0 commit a58d67a

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

include/llvm/CodeGen/MachineRegisterInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ class MachineRegisterInfo {
157157
// Strictly for use by MachineInstr.cpp.
158158
void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
159159

160+
/// Verify the sanity of the use list for Reg.
161+
void verifyUseList(unsigned Reg) const;
162+
163+
/// Verify the use list of all registers.
164+
void verifyUseLists() const;
165+
160166
/// reg_begin/reg_end - Provide iteration support to walk over all definitions
161167
/// and uses of a register within the MachineFunction that corresponds to this
162168
/// MachineRegisterInfo object.

lib/CodeGen/MachineRegisterInfo.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "llvm/CodeGen/MachineInstrBuilder.h"
1616
#include "llvm/Target/TargetInstrInfo.h"
1717
#include "llvm/Target/TargetMachine.h"
18+
#include "llvm/Support/raw_os_ostream.h"
19+
1820
using namespace llvm;
1921

2022
MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
@@ -106,13 +108,59 @@ MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
106108
/// clearVirtRegs - Remove all virtual registers (after physreg assignment).
107109
void MachineRegisterInfo::clearVirtRegs() {
108110
#ifndef NDEBUG
109-
for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
110-
assert(VRegInfo[TargetRegisterInfo::index2VirtReg(i)].second == 0 &&
111-
"Vreg use list non-empty still?");
111+
for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
112+
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
113+
if (!VRegInfo[Reg].second)
114+
continue;
115+
verifyUseList(Reg);
116+
llvm_unreachable("Remaining virtual register operands");
117+
}
112118
#endif
113119
VRegInfo.clear();
114120
}
115121

122+
void MachineRegisterInfo::verifyUseList(unsigned Reg) const {
123+
#ifndef NDEBUG
124+
bool Valid = true;
125+
for (reg_iterator I = reg_begin(Reg), E = reg_end(); I != E; ++I) {
126+
MachineOperand *MO = &I.getOperand();
127+
MachineInstr *MI = MO->getParent();
128+
if (!MI) {
129+
errs() << PrintReg(Reg, TRI) << " use list MachineOperand " << MO
130+
<< " has no parent instruction.\n";
131+
Valid = false;
132+
}
133+
MachineOperand *MO0 = &MI->getOperand(0);
134+
unsigned NumOps = MI->getNumOperands();
135+
if (!(MO >= MO0 && MO < MO0+NumOps)) {
136+
errs() << PrintReg(Reg, TRI) << " use list MachineOperand " << MO
137+
<< " doesn't belong to parent MI: " << *MI;
138+
Valid = false;
139+
}
140+
if (!MO->isReg()) {
141+
errs() << PrintReg(Reg, TRI) << " MachineOperand " << MO << ": " << *MO
142+
<< " is not a register\n";
143+
Valid = false;
144+
}
145+
if (MO->getReg() != Reg) {
146+
errs() << PrintReg(Reg, TRI) << " use-list MachineOperand " << MO << ": "
147+
<< *MO << " is the wrong register\n";
148+
Valid = false;
149+
}
150+
}
151+
assert(Valid && "Invalid use list");
152+
#endif
153+
}
154+
155+
void MachineRegisterInfo::verifyUseLists() const {
156+
#ifndef NDEBUG
157+
for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
158+
verifyUseList(TargetRegisterInfo::index2VirtReg(i));
159+
for (unsigned i = 1, e = TRI->getNumRegs(); i != e; ++i)
160+
verifyUseList(i);
161+
#endif
162+
}
163+
116164
/// Add MO to the linked list of operands for its register.
117165
void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) {
118166
assert(!MO->isOnRegUseList() && "Already on list");

lib/CodeGen/MachineVerifier.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ void MachineVerifier::visitMachineFunctionBefore() {
472472
if (MInfo.Succs.size() != I->succ_size())
473473
report("MBB has duplicate entries in its successor list.", I);
474474
}
475+
476+
// Check that the register use lists are sane.
477+
MRI->verifyUseLists();
475478
}
476479

477480
// Does iterator point to a and b as the first two elements?

0 commit comments

Comments
 (0)