[llvm] 3800b15 - [SimplifyCFG] Share code to compute switch density between ShouldBuildLookupTable() and ReduceSwitchRange()
Hans Wennborg via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 10 06:30:08 PDT 2022
Author: Hans Wennborg
Date: 2022-06-10T15:29:36+02:00
New Revision: 3800b157d77b80fba204afff8cc8d545198899d5
URL: https://github.com/llvm/llvm-project/commit/3800b157d77b80fba204afff8cc8d545198899d5
DIFF: https://github.com/llvm/llvm-project/commit/3800b157d77b80fba204afff8cc8d545198899d5.diff
LOG: [SimplifyCFG] Share code to compute switch density between ShouldBuildLookupTable() and ReduceSwitchRange()
They're computing the same thing. No functionality change.
Differential revision: https://reviews.llvm.org/D127482
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index c0f3707b7874..067d432cd19b 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6107,6 +6107,27 @@ static bool isTypeLegalForLookupTable(Type *Ty, const TargetTransformInfo &TTI,
DL.fitsInLegalInteger(IT->getBitWidth());
}
+static bool isSwitchDense(uint64_t NumCases, uint64_t CaseRange) {
+ // 40% is the default density for building a jump table in optsize/minsize
+ // mode. See also TargetLoweringBase::isSuitableForJumpTable(), which this
+ // function was based on.
+ const uint64_t MinDensity = 40;
+
+ if (CaseRange >= UINT64_MAX / 100)
+ return false; // Avoid multiplication overflows below.
+
+ return NumCases * 100 >= CaseRange * MinDensity;
+}
+
+static bool isSwitchDense(ArrayRef<int64_t> Values) {
+ uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
+ uint64_t Range = Diff + 1;
+ if (Range < Diff)
+ return false; // Overflow.
+
+ return isSwitchDense(Values.size(), Range);
+}
+
/// Determine whether a lookup table should be built for this switch, based on
/// the number of cases, size of the table, and the types of the results.
// TODO: We could support larger than legal types by limiting based on the
@@ -6116,8 +6137,8 @@ static bool
ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
const TargetTransformInfo &TTI, const DataLayout &DL,
const SmallDenseMap<PHINode *, Type *> &ResultTypes) {
- if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
- return false; // TableSize overflowed, or mul below might overflow.
+ if (SI->getNumCases() > TableSize)
+ return false; // TableSize overflowed.
bool AllTablesFitInRegister = true;
bool HasIllegalType = false;
@@ -6147,10 +6168,7 @@ ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
if (HasIllegalType)
return false;
- // The table density should be at least 40%. This is the same criterion as for
- // jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
- // FIXME: Find the best cut-off.
- return SI->getNumCases() * 10 >= TableSize * 4;
+ return isSwitchDense(SI->getNumCases(), TableSize);
}
/// Try to reuse the switch table index compare. Following pattern:
@@ -6486,17 +6504,6 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
return true;
}
-static bool isSwitchDense(ArrayRef<int64_t> Values) {
- // See also SelectionDAGBuilder::isDense(), which this function was based on.
- uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
- uint64_t Range = Diff + 1;
- uint64_t NumCases = Values.size();
- // 40% is the default density for building a jump table in optsize/minsize mode.
- uint64_t MinDensity = 40;
-
- return NumCases * 100 >= Range * MinDensity;
-}
-
/// Try to transform a switch that has "holes" in it to a contiguous sequence
/// of cases.
///
More information about the llvm-commits
mailing list