Skip to content

Commit 57ad621

Browse files
author
Joe Shajrawi
committed
[SR-2512] Ignore duplicate conditions when optimizing switch statement
1 parent efa6dd9 commit 57ad621

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,7 @@ bool simplifyToSelectValue(SILBasicBlock *MergeBlock, unsigned ArgNum,
31903190
}
31913191

31923192
SmallVector<std::pair<SILValue, SILValue>, 8> Cases;
3193+
llvm::SmallDenseMap<SILValue, SILValue> CaseLiteralsToResultMap;
31933194
SILValue defaultResult;
31943195

31953196
// The block of the first input value compare. It dominates all other blocks
@@ -3202,7 +3203,23 @@ bool simplifyToSelectValue(SILBasicBlock *MergeBlock, unsigned ArgNum,
32023203
auto *BrInst = cast<CondBranchInst>(CaseInfo.CmpOrDefault->getTerminator());
32033204
if (FoundCmpBlocks.count(BrInst->getFalseBB()) != 1)
32043205
return false;
3205-
Cases.push_back({CaseInfo.Literal, CaseInfo.Result});
3206+
// Ignore duplicate cases
3207+
if (CaseLiteralsToResultMap.find(CaseInfo.Literal) ==
3208+
CaseLiteralsToResultMap.end()) {
3209+
CaseLiteralsToResultMap.insert({CaseInfo.Literal, CaseInfo.Result});
3210+
Cases.push_back({CaseInfo.Literal, CaseInfo.Result});
3211+
} else {
3212+
// Check if the result value matches
3213+
EnumInst *PrevResult =
3214+
dyn_cast<EnumInst>(CaseLiteralsToResultMap[CaseInfo.Literal]);
3215+
assert(PrevResult && "Prev. case result is not an EnumInst");
3216+
EnumInst *CurrResult = dyn_cast<EnumInst>(CaseInfo.Result);
3217+
assert(CurrResult && "Curr. case result is not an EnumInst");
3218+
if (PrevResult->getElement() != CurrResult->getElement()) {
3219+
// result value does not match - bail
3220+
return false;
3221+
}
3222+
}
32063223
SILBasicBlock *Pred = CaseInfo.CmpOrDefault->getSinglePredecessor();
32073224
if (!Pred || FoundCmpBlocks.count(Pred) == 0) {
32083225
// There may be only a single block whose predecessor we didn't see. And

0 commit comments

Comments
 (0)