@@ -6743,8 +6743,15 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6743
6743
TableSize =
6744
6744
(MaxCaseVal->getValue () - MinCaseVal->getValue ()).getLimitedValue () + 1 ;
6745
6745
6746
+ // If the default destination is unreachable, or if the lookup table covers
6747
+ // all values of the conditional variable, branch directly to the lookup table
6748
+ // BB. Otherwise, check that the condition is within the case range.
6749
+ bool DefaultIsReachable =
6750
+ !isa<UnreachableInst>(SI->getDefaultDest ()->getFirstNonPHIOrDbg ());
6751
+
6746
6752
bool TableHasHoles = (NumResults < TableSize);
6747
- bool NeedMask = (TableHasHoles && !HasDefaultResults);
6753
+ bool HolesHaveUndefinedResults = (TableHasHoles && !HasDefaultResults);
6754
+ bool NeedMask = (HolesHaveUndefinedResults && DefaultIsReachable);
6748
6755
if (NeedMask) {
6749
6756
// As an extra penalty for the validity test we require more cases.
6750
6757
if (SI->getNumCases () < 4 ) // FIXME: Find best threshold value (benchmark).
@@ -6766,12 +6773,6 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6766
6773
" It is impossible for a switch to have more entries than the max "
6767
6774
" representable value of its input integer type's size." );
6768
6775
6769
- // If the default destination is unreachable, or if the lookup table covers
6770
- // all values of the conditional variable, branch directly to the lookup table
6771
- // BB. Otherwise, check that the condition is within the case range.
6772
- bool DefaultIsReachable =
6773
- !isa<UnreachableInst>(SI->getDefaultDest ()->getFirstNonPHIOrDbg ());
6774
-
6775
6776
// Create the BB that does the lookups.
6776
6777
Module &Mod = *CommonDest->getParent ()->getParent ();
6777
6778
BasicBlock *LookupBB = BasicBlock::Create (
@@ -6895,8 +6896,9 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6895
6896
for (PHINode *PHI : PHIs) {
6896
6897
const ResultListTy &ResultList = ResultLists[PHI];
6897
6898
6898
- // If using a bitmask, use any value to fill the lookup table holes.
6899
- Constant *DV = NeedMask ? ResultLists[PHI][0 ].second : DefaultResults[PHI];
6899
+ // Use any value to fill the lookup table holes.
6900
+ Constant *DV = HolesHaveUndefinedResults ? ResultLists[PHI][0 ].second
6901
+ : DefaultResults[PHI];
6900
6902
StringRef FuncName = Fn->getName ();
6901
6903
SwitchLookupTable Table (Mod, TableSize, TableIndexOffset, ResultList, DV,
6902
6904
DL, FuncName);
0 commit comments