[LowerSwitch] Fix a bug when LowerSwitch deletes the default block
authorChen Li <meloli87@gmail.com>
Tue, 11 Aug 2015 18:12:26 +0000 (18:12 +0000)
committerChen Li <meloli87@gmail.com>
Tue, 11 Aug 2015 18:12:26 +0000 (18:12 +0000)
commita0180252f10152ae98e6e4926b3c2d772f881b90
tree2fb0939beb5339412bd46a69e0ee531b1ee1bf32
parent2f3b47dd617e02e31d0b32e5a647ddf3cf545c9a
[LowerSwitch] Fix a bug when LowerSwitch deletes the default block

Summary: 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.

Reviewers: kariddi, resistor, hans, reames

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D11852

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244642 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/Utils/LowerSwitch.cpp
test/Transforms/LowerSwitch/delete-default-block-crash.ll [new file with mode: 0644]