Null pointer reference in llvm::MachineOperand::print

First of all the code in question:

void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
const TargetRegisterInfo *TRI) const {

case MachineOperand::MO_RegisterMask: {
unsigned NumRegsInMask = 0;
unsigned NumRegsEmitted = 0;
OS << “<regmask”;
for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {


This looks innocent enough, but this is in turn called by:

void MachineOperand::print(raw_ostream &OS,
const TargetRegisterInfo *TRI) const {
ModuleSlotTracker DummyMST(nullptr);
print(OS, DummyMST, TRI);


inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
MO.print(OS, nullptr);
return OS;

Notice the nullptr as the const TargetRegisterInfo* argument.
As a result, when TRI->getNumRegs() gets called later on, we get a nullptr dereference.

There seems to be a couple of couple of possibilities here:
a) The wrong case in the switch has been selected due to a bug elsewhere
b) The case in question should be checking for a nullptr

I encountered this whilst iterating through instructions in a MachineBasicBlock.
Does anybody have any insight?