Skip to content

Commit baaaec1

Browse files
committed
Consider a cross signed max-min table
1 parent 135b2d4 commit baaaec1

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6640,7 +6640,7 @@ static void reuseTableCompare(
66406640
static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
66416641
DomTreeUpdater *DTU, const DataLayout &DL,
66426642
const TargetTransformInfo &TTI,
6643-
bool TryMinTableSize) {
6643+
bool ConsiderCrossSignedMaxMinTable) {
66446644
assert(SI->getNumCases() > 1 && "Degenerate switch?");
66456645

66466646
BasicBlock *BB = SI->getParent();
@@ -6686,13 +6686,12 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
66866686
ConstantInt *BeginCaseVal = *CaseValIter;
66876687
ConstantInt *EndCaseVal = CaseVals.back();
66886688
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) {
66966695
// We consider cases where the starting to the endpoint will cross the
66976696
// signed max and min. For example, for the i8 range `[-128, -127, 126,
66986697
// 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,
67046703
const auto &NextVal = NextCaseVal->getValue();
67056704
const auto &CurVal = CurCaseVal->getValue();
67066705
uint64_t RequireTableSize = (CurVal - NextVal).getLimitedValue() + 1;
6707-
if (RequireTableSize < MinTableSize) {
6706+
if (RequireTableSize < LookupTableSize) {
67086707
BeginCaseVal = NextCaseVal;
67096708
EndCaseVal = CurCaseVal;
6710-
MinTableSize = RequireTableSize;
6709+
LookupTableSize = RequireTableSize;
67116710
}
67126711
}
67136712
}
@@ -6758,7 +6757,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
67586757
if (UseSwitchConditionAsTableIndex)
67596758
TableSize = EndCaseVal->getLimitedValue() + 1;
67606759
else
6761-
TableSize = MinTableSize;
6760+
TableSize = LookupTableSize;
67626761

67636762
// If the default destination is unreachable, or if the lookup table covers
67646763
// all values of the conditional variable, branch directly to the lookup table
@@ -6789,8 +6788,8 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
67896788

67906789
if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
67916790
// 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 &&
67946793
SwitchToLookupTable(SI, Builder, DTU, DL, TTI, true);
67956794

67966795
std::vector<DominatorTree::UpdateType> Updates;

llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ return:
122122

123123
}
124124

125-
; The minimal table range is [122, -128]([122, 128]).
125+
; The cross signed max-min table range is [122, -128]([122, 128]).
126126

127127
define i32 @f_i8_128(i8 %c) {
128128
; CHECK-LABEL: @f_i8_128(
@@ -161,7 +161,7 @@ return:
161161
ret i32 %retval.0
162162
}
163163

164-
; The minimal table range is [3, 0].
164+
; The cross signed max-min table range is [3, 0].
165165

166166
define i32 @f_min_max(i3 %c) {
167167
; CHECK-LABEL: @f_min_max(
@@ -193,7 +193,7 @@ return:
193193
ret i32 %retval.0
194194
}
195195

196-
; The minimal table range is [-1, -4].
196+
; The cross signed max-min table range is [-1, -4].
197197

198198
define i32 @f_min_max_2(i3 %c) {
199199
; CHECK-LABEL: @f_min_max_2(

0 commit comments

Comments
 (0)