[llvm] [InlineCost] Consider the default branch when calculating cost (PR #77856)
Quentin Dian via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 14 06:18:07 PST 2024
https://github.com/DianQK updated https://github.com/llvm/llvm-project/pull/77856
>From 479121181b8800047e053aa815921cb309f0171d Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Wed, 10 Jan 2024 22:47:58 +0800
Subject: [PATCH 1/3] Pre-commit test cases
---
.../Inline/inline-switch-default-2.ll | 334 ++++++++++++++++++
.../Inline/inline-switch-default.ll | 250 +++++++++++++
2 files changed, 584 insertions(+)
create mode 100644 llvm/test/Transforms/Inline/inline-switch-default-2.ll
create mode 100644 llvm/test/Transforms/Inline/inline-switch-default.ll
diff --git a/llvm/test/Transforms/Inline/inline-switch-default-2.ll b/llvm/test/Transforms/Inline/inline-switch-default-2.ll
new file mode 100644
index 00000000000000..dd889124e3b5b1
--- /dev/null
+++ b/llvm/test/Transforms/Inline/inline-switch-default-2.ll
@@ -0,0 +1,334 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt %s -S -passes=inline -inline-threshold=21 | FileCheck %s
+
+; Check for scenarios without TTI.
+
+define i64 @foo1(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @foo1(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT:%.*]]
+; LOOKUPTABLE: branch_2.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: branch_4.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: branch_6.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: default_branch.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: bar1.exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @foo1(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT:%.*]]
+; SWITCH: branch_2.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: branch_4.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: branch_6.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: default_branch.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: bar1.exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+; CHECK-LABEL: define i64 @foo1(
+; CHECK-SAME: i64 [[TMP0:%.*]]) {
+; CHECK-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
+; CHECK-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; CHECK-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; CHECK-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; CHECK-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; CHECK-NEXT: ]
+; CHECK: branch_0.i:
+; CHECK-NEXT: br label [[BAR1_EXIT:%.*]]
+; CHECK: branch_2.i:
+; CHECK-NEXT: br label [[BAR1_EXIT]]
+; CHECK: branch_4.i:
+; CHECK-NEXT: br label [[BAR1_EXIT]]
+; CHECK: branch_6.i:
+; CHECK-NEXT: br label [[BAR1_EXIT]]
+; CHECK: default_branch.i:
+; CHECK-NEXT: br label [[BAR1_EXIT]]
+; CHECK: bar1.exit:
+; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; CHECK-NEXT: ret i64 [[TMP2]]
+;
+ %2 = call i64 @bar1(i64 %0)
+ ret i64 %2
+}
+
+define i64 @foo2(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @foo2(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT:%.*]]
+; LOOKUPTABLE: branch_2.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT]]
+; LOOKUPTABLE: branch_4.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT]]
+; LOOKUPTABLE: branch_6.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT]]
+; LOOKUPTABLE: unreachabledefault.i:
+; LOOKUPTABLE-NEXT: unreachable
+; LOOKUPTABLE: bar2.exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @foo2(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT:%.*]]
+; SWITCH: branch_2.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT]]
+; SWITCH: branch_4.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT]]
+; SWITCH: branch_6.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT]]
+; SWITCH: unreachabledefault.i:
+; SWITCH-NEXT: unreachable
+; SWITCH: bar2.exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+; CHECK-LABEL: define i64 @foo2(
+; CHECK-SAME: i64 [[TMP0:%.*]]) {
+; CHECK-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; CHECK-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; CHECK-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; CHECK-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; CHECK-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; CHECK-NEXT: ]
+; CHECK: branch_0.i:
+; CHECK-NEXT: br label [[BAR2_EXIT:%.*]]
+; CHECK: branch_2.i:
+; CHECK-NEXT: br label [[BAR2_EXIT]]
+; CHECK: branch_4.i:
+; CHECK-NEXT: br label [[BAR2_EXIT]]
+; CHECK: branch_6.i:
+; CHECK-NEXT: br label [[BAR2_EXIT]]
+; CHECK: unreachabledefault.i:
+; CHECK-NEXT: unreachable
+; CHECK: bar2.exit:
+; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; CHECK-NEXT: ret i64 [[TMP2]]
+;
+ %2 = call i64 @bar2(i64 %0)
+ ret i64 %2
+}
+
+define i64 @bar1(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @bar1(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0:
+; LOOKUPTABLE-NEXT: br label [[EXIT:%.*]]
+; LOOKUPTABLE: branch_2:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_4:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_6:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: default_branch:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @bar1(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0:
+; SWITCH-NEXT: br label [[EXIT:%.*]]
+; SWITCH: branch_2:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_4:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_6:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: default_branch:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+; CHECK-LABEL: define i64 @bar1(
+; CHECK-SAME: i64 [[TMP0:%.*]]) {
+; CHECK-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; CHECK-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; CHECK-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; CHECK-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; CHECK-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; CHECK-NEXT: ]
+; CHECK: branch_0:
+; CHECK-NEXT: br label [[EXIT:%.*]]
+; CHECK: branch_2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: branch_4:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: branch_6:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: default_branch:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; CHECK-NEXT: ret i64 [[TMP2]]
+;
+ switch i64 %0, label %default_branch [
+ i64 0, label %branch_0
+ i64 2, label %branch_2
+ i64 4, label %branch_4
+ i64 6, label %branch_6
+ ]
+
+branch_0:
+ br label %exit
+
+branch_2:
+ br label %exit
+
+branch_4:
+ br label %exit
+
+branch_6:
+ br label %exit
+
+default_branch:
+ br label %exit
+
+exit:
+ %2 = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ], [ 3, %default_branch ]
+ ret i64 %2
+}
+
+define i64 @bar2(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @bar2(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0:
+; LOOKUPTABLE-NEXT: br label [[EXIT:%.*]]
+; LOOKUPTABLE: branch_2:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_4:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_6:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: unreachabledefault:
+; LOOKUPTABLE-NEXT: unreachable
+; LOOKUPTABLE: exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @bar2(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0:
+; SWITCH-NEXT: br label [[EXIT:%.*]]
+; SWITCH: branch_2:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_4:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_6:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: unreachabledefault:
+; SWITCH-NEXT: unreachable
+; SWITCH: exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+; CHECK-LABEL: define i64 @bar2(
+; CHECK-SAME: i64 [[TMP0:%.*]]) {
+; CHECK-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; CHECK-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; CHECK-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; CHECK-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; CHECK-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; CHECK-NEXT: ]
+; CHECK: branch_0:
+; CHECK-NEXT: br label [[EXIT:%.*]]
+; CHECK: branch_2:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: branch_4:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: branch_6:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: unreachabledefault:
+; CHECK-NEXT: unreachable
+; CHECK: exit:
+; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; CHECK-NEXT: ret i64 [[TMP2]]
+;
+ switch i64 %0, label %unreachabledefault [
+ i64 0, label %branch_0
+ i64 2, label %branch_2
+ i64 4, label %branch_4
+ i64 6, label %branch_6
+ ]
+
+branch_0:
+ br label %exit
+
+branch_2:
+ br label %exit
+
+branch_4:
+ br label %exit
+
+branch_6:
+ br label %exit
+
+unreachabledefault:
+ unreachable
+
+exit:
+ %2 = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ]
+ ret i64 %2
+}
diff --git a/llvm/test/Transforms/Inline/inline-switch-default.ll b/llvm/test/Transforms/Inline/inline-switch-default.ll
new file mode 100644
index 00000000000000..da95b9f5a458ef
--- /dev/null
+++ b/llvm/test/Transforms/Inline/inline-switch-default.ll
@@ -0,0 +1,250 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt %s -S -passes=inline -inline-threshold=26 -min-jump-table-entries=4 | FileCheck %s -check-prefix=LOOKUPTABLE
+; RUN: opt %s -S -passes=inline -inline-threshold=21 -min-jump-table-entries=5 | FileCheck %s -check-prefix=SWITCH
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; FIXME: The `bar1` should not be inlined.
+
+define i64 @foo1(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @foo1(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT:%.*]]
+; LOOKUPTABLE: branch_2.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: branch_4.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: branch_6.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: default_branch.i:
+; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
+; LOOKUPTABLE: bar1.exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @foo1(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT:%.*]]
+; SWITCH: branch_2.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: branch_4.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: branch_6.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: default_branch.i:
+; SWITCH-NEXT: br label [[BAR1_EXIT]]
+; SWITCH: bar1.exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+ %2 = call i64 @bar1(i64 %0)
+ ret i64 %2
+}
+
+define i64 @foo2(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @foo2(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT:%.*]]
+; LOOKUPTABLE: branch_2.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT]]
+; LOOKUPTABLE: branch_4.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT]]
+; LOOKUPTABLE: branch_6.i:
+; LOOKUPTABLE-NEXT: br label [[BAR2_EXIT]]
+; LOOKUPTABLE: unreachabledefault.i:
+; LOOKUPTABLE-NEXT: unreachable
+; LOOKUPTABLE: bar2.exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @foo2(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT:%.*]]
+; SWITCH: branch_2.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT]]
+; SWITCH: branch_4.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT]]
+; SWITCH: branch_6.i:
+; SWITCH-NEXT: br label [[BAR2_EXIT]]
+; SWITCH: unreachabledefault.i:
+; SWITCH-NEXT: unreachable
+; SWITCH: bar2.exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+ %2 = call i64 @bar2(i64 %0)
+ ret i64 %2
+}
+
+define i64 @bar1(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @bar1(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0:
+; LOOKUPTABLE-NEXT: br label [[EXIT:%.*]]
+; LOOKUPTABLE: branch_2:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_4:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_6:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: default_branch:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @bar1(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0:
+; SWITCH-NEXT: br label [[EXIT:%.*]]
+; SWITCH: branch_2:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_4:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_6:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: default_branch:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+ switch i64 %0, label %default_branch [
+ i64 0, label %branch_0
+ i64 2, label %branch_2
+ i64 4, label %branch_4
+ i64 6, label %branch_6
+ ]
+
+branch_0:
+ br label %exit
+
+branch_2:
+ br label %exit
+
+branch_4:
+ br label %exit
+
+branch_6:
+ br label %exit
+
+default_branch:
+ br label %exit
+
+exit:
+ %2 = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ], [ 3, %default_branch ]
+ ret i64 %2
+}
+
+define i64 @bar2(i64 %0) {
+; LOOKUPTABLE-LABEL: define i64 @bar2(
+; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; LOOKUPTABLE-NEXT: ]
+; LOOKUPTABLE: branch_0:
+; LOOKUPTABLE-NEXT: br label [[EXIT:%.*]]
+; LOOKUPTABLE: branch_2:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_4:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: branch_6:
+; LOOKUPTABLE-NEXT: br label [[EXIT]]
+; LOOKUPTABLE: unreachabledefault:
+; LOOKUPTABLE-NEXT: unreachable
+; LOOKUPTABLE: exit:
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+;
+; SWITCH-LABEL: define i64 @bar2(
+; SWITCH-SAME: i64 [[TMP0:%.*]]) {
+; SWITCH-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; SWITCH-NEXT: i64 0, label [[BRANCH_0:%.*]]
+; SWITCH-NEXT: i64 2, label [[BRANCH_2:%.*]]
+; SWITCH-NEXT: i64 4, label [[BRANCH_4:%.*]]
+; SWITCH-NEXT: i64 6, label [[BRANCH_6:%.*]]
+; SWITCH-NEXT: ]
+; SWITCH: branch_0:
+; SWITCH-NEXT: br label [[EXIT:%.*]]
+; SWITCH: branch_2:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_4:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: branch_6:
+; SWITCH-NEXT: br label [[EXIT]]
+; SWITCH: unreachabledefault:
+; SWITCH-NEXT: unreachable
+; SWITCH: exit:
+; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; SWITCH-NEXT: ret i64 [[TMP2]]
+;
+ switch i64 %0, label %unreachabledefault [
+ i64 0, label %branch_0
+ i64 2, label %branch_2
+ i64 4, label %branch_4
+ i64 6, label %branch_6
+ ]
+
+branch_0:
+ br label %exit
+
+branch_2:
+ br label %exit
+
+branch_4:
+ br label %exit
+
+branch_6:
+ br label %exit
+
+unreachabledefault:
+ unreachable
+
+exit:
+ %2 = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ]
+ ret i64 %2
+}
>From fe59746686e2b635372cb8544f0f9e28f7262610 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Thu, 11 Jan 2024 19:05:03 +0800
Subject: [PATCH 2/3] [InlineCost] Consider the default branch when calculating
cost
---
.../llvm/Analysis/TargetTransformInfoImpl.h | 4 +-
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 12 +++---
llvm/lib/Analysis/InlineCost.cpp | 6 ++-
.../Inline/inline-switch-default-2.ll | 19 +--------
.../Inline/inline-switch-default.ll | 40 ++-----------------
5 files changed, 19 insertions(+), 62 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 60eab53fa2f601..1bf9cec8a2c40b 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -65,7 +65,9 @@ class TargetTransformInfoImplBase {
(void)PSI;
(void)BFI;
JTSize = 0;
- return SI.getNumCases();
+ bool HasDefault =
+ !isa<UnreachableInst>(SI.getDefaultDest()->getFirstNonPHIOrDbg());
+ return SI.getNumCases() + HasDefault;
}
unsigned getInliningThresholdMultiplier() const { return 1; }
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 5e7bdcdf72a49f..45849869be4a65 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -446,6 +446,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
/// inline cost heuristic, but it's a generic cost model to be used in other
/// places (e.g., in loop unrolling).
unsigned N = SI.getNumCases();
+ bool HasDefault =
+ !isa<UnreachableInst>(SI.getDefaultDest()->getFirstNonPHIOrDbg());
const TargetLoweringBase *TLI = getTLI();
const DataLayout &DL = this->getDataLayout();
@@ -454,7 +456,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
// Early exit if both a jump table and bit test are not allowed.
if (N < 1 || (!IsJTAllowed && DL.getIndexSizeInBits(0u) < N))
- return N;
+ return N + HasDefault;
APInt MaxCaseVal = SI.case_begin()->getCaseValue()->getValue();
APInt MinCaseVal = MaxCaseVal;
@@ -474,23 +476,23 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
if (TLI->isSuitableForBitTests(Dests.size(), N, MinCaseVal, MaxCaseVal,
DL))
- return 1;
+ return 1 + HasDefault;
}
// Check if suitable for a jump table.
if (IsJTAllowed) {
if (N < 2 || N < TLI->getMinimumJumpTableEntries())
- return N;
+ return N + HasDefault;
uint64_t Range =
(MaxCaseVal - MinCaseVal)
.getLimitedValue(std::numeric_limits<uint64_t>::max() - 1) + 1;
// Check whether a range of clusters is dense enough for a jump table
if (TLI->isSuitableForJumpTable(&SI, N, Range, PSI, BFI)) {
JumpTableSize = Range;
- return 1;
+ return 1 + HasDefault;
}
}
- return N;
+ return N + HasDefault;
}
bool shouldBuildLookupTables() {
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index 1fa7badaa4fa01..7b9e6e02f7b093 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -707,8 +707,9 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
if (JumpTableSize) {
int64_t JTCost =
static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;
-
addCost(JTCost);
+ if (NumCaseCluster > 1)
+ addCost((NumCaseCluster - 1) * 2 * InstrCost);
return;
}
@@ -1238,6 +1239,9 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
JTCostMultiplier * InstrCost;
increment(InlineCostFeatureIndex::jump_table_penalty, JTCost);
+ if (NumCaseCluster > 1)
+ increment(InlineCostFeatureIndex::case_cluster_penalty,
+ (NumCaseCluster - 1) * CaseClusterCostMultiplier * InstrCost);
return;
}
diff --git a/llvm/test/Transforms/Inline/inline-switch-default-2.ll b/llvm/test/Transforms/Inline/inline-switch-default-2.ll
index dd889124e3b5b1..a784bd8d8613ab 100644
--- a/llvm/test/Transforms/Inline/inline-switch-default-2.ll
+++ b/llvm/test/Transforms/Inline/inline-switch-default-2.ll
@@ -50,24 +50,7 @@ define i64 @foo1(i64 %0) {
;
; CHECK-LABEL: define i64 @foo1(
; CHECK-SAME: i64 [[TMP0:%.*]]) {
-; CHECK-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
-; CHECK-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
-; CHECK-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
-; CHECK-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
-; CHECK-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
-; CHECK-NEXT: ]
-; CHECK: branch_0.i:
-; CHECK-NEXT: br label [[BAR1_EXIT:%.*]]
-; CHECK: branch_2.i:
-; CHECK-NEXT: br label [[BAR1_EXIT]]
-; CHECK: branch_4.i:
-; CHECK-NEXT: br label [[BAR1_EXIT]]
-; CHECK: branch_6.i:
-; CHECK-NEXT: br label [[BAR1_EXIT]]
-; CHECK: default_branch.i:
-; CHECK-NEXT: br label [[BAR1_EXIT]]
-; CHECK: bar1.exit:
-; CHECK-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; CHECK-NEXT: [[TMP2:%.*]] = call i64 @bar1(i64 [[TMP0]])
; CHECK-NEXT: ret i64 [[TMP2]]
;
%2 = call i64 @bar1(i64 %0)
diff --git a/llvm/test/Transforms/Inline/inline-switch-default.ll b/llvm/test/Transforms/Inline/inline-switch-default.ll
index da95b9f5a458ef..6271d6c06dcc6f 100644
--- a/llvm/test/Transforms/Inline/inline-switch-default.ll
+++ b/llvm/test/Transforms/Inline/inline-switch-default.ll
@@ -5,51 +5,17 @@
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
-; FIXME: The `bar1` should not be inlined.
+; The `bar1` should not be inlined since there is a default branch.
define i64 @foo1(i64 %0) {
; LOOKUPTABLE-LABEL: define i64 @foo1(
; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
-; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
-; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
-; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
-; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
-; LOOKUPTABLE-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
-; LOOKUPTABLE-NEXT: ]
-; LOOKUPTABLE: branch_0.i:
-; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT:%.*]]
-; LOOKUPTABLE: branch_2.i:
-; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
-; LOOKUPTABLE: branch_4.i:
-; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
-; LOOKUPTABLE: branch_6.i:
-; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
-; LOOKUPTABLE: default_branch.i:
-; LOOKUPTABLE-NEXT: br label [[BAR1_EXIT]]
-; LOOKUPTABLE: bar1.exit:
-; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = call i64 @bar1(i64 [[TMP0]])
; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
;
; SWITCH-LABEL: define i64 @foo1(
; SWITCH-SAME: i64 [[TMP0:%.*]]) {
-; SWITCH-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
-; SWITCH-NEXT: i64 0, label [[BRANCH_0_I:%.*]]
-; SWITCH-NEXT: i64 2, label [[BRANCH_2_I:%.*]]
-; SWITCH-NEXT: i64 4, label [[BRANCH_4_I:%.*]]
-; SWITCH-NEXT: i64 6, label [[BRANCH_6_I:%.*]]
-; SWITCH-NEXT: ]
-; SWITCH: branch_0.i:
-; SWITCH-NEXT: br label [[BAR1_EXIT:%.*]]
-; SWITCH: branch_2.i:
-; SWITCH-NEXT: br label [[BAR1_EXIT]]
-; SWITCH: branch_4.i:
-; SWITCH-NEXT: br label [[BAR1_EXIT]]
-; SWITCH: branch_6.i:
-; SWITCH-NEXT: br label [[BAR1_EXIT]]
-; SWITCH: default_branch.i:
-; SWITCH-NEXT: br label [[BAR1_EXIT]]
-; SWITCH: bar1.exit:
-; SWITCH-NEXT: [[TMP2:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ], [ 3, [[DEFAULT_BRANCH_I]] ]
+; SWITCH-NEXT: [[TMP2:%.*]] = call i64 @bar1(i64 [[TMP0]])
; SWITCH-NEXT: ret i64 [[TMP2]]
;
%2 = call i64 @bar1(i64 %0)
>From 2446d3c913d72af0cb734fb5552538209032c7d5 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Sun, 14 Jan 2024 22:17:03 +0800
Subject: [PATCH 3/3] fixup! [InlineCost] Consider the default branch when
calculating cost
Add `defaultDestIsUnreachable`
---
llvm/include/llvm/Analysis/TargetTransformInfoImpl.h | 3 +--
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 3 +--
llvm/include/llvm/IR/Instructions.h | 5 +++++
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 1bf9cec8a2c40b..03baf2231e943c 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -65,8 +65,7 @@ class TargetTransformInfoImplBase {
(void)PSI;
(void)BFI;
JTSize = 0;
- bool HasDefault =
- !isa<UnreachableInst>(SI.getDefaultDest()->getFirstNonPHIOrDbg());
+ bool HasDefault = !SI.defaultDestIsUnreachable();
return SI.getNumCases() + HasDefault;
}
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 45849869be4a65..f019af9d4618a0 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -446,8 +446,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
/// inline cost heuristic, but it's a generic cost model to be used in other
/// places (e.g., in loop unrolling).
unsigned N = SI.getNumCases();
- bool HasDefault =
- !isa<UnreachableInst>(SI.getDefaultDest()->getFirstNonPHIOrDbg());
+ bool HasDefault = !SI.defaultDestIsUnreachable();
const TargetLoweringBase *TLI = getTLI();
const DataLayout &DL = this->getDataLayout();
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index 4b5a4423b76899..e0d0397f6d9848 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -49,6 +49,7 @@ class DataLayout;
class StringRef;
class Type;
class Value;
+class UnreachableInst;
//===----------------------------------------------------------------------===//
// AllocaInst Class
@@ -3505,6 +3506,10 @@ class SwitchInst : public Instruction {
return cast<BasicBlock>(getOperand(1));
}
+ bool defaultDestIsUnreachable() const {
+ return isa<UnreachableInst>(getDefaultDest()->getFirstNonPHIOrDbg());
+ }
+
void setDefaultDest(BasicBlock *DefaultCase) {
setOperand(1, reinterpret_cast<Value*>(DefaultCase));
}
More information about the llvm-commits
mailing list