LowerSwitch crashed with the attached test case after deleting the default block. This happened because the current implementation of deleting dead blocks is wrong. After the default block being deleted, it contains no instruction or terminator, and it should no be traversed anymore. However, since the iterator is advanced before processSwitchInst() function is executed, the block advanced to could be deleted inside processSwitchInst(). The deleted block would then be visited next and crash dyn_cast<SwitchInst>(Cur->getTerminator()) because Cur->getTerminator() returns a nullptr. This patch fixes this problem by recording dead default blocks into a list, and delete them after all processSwitchInst() has been done. It still possible to visit dead default blocks and waste time process them. But it is a compile time issue, and I plan to have another patch to add support to skip dead blocks.
Details
Diff Detail
Event Timeline
lib/Transforms/Utils/LowerSwitch.cpp | ||
---|---|---|
526–527 | I don't think maintaining a DeleteList is very nice, and as you say it has the problem that we might call processSwitchInst on a block even if it's dead. How about we remove the DeleteDeadBlock call here, and in the loop in runOnFunction(), we add something like: if (block has no predecessors) { delete it continue } |
lib/Transforms/Utils/LowerSwitch.cpp | ||
---|---|---|
526–527 | Yes, I think that should work too. Though we need to handle carefully with entry block, which also has no predecessors. |
The revised patch is strictly less powerful than the current code and the previous patch. If a block occurs earlier in the iteration order than the only block which reaches it and we then delete the reaching edge, we will now fail to remove the unreachable block. I would prefer the first patch posted over the revised one.
Yes, you are correct. I didn't think about this case. I will submit the initial patch if Hans is ok with that.
I don't think maintaining a DeleteList is very nice, and as you say it has the problem that we might call processSwitchInst on a block even if it's dead.
How about we remove the DeleteDeadBlock call here, and in the loop in runOnFunction(), we add something like: