[llvm] [InlineCost] Correct the default branch cost for the switch statement (PR #85160)
Quentin Dian via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 22 22:07:01 PDT 2024
================
@@ -536,7 +536,13 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
// Considering comparisons from leaf and non-leaf nodes, we can estimate the
// number of comparisons in a simple closed form :
// n + n / 2 - 1 = n * 3 / 2 - 1
-int64_t getExpectedNumberOfCompare(int NumCaseCluster) {
+int64_t getExpectedNumberOfCompare(int NumCaseCluster,
+ bool DefaultDestUndefined) {
+ // The compare instruction count should be less than the branch count
+ // when default branch is undefined.
+ if (DefaultDestUndefined) {
+ return static_cast<int64_t>(NumCaseCluster) - 1;
+ }
----------------
DianQK wrote:
Here is an example: https://llvm.godbolt.org/z/x6ETdfY79. If there are common target branches, the number of compare instructions will decrease. I haven't started learning about instruction selection, so I will leave a note here.
https://github.com/llvm/llvm-project/pull/85160
More information about the llvm-commits
mailing list