@@ -3875,9 +3875,11 @@ IGC_INITIALIZE_PASS_BEGIN(GenStrengthReduction, "GenStrengthReduction",
3875
3875
This class flatten small switch. For example,
3876
3876
3877
3877
before optimization:
3878
+ then153:
3878
3879
switch i32 %115, label %else229 [
3879
3880
i32 1, label %then214
3880
3881
i32 2, label %then222
3882
+ i32 3, label %then222 ; duplicate blocks are fine
3881
3883
]
3882
3884
3883
3885
then214: ; preds = %then153
@@ -3981,6 +3983,9 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
3981
3983
if (br->getSuccessor (0 ) != MergeBlock)
3982
3984
return false ;
3983
3985
3986
+ if (!BB->getUniquePredecessor ())
3987
+ return false ;
3988
+
3984
3989
return true ;
3985
3990
};
3986
3991
@@ -4059,14 +4064,13 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
4059
4064
// from BB to the InsertPoint.
4060
4065
auto splice = [](BasicBlock* BB, Instruction* InsertPoint)
4061
4066
{
4062
- Instruction* preIter = nullptr ;
4063
- for (auto & iter : *BB)
4067
+ for (auto II = BB->begin (), IE = BB->end (); II != IE; /* empty */ )
4064
4068
{
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 );
4070
4074
}
4071
4075
};
4072
4076
@@ -4105,6 +4109,8 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
4105
4109
// connect the original block and the phi node block with a pass through branch
4106
4110
builder.CreateBr (Dest);
4107
4111
4112
+ SmallPtrSet<BasicBlock*, 4 > Succs;
4113
+
4108
4114
// Remove the switch.
4109
4115
BasicBlock* SelectBB = SI->getParent ();
4110
4116
for (unsigned i = 0 , e = SI->getNumSuccessors (); i < e; ++i)
@@ -4114,7 +4120,9 @@ bool FlattenSmallSwitch::processSwitchInst(SwitchInst* SI)
4114
4120
{
4115
4121
continue ;
4116
4122
}
4117
- Succ->removePredecessor (SelectBB);
4123
+
4124
+ if (Succs.insert (Succ).second )
4125
+ Succ->removePredecessor (SelectBB);
4118
4126
}
4119
4127
SI->eraseFromParent ();
4120
4128
0 commit comments