Skip to content

Commit c538f3a

Browse files
scottp101sys_zuul
authored and
sys_zuul
committed
FlattenSmallSwitch: Ensure that all case blocks in the
switch have the switch block as their unique predecessor to ensure that the switch block dominates the merge block. Change-Id: I4dd9096498a0d930ce0a15b8468bd18df9e01775
1 parent 121b810 commit c538f3a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

IGC/Compiler/CustomSafeOptPass.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,9 +3875,11 @@ IGC_INITIALIZE_PASS_BEGIN(GenStrengthReduction, "GenStrengthReduction",
38753875
This class flatten small switch. For example,
38763876
38773877
before optimization:
3878+
then153:
38783879
switch i32 %115, label %else229 [
38793880
i32 1, label %then214
38803881
i32 2, label %then222
3882+
i32 3, label %then222 ; duplicate blocks are fine
38813883
]
38823884
38833885
then214: ; preds = %then153
@@ -3981,6 +3983,9 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
39813983
if (br->getSuccessor(0) != MergeBlock)
39823984
return false;
39833985

3986+
if (!BB->getUniquePredecessor())
3987+
return false;
3988+
39843989
return true;
39853990
};
39863991

@@ -4059,14 +4064,13 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
40594064
// from BB to the InsertPoint.
40604065
auto splice = [](BasicBlock* BB, Instruction* InsertPoint)
40614066
{
4062-
Instruction* preIter = nullptr;
4063-
for (auto& iter : *BB)
4067+
for (auto II = BB->begin(), IE = BB->end(); II != IE; /* empty */)
40644068
{
4065-
if (preIter)
4066-
{
4067-
preIter->moveBefore(InsertPoint);
4068-
}
4069-
preIter = cast<Instruction>(&iter);
4069+
auto* I = &*II++;
4070+
if (I->isTerminator())
4071+
return;
4072+
4073+
I->moveBefore(InsertPoint);
40704074
}
40714075
};
40724076

@@ -4105,6 +4109,8 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
41054109
// connect the original block and the phi node block with a pass through branch
41064110
builder.CreateBr(Dest);
41074111

4112+
SmallPtrSet<BasicBlock*, 4> Succs;
4113+
41084114
// Remove the switch.
41094115
BasicBlock* SelectBB = SI->getParent();
41104116
for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i)
@@ -4114,7 +4120,9 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
41144120
{
41154121
continue;
41164122
}
4117-
Succ->removePredecessor(SelectBB);
4123+
4124+
if (Succs.insert(Succ).second)
4125+
Succ->removePredecessor(SelectBB);
41184126
}
41194127
SI->eraseFromParent();
41204128

0 commit comments

Comments
 (0)