Thanks for your reply.
That’s exactly what I am doing, but I was looking for a default optimization or pass implementation if there was.
I used BasicBlock::splitBasicBlock() but it puts “br” end of original basic block. I tried to delete the br instruction by using eraseFromParent() but it didn’t work.
I had to rewrite my own splitBasicBlock() by modifying the original one. Just removed the br instruction creation lines.
Just curious, how are you getting return instructions in the middle of a basic block?
My code generation pass allows multiple return instruction generation since that simplifies front-end significantly.
Here is the code if anyone would like to use it:
void CodeGenPass::DeleteDeadCode(llvm::BasicBlock * basicBlock)
for (auto it = basicBlock->begin(); it != basicBlock->end(); ++it)
// Split after first return instruction.
if (it->getOpcode() == llvm::Instruction::Ret)
// Split only if there is a following instruction.
if (it != basicBlock->getInstList().end())
auto deadCodeBlock = SplitBasicBlock(basicBlock, it);
llvm::BasicBlock * CodeGenPass::SplitBasicBlock(llvm::BasicBlock * basicBlock, llvm::BasicBlock::iterator it)
assert(basicBlock->getTerminator() && “Block must have terminator instruction.”);
assert(it != basicBlock->getInstList().end() && “Can’t split block since there is no following instruction in the basic block.”);
auto newBlock = llvm::BasicBlock::Create(basicBlock->getContext(), “splitedBlock”, basicBlock->getParent(), basicBlock->getNextNode());
// Move all of the instructions from original block into new block.
newBlock->getInstList().splice(newBlock->end(), basicBlock->getInstList(), it, basicBlock->end());
// Now we must loop through all of the successors of the New block (which
// were the successors of the ‘this’ block), and update any PHI nodes in
// successors. If there were PHI nodes in the successors, then they need to
// know that incoming branches will be from New, not from Old.
for (llvm::succ_iterator I = llvm::succ_begin(newBlock), E = llvm::succ_end(newBlock); I != E; ++I)
// Loop over any phi nodes in the basic block, updating the BB field of
// incoming values…
llvm::BasicBlock *Successor = *I;
for (auto &PN : Successor->phis())
int Idx = PN.getBasicBlockIndex(basicBlock);
while (Idx != -1)
Idx = PN.getBasicBlockIndex(basicBlock);