[llvm] Increase number of icmps needed for converting to a switch statement (PR #99269)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 21:54:46 PDT 2024


https://github.com/hiraditya created https://github.com/llvm/llvm-project/pull/99269

In RISC-V the minimum number of case needed for code-generating a jump table is 5, while on AArch64 it is 13. Ideally we should query the backend for their preferable jump table size before deciding to convert a sequence of compares to a switch statement.

Also make this a parameter for anyone to tune.

Fixes: #48188

>From 2351f5cf942ba822dd48f1d1a574d0b188c6ee8e Mon Sep 17 00:00:00 2001
From: AdityaK <hiraditya at msn.com>
Date: Tue, 16 Jul 2024 21:51:36 -0700
Subject: [PATCH] Increase number of icmps needed for converting to a switch
 statement

In RISC-V the minimum number of case needed for code-generating
a jump table is 5, while on AArch64 it is 13. Ideally we should
query the backend for their preferable jump table size before
deciding to convert a sequence of compares to a switch statement.

Also make this a parameter for anyone to tune.

Fixes: #48188
---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 8f717cb43bcb4..e85fd5de3d4f7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -180,6 +180,10 @@ static cl::opt<unsigned> MaxSwitchCasesPerResult(
     "max-switch-cases-per-result", cl::Hidden, cl::init(16),
     cl::desc("Limit cases to analyze when converting a switch to select"));
 
+static cl::opt<unsigned> MinICmpsToSwitch(
+    "min-icmps-to-switch", cl::Hidden, cl::init(9),
+    cl::desc("Minimum number of icmp sequences to convert to switch"));
+
 STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
 STATISTIC(NumLinearMaps,
           "Number of switch instructions turned into linear mapping");
@@ -4893,8 +4897,8 @@ bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst *BI,
   if (!CompVal)
     return false;
 
-  // Avoid turning single icmps into a switch.
-  if (UsedICmps <= 1)
+  // Avoid turning few icmps into a switch.
+  if (UsedICmps < MinICmpsToSwitch)
     return false;
 
   bool TrueWhenEqual = match(Cond, m_LogicalOr(m_Value(), m_Value()));



More information about the llvm-commits mailing list