void MyPass::runOnMachineFunction(MachineFunction &MF) { for (MachineBasicBlock &MBB : MF) { for (MachineInstr &MI : MBB) { // Do something with MI } } }
void MyPass::runOnMachineFunction(MachineFunction &MF) { for (MachineBasicBlock &MBB : MF) { for (MachineInstr &MI : MBB) { if (MI.isBranch()) { // Replace the branch instruction MachineInstrBuilder MIB = BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(X86::JMP_1)); MIB.add(MI.getOperand(0)); MIB.addImm(X86::COND_E); MI.eraseFromParent(); } } } }This example shows how to replace a branch instruction with a jump instruction. It uses the BuildMI function to create a new jump instruction, adds operands to it, and then erases the original instruction. Package/Library: LLVM (the Low Level Virtual Machine), specifically the LLVM Target library.