From: Michael Liao Date: Tue, 17 Mar 2015 18:03:10 +0000 (+0000) Subject: [SwitchLowering] Remove incoming values in the reverse order X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=de3983775efdbcd0121bcaf65a6d45d21a7fb269;p=oota-llvm.git [SwitchLowering] Remove incoming values in the reverse order - To prevent invalidating *successive* indices. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232510 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Utils/LowerSwitch.cpp b/lib/Transforms/Utils/LowerSwitch.cpp index b3bdae47d94..7bfb3d9f4d4 100644 --- a/lib/Transforms/Utils/LowerSwitch.cpp +++ b/lib/Transforms/Utils/LowerSwitch.cpp @@ -175,11 +175,16 @@ static void fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB, // Remove additional occurences coming from condensed cases and keep the // number of incoming values equal to the number of branches to SuccBB. + SmallVector Indices; for (++Idx; LocalNumMergedCases > 0 && Idx < E; ++Idx) if (PN->getIncomingBlock(Idx) == OrigBB) { - PN->removeIncomingValue(Idx); + Indices.push_back(Idx); LocalNumMergedCases--; } + // Remove incoming values in the reverse order to prevent invalidating + // *successive* index. + for (auto III = Indices.rbegin(), IIE = Indices.rend(); III != IIE; ++III) + PN->removeIncomingValue(*III); } } diff --git a/test/Transforms/Util/lowerswitch.ll b/test/Transforms/Util/lowerswitch.ll index 17c12028705..1eddb43c1a0 100644 --- a/test/Transforms/Util/lowerswitch.ll +++ b/test/Transforms/Util/lowerswitch.ll @@ -52,3 +52,137 @@ bb3: exit: ret void } + +; Test that we don't crash. +define void @test2(i32 %mode) { +; CHECK-LABEL: @test2 + br i1 undef, label %1, label %._crit_edge + +;