Question

I want to remove an UnreachableInst since a previous transformation has made it reachable. However, calling eraseFromParent() gives me a malformed BasicBlock since the UnreachableInst is the terminator of its BasicBlock. How do I fix the BasicBlock to terminate at the instruction previous to the UnreachableInst?

Was it helpful?

Solution

Every basic block must end with a terminator. I think that the most straightforward way to remove the unreachable instruction, then, is to replace it with another terminator - for example, a branch into the next basic block, a return instruction, etc.

Take a look at llvm::ReplaceInstWithInst in BasicBlockUtils.h for a convenient way to replace one instruction with another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top