MachineInstr *MI = ...; const MachineOperand &MO = MI->getOperand(0); if (MO.isReg()) { unsigned Reg = MO.getReg(); // Do something with the register number... }
void MyPass::runOnMachineFunction(MachineFunction &MF) { for (MachineBasicBlock &MBB : MF) { for (MachineInstr &MI : MBB) { unsigned NumOps = MI.getNumOperands(); for (unsigned i = 0; i < NumOps; ++i) { const MachineOperand &MO = MI.getOperand(i); if (MO.isReg()) { unsigned Reg = MO.getReg(); // Do something with the register number... } } } } }In this example, we have a custom LLVM pass that analyzes a MachineFunction (not shown). We iterate over each basic block in the function and then each instruction in each block. For each instruction, we iterate over all of its operands and use getReg to retrieve the register number for any register operands we find. Package/library: LLVM/LLVM-C++ API.