diff options
author | Quentin Dian <dianqk@dianqk.net> | 2024-05-05 21:28:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-05 21:28:31 +0800 |
commit | 882814edd33cab853859f07b1dd4c4fa1393e0ea (patch) | |
tree | a6fbc487fcd68d313a1f433d62dbb58d9f8d3b8c /llvm/lib/Analysis/InlineCost.cpp | |
parent | de9b386f84b58ad0ffc12e221bc6d9161ca5b62d (diff) | |
download | llvm-882814edd33cab853859f07b1dd4c4fa1393e0ea.zip llvm-882814edd33cab853859f07b1dd4c4fa1393e0ea.tar.gz llvm-882814edd33cab853859f07b1dd4c4fa1393e0ea.tar.bz2 |
[InlineCost] Correct the default branch cost for the switch statement (#85160)
Fixes #81723.
The earliest commit of the related code is:
https://github.com/llvm/llvm-project/commit/919f9e8d65ada6552b8b8a5ec12ea49db91c922a.
I tried to understand the following code with
https://github.com/llvm/llvm-project/pull/77856#issuecomment-1993499085.
https://github.com/llvm/llvm-project/blob/5932fcc47855fdd209784f38820422d2369b84b2/llvm/lib/Analysis/InlineCost.cpp#L709-L720
I think only scenarios where there is a default branch were considered.
Diffstat (limited to 'llvm/lib/Analysis/InlineCost.cpp')
-rw-r--r-- | llvm/lib/Analysis/InlineCost.cpp | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp index c75460f..e4989db 100644 --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -701,21 +701,26 @@ class InlineCostCallAnalyzer final : public CallAnalyzer { void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster, bool DefaultDestUndefined) override { - if (!DefaultDestUndefined) - addCost(2 * InstrCost); // If suitable for a jump table, consider the cost for the table size and // branch to destination. // Maximum valid cost increased in this function. if (JumpTableSize) { + // Suppose a default branch includes one compare and one conditional + // branch if it's reachable. + if (!DefaultDestUndefined) + addCost(2 * InstrCost); + // Suppose a jump table requires one load and one jump instruction. int64_t JTCost = - static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost; + static_cast<int64_t>(JumpTableSize) * InstrCost + 2 * InstrCost; addCost(JTCost); return; } if (NumCaseCluster <= 3) { // Suppose a comparison includes one compare and one conditional branch. - addCost(NumCaseCluster * 2 * InstrCost); + // We can reduce a set of instructions if the default branch is + // undefined. + addCost((NumCaseCluster - DefaultDestUndefined) * 2 * InstrCost); return; } @@ -1152,7 +1157,7 @@ private: // FIXME: These constants are taken from the heuristic-based cost visitor. // These should be removed entirely in a later revision to avoid reliance on // heuristics in the ML inliner. - static constexpr int JTCostMultiplier = 4; + static constexpr int JTCostMultiplier = 2; static constexpr int CaseClusterCostMultiplier = 2; static constexpr int SwitchDefaultDestCostMultiplier = 2; static constexpr int SwitchCostMultiplier = 2; @@ -1235,11 +1240,10 @@ private: void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster, bool DefaultDestUndefined) override { - if (!DefaultDestUndefined) - increment(InlineCostFeatureIndex::switch_default_dest_penalty, - SwitchDefaultDestCostMultiplier * InstrCost); - if (JumpTableSize) { + if (!DefaultDestUndefined) + increment(InlineCostFeatureIndex::switch_default_dest_penalty, + SwitchDefaultDestCostMultiplier * InstrCost); int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost + JTCostMultiplier * InstrCost; increment(InlineCostFeatureIndex::jump_table_penalty, JTCost); @@ -1248,7 +1252,8 @@ private: if (NumCaseCluster <= 3) { increment(InlineCostFeatureIndex::case_cluster_penalty, - NumCaseCluster * CaseClusterCostMultiplier * InstrCost); + (NumCaseCluster - DefaultDestUndefined) * + CaseClusterCostMultiplier * InstrCost); return; } |