[llvm-dev] LLVM Pass To Remove Dead Code In A Basic Block

Dean Michael Berris via llvm-dev llvm-dev at lists.llvm.org
Thu May 24 09:22:54 PDT 2018



> On 25 May 2018, at 01:46, Aaron via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi all,
> 
> LLVM optimization pass gives an error "Terminator found in the middle of a basic block!" since basic block IR may have multiple "ret" instructions. It seems LLVM does not accept multiple return in a basic block by default. 
> 

Yes, if you’re inserting terminators in a basic block at the IR level, you’re going to have to split it into two different blocks yourself. Essentially you’re encountering a legaliser failure here — it’s an invariant of a BasicBlock that there are no terminator instructions in the body, and the last instruction should be a terminator.

> Is there a specific optimization or pass that I can enable to remove unreachable codes in basic blocks?
> 

If you’re writing your own pass, you can do this yourself — after you insert the return instruction, just delete the rest of the instructions in the basic block after that instruction.

Just curious, how are you getting return instructions in the middle of a basic block?

> Best,
> 
> Aaron
> 
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

-- Dean



More information about the llvm-dev mailing list