Skip to content

Increase number of icmps needed for converting to a switch statement #99269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

hiraditya
Copy link
Collaborator

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

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: llvm#48188
@llvmbot
Copy link
Member

llvmbot commented Jul 17, 2024

@llvm/pr-subscribers-llvm-transforms

Author: AdityaK (hiraditya)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/99269.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+6-2)
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()));

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting icmp sequences to switch is part of the canonical form, and many other optimizations depend on it.

@hiraditya
Copy link
Collaborator Author

As vectorization gets blocked in the cases noted in #48188, how should we go about addressing that issue?

@hiraditya
Copy link
Collaborator Author

#99808 is a better approach.

@hiraditya hiraditya closed this Jul 26, 2024
@hiraditya hiraditya deleted the icmp branch December 4, 2024 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missed loop replace_if vectorization due to the presence of switch
3 participants