aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/InlineCost.cpp
diff options
context:
space:
mode:
authorQuentin Dian <dianqk@dianqk.net>2024-02-11 18:24:59 +0800
committerGitHub <noreply@github.com>2024-02-11 18:24:59 +0800
commit5932fcc47855fdd209784f38820422d2369b84b2 (patch)
tree4c3818734563aae919974d11eb98c0ca99aff233 /llvm/lib/Analysis/InlineCost.cpp
parent5aec9392674572fa5a06283173a6a739742d261d (diff)
downloadllvm-5932fcc47855fdd209784f38820422d2369b84b2.zip
llvm-5932fcc47855fdd209784f38820422d2369b84b2.tar.gz
llvm-5932fcc47855fdd209784f38820422d2369b84b2.tar.bz2
[InlineCost] Consider the default branch when calculating cost (#77856)
First step in fixing #76772. This PR considers the default branch as a case branch. This will give the unreachable default branch fair consideration.
Diffstat (limited to 'llvm/lib/Analysis/InlineCost.cpp')
-rw-r--r--llvm/lib/Analysis/InlineCost.cpp21
1 files changed, 13 insertions, 8 deletions
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index 5b780b5..e55eaa5 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -336,8 +336,8 @@ protected:
/// Called at the end of processing a switch instruction, with the given
/// number of case clusters.
- virtual void onFinalizeSwitch(unsigned JumpTableSize,
- unsigned NumCaseCluster) {}
+ virtual void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
+ bool DefaultDestUndefined) {}
/// Called to account for any other instruction not specifically accounted
/// for.
@@ -699,15 +699,16 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
CallPenalty));
}
- void onFinalizeSwitch(unsigned JumpTableSize,
- unsigned NumCaseCluster) override {
+ 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) {
int64_t JTCost =
static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;
-
addCost(JTCost);
return;
}
@@ -1153,6 +1154,7 @@ private:
// heuristics in the ML inliner.
static constexpr int JTCostMultiplier = 4;
static constexpr int CaseClusterCostMultiplier = 2;
+ static constexpr int SwitchDefaultDestCostMultiplier = 2;
static constexpr int SwitchCostMultiplier = 2;
// FIXME: These are taken from the heuristic-based cost visitor: we should
@@ -1231,8 +1233,11 @@ private:
}
}
- void onFinalizeSwitch(unsigned JumpTableSize,
- unsigned NumCaseCluster) override {
+ void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
+ bool DefaultDestUndefined) override {
+ if (!DefaultDestUndefined)
+ increment(InlineCostFeatureIndex::switch_default_dest_penalty,
+ SwitchDefaultDestCostMultiplier * InstrCost);
if (JumpTableSize) {
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
@@ -2461,7 +2466,7 @@ bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
unsigned NumCaseCluster =
TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);
- onFinalizeSwitch(JumpTableSize, NumCaseCluster);
+ onFinalizeSwitch(JumpTableSize, NumCaseCluster, SI.defaultDestUndefined());
return false;
}