[llvm-dev] Null pointer reference in llvm::MachineOperand::print

Joshua Gerrard via llvm-dev llvm-dev at lists.llvm.org
Thu Dec 3 00:42:43 PST 2015

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

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
Does anybody have any insight?

Joshua Gerrard
JUCE Software Developer

*ROLI’s **award-winning*
GRAND, celebrated as the “**piano of the future*
is now joined by the **Seaboard RISE*
<https://www.youtube.com/watch?v=fGr7VbDiRNw>*, “**every bit as slimline
and attractive as its bigger brother*
<http://www.soundonsound.com/news?NewsID=18726>*”. The press is hailing the
Seaboard RISE as “**innovative*
and “**a keyboard controller that does to piano keys what 3D touch does to
the iPhone*
Now available for preorder at **www.roli.com* <http://www.roli.com/>*.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20151203/ab3425a2/attachment.html>

More information about the llvm-dev mailing list