[llvm] [SimplifyCFG] Relax `cttz` cost check in `simplifySwitchOfPowersOfTwo` (PR #145159)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 22 23:08:43 PDT 2025


================
@@ -7211,17 +7213,15 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
       !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.
+  // Ensure trailing zeroes count intrinsic emission is not too expensive.
+  IntrinsicCostAttributes Attrs(Intrinsic::cttz, CondTy,
+                                {Condition, ConstantInt::getTrue(Context)});
+  if (TTI.getIntrinsicInstrCost(Attrs, TTI::TCK_SizeAndLatency) >
+      TTI::TCC_Basic * 2)
----------------
dtcxzyw wrote:

> Is there not a cost for the alternative codegen to compare against?

Without this transform, the switch will be lowered into an icmp chain/indirect jump table. Thus, the transform is always profitable if `ctlz` is natively supported. The default cost for native `ctlz` is `TCC_Basic`. Can we increase the threshold to `< TCC_Expensive` as it is also used in other places?


https://github.com/llvm/llvm-project/pull/145159


More information about the llvm-commits mailing list