[LLVMdev] deleting or replacing a MachineInst

Mehdi Amini mehdi.amini at apple.com
Tue Feb 10 21:46:24 PST 2015


> On Feb 10, 2015, at 9:25 PM, Chris Sears <chris.sears at gmail.com> wrote:
> 
> 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.

Don’t you have a problem of iterator invalidation?
How do you loop through the block?
If you can post the full body of the loop (if not too large), or a simpler version that has the bug.

— 
Mehdi



> 
>   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.
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





More information about the llvm-dev mailing list