[llvm] [SimplifyCFG] Add optimization for switches of powers of two (PR #70977)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 13 05:29:25 PST 2023
================
@@ -6839,6 +6837,89 @@ static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
return true;
}
+static bool isSwitchOfPowersOfTwo(ArrayRef<int64_t> Values) {
+ for (auto &Value : Values) {
+ if (!llvm::isPowerOf2_64(Value))
+ return false;
+ }
+
+ return true;
+}
+
+static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
+ const DataLayout &DL,
+ const TargetTransformInfo &TTI) {
+
+ auto *Condition = SI->getCondition();
+ auto &Context = SI->getContext();
+ auto *CondTy = cast<IntegerType>(Condition->getType());
+
+ if (CondTy->getIntegerBitWidth() > 64 ||
+ !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
+ return false;
+
+ const auto CttzIntrinsicCost = TTI.getIntrinsicInstrCost(
+ IntrinsicCostAttributes(Intrinsic::cttz, CondTy,
+ {Condition, ConstantInt::getTrue(Context)}),
+ TTI::TCK_SizeAndLatency);
+
+ if (CttzIntrinsicCost > TTI::TCC_Basic) {
+ // Inserting intrinsic is too expensive
+ return false;
+ }
+
+ // Only bother with this optimization if there are more than 3 switch cases;
+ // SDAG will only bother creating jump tables for 4 or more cases.
+ if (SI->getNumCases() < 4)
+ return false;
+
+ SmallVector<int64_t, 4> Values;
+ for (const auto &Case : SI->cases())
+ Values.push_back(Case.getCaseValue()->getValue().getSExtValue());
+
+ // isSwichDence requires case values to be sorted
+ llvm::sort(Values);
+ if (isSwitchDense(Values))
+ // Switch is already dense
+ return false;
+
+ // We perform this optimization only for switches with
+ // unreachable default case.
+ // This assumtion will save us from checking if `Condition` is a power of two
+ bool HasDefault =
+ !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
+ if (HasDefault || !isSwitchOfPowersOfTwo(Values))
+ return false;
+
+ SmallVector<int64_t, 4> TransformedValues;
+ for (auto &Case : SI->cases())
+ TransformedValues.push_back(
+ static_cast<int64_t>(Case.getCaseValue()->getValue().countr_zero()));
+
+ // isSwichDence requires case values to be sorted
+ llvm::sort(TransformedValues);
+ if (!isSwitchDense(TransformedValues))
----------------
dtcxzyw wrote:
```suggestion
if (!isSwitchDense(Values.size(), llvm::countr_zero(Values.back()) - llvm::counts_zero(Values.front())+ 1))
```
https://github.com/llvm/llvm-project/pull/70977
More information about the llvm-commits
mailing list