<div dir="ltr">I'm writing a peephole pass and I'm done with the X86_64 instruction level detail work. But I'm having difficulty with the basic block surgery of replacing the old MachineInst.<div><br></div><div>The peephole pass gets called per MachineFunction and then iterates over each MachineBasicBlock and in turn over each MachineInst. When it finds an instruction which should be replaced, it builds a new instruction:
<div><br></div><div><div>  NewMI = BuildMI(*MBB, MBBI, MBBI->getDebugLoc(), TII->get(X86::opcode))<br></div><div>    .addReg(X86::new_reg, kill)</div><div>    .addImm(i);</div></div><div><br></div><div>This works and it correctly places the new instruction just before the old instruction in the assembly output. So far so good.</div><div><br></div><div>Now I have to remove the old instruction. But everything I try crashes LLVM, either immediately or eventually. Various incantations which haven't worked.</div><div><br></div><div><div>  MBB->remove_instr(OldMI);</div><div>  OldMI->removeFromParent();</div><div>  MBB.erase(OldMI);</div></div><div><br></div><div>I should add that there are flags</div><div><br></div><div><div>      // %EFLAGS<imp-def>   is getting copied automatically</div><div>      // %RDX<imp-use,kill> is not getting copied (when it appears)</div></div><div><br></div><div>So, any advice to peephole adding/deleting or just replacing MachineInst's ? I've looked over the other peephole optimizers. None seem to direct replacement. You'd think there'd be a MachineInst method.</div></div></div>