@@ -6743,8 +6743,14 @@ 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 = !SI->defaultDestUndefined ();
6750
+
6746
6751
bool TableHasHoles = (NumResults < TableSize);
6747
- bool NeedMask = (TableHasHoles && !HasDefaultResults);
6752
+ bool HolesHaveUndefinedResults = (TableHasHoles && !HasDefaultResults);
6753
+ bool NeedMask = (HolesHaveUndefinedResults && DefaultIsReachable);
6748
6754
if (NeedMask) {
6749
6755
// As an extra penalty for the validity test we require more cases.
6750
6756
if (SI->getNumCases () < 4 ) // FIXME: Find best threshold value (benchmark).
@@ -6766,12 +6772,6 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6766
6772
" It is impossible for a switch to have more entries than the max "
6767
6773
" representable value of its input integer type's size." );
6768
6774
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
6775
// Create the BB that does the lookups.
6776
6776
Module &Mod = *CommonDest->getParent ()->getParent ();
6777
6777
BasicBlock *LookupBB = BasicBlock::Create (
@@ -6895,8 +6895,9 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6895
6895
for (PHINode *PHI : PHIs) {
6896
6896
const ResultListTy &ResultList = ResultLists[PHI];
6897
6897
6898
- // If using a bitmask, use any value to fill the lookup table holes.
6899
- Constant *DV = NeedMask ? ResultLists[PHI][0 ].second : DefaultResults[PHI];
6898
+ // Use any value to fill the lookup table holes.
6899
+ Constant *DV = HolesHaveUndefinedResults ? ResultLists[PHI][0 ].second
6900
+ : DefaultResults[PHI];
6900
6901
StringRef FuncName = Fn->getName ();
6901
6902
SwitchLookupTable Table (Mod, TableSize, TableIndexOffset, ResultList, DV,
6902
6903
DL, FuncName);
0 commit comments