[LLVMdev] deleting or replacing a MachineInst

Chris Sears chris.sears at gmail.com
Tue Feb 10 21:25:37 PST 2015


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.

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:

  NewMI = BuildMI(*MBB, MBBI, MBBI->getDebugLoc(), TII->get(X86::opcode))
    .addReg(X86::new_reg, kill)
    .addImm(i);

This works and it correctly places the new instruction just before the old
instruction in the assembly output. So far so good.

Now I have to remove the old instruction. But everything I try crashes
LLVM, either immediately or eventually. Various incantations which haven't
worked.

  MBB->remove_instr(OldMI);
  OldMI->removeFromParent();
  MBB.erase(OldMI);

I should add that there are flags

      // %EFLAGS<imp-def>   is getting copied automatically
      // %RDX<imp-use,kill> is not getting copied (when it appears)

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150210/b7b2cb98/attachment.html>


More information about the llvm-dev mailing list