@@ -6640,7 +6640,7 @@ static void reuseTableCompare(
6640
6640
static bool SwitchToLookupTable (SwitchInst *SI, IRBuilder<> &Builder,
6641
6641
DomTreeUpdater *DTU, const DataLayout &DL,
6642
6642
const TargetTransformInfo &TTI,
6643
- bool TryMinTableSize ) {
6643
+ bool ConsiderCrossSignedMaxMinTable ) {
6644
6644
assert (SI->getNumCases () > 1 && " Degenerate switch?" );
6645
6645
6646
6646
BasicBlock *BB = SI->getParent ();
@@ -6686,13 +6686,12 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6686
6686
ConstantInt *BeginCaseVal = *CaseValIter;
6687
6687
ConstantInt *EndCaseVal = CaseVals.back ();
6688
6688
bool RangeOverflow = false ;
6689
- uint64_t MinTableSize = EndCaseVal->getValue ()
6690
- .ssub_ov (BeginCaseVal->getValue (), RangeOverflow)
6691
- .getLimitedValue () +
6692
- 1 ;
6693
- // If there is no overflow, then this must be the minimal table.
6694
- // The signed max-min can no longer build a lookup table, so return.
6695
- if (RangeOverflow && TryMinTableSize) {
6689
+ uint64_t LookupTableSize =
6690
+ EndCaseVal->getValue ()
6691
+ .ssub_ov (BeginCaseVal->getValue (), RangeOverflow)
6692
+ .getLimitedValue () +
6693
+ 1 ;
6694
+ if (RangeOverflow && ConsiderCrossSignedMaxMinTable) {
6696
6695
// We consider cases where the starting to the endpoint will cross the
6697
6696
// signed max and min. For example, for the i8 range `[-128, -127, 126,
6698
6697
// 127]`, we choose from 126 to -127. The length of the lookup table is 4.
@@ -6704,10 +6703,10 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6704
6703
const auto &NextVal = NextCaseVal->getValue ();
6705
6704
const auto &CurVal = CurCaseVal->getValue ();
6706
6705
uint64_t RequireTableSize = (CurVal - NextVal).getLimitedValue () + 1 ;
6707
- if (RequireTableSize < MinTableSize ) {
6706
+ if (RequireTableSize < LookupTableSize ) {
6708
6707
BeginCaseVal = NextCaseVal;
6709
6708
EndCaseVal = CurCaseVal;
6710
- MinTableSize = RequireTableSize;
6709
+ LookupTableSize = RequireTableSize;
6711
6710
}
6712
6711
}
6713
6712
}
@@ -6758,7 +6757,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6758
6757
if (UseSwitchConditionAsTableIndex)
6759
6758
TableSize = EndCaseVal->getLimitedValue () + 1 ;
6760
6759
else
6761
- TableSize = MinTableSize ;
6760
+ TableSize = LookupTableSize ;
6762
6761
6763
6762
// If the default destination is unreachable, or if the lookup table covers
6764
6763
// all values of the conditional variable, branch directly to the lookup table
@@ -6789,8 +6788,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6789
6788
6790
6789
if (!ShouldBuildLookupTable (SI, TableSize, TTI, DL, ResultTypes))
6791
6790
// When a signed max-min cannot construct a lookup table, try to find a
6792
- // range with a minimal lookup table.
6793
- return !TryMinTableSize &&
6791
+ // range with a smaller lookup table.
6792
+ return RangeOverflow && !ConsiderCrossSignedMaxMinTable &&
6794
6793
SwitchToLookupTable (SI, Builder, DTU, DL, TTI, true );
6795
6794
6796
6795
std::vector<DominatorTree::UpdateType> Updates;
0 commit comments