[llvm] [InlineCost] Consider the default branch when calculating cost (PR #77856)
Quentin Dian via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 6 16:40:56 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/6] 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 0000000000000..dd889124e3b5b
--- /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 0000000000000..da95b9f5a458e
--- /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/6] [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 60eab53fa2f60..1bf9cec8a2c40 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 5e7bdcdf72a49..45849869be4a6 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 1fa7badaa4fa0..7b9e6e02f7b09 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 dd889124e3b5b..a784bd8d8613a 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 da95b9f5a458e..6271d6c06dcc6 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/6] 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 1bf9cec8a2c40..03baf2231e943 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 45849869be4a6..f019af9d4618a 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 4b5a4423b7689..e0d0397f6d984 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));
}
>From 7ce42c05907812047d3488a34bbe397c7c7759c6 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Wed, 17 Jan 2024 21:00:09 +0800
Subject: [PATCH 4/6] fixup! [InlineCost] Consider the default branch when
calculating cost
---
llvm/include/llvm/Analysis/TargetTransformInfoImpl.h | 2 +-
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 2 +-
llvm/include/llvm/IR/Instructions.h | 4 +++-
3 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 03baf2231e943..0349c144d9723 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -65,7 +65,7 @@ class TargetTransformInfoImplBase {
(void)PSI;
(void)BFI;
JTSize = 0;
- bool HasDefault = !SI.defaultDestIsUnreachable();
+ bool HasDefault = !SI.defaultDestUndefined();
return SI.getNumCases() + HasDefault;
}
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index f019af9d4618a..8152efdf60902 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -446,7 +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 = !SI.defaultDestIsUnreachable();
+ bool HasDefault = !SI.defaultDestUndefined();
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 e0d0397f6d984..1db4ff2f09bca 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -3506,7 +3506,9 @@ class SwitchInst : public Instruction {
return cast<BasicBlock>(getOperand(1));
}
- bool defaultDestIsUnreachable() const {
+ /// Returns true if the default branch must result in immediate undefined
+ /// behavior, false otherwise.
+ bool defaultDestUndefined() const {
return isa<UnreachableInst>(getDefaultDest()->getFirstNonPHIOrDbg());
}
>From 405a58f5609c14080e697f90e8ccaec7545c9e37 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Wed, 7 Feb 2024 08:06:08 +0800
Subject: [PATCH 5/6] fixup! Pre-commit test cases
---
.../Inline/inline-switch-default-2.ll | 58 ++++++------
.../Inline/inline-switch-default.ll | 88 +++++++++----------
2 files changed, 73 insertions(+), 73 deletions(-)
diff --git a/llvm/test/Transforms/Inline/inline-switch-default-2.ll b/llvm/test/Transforms/Inline/inline-switch-default-2.ll
index a784bd8d8613a..8d3e24c798df8 100644
--- a/llvm/test/Transforms/Inline/inline-switch-default-2.ll
+++ b/llvm/test/Transforms/Inline/inline-switch-default-2.ll
@@ -3,7 +3,7 @@
; Check for scenarios without TTI.
-define i64 @foo1(i64 %0) {
+define i64 @foo1(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @foo1(
; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH_I:%.*]] [
@@ -49,15 +49,15 @@ define i64 @foo1(i64 %0) {
; SWITCH-NEXT: ret i64 [[TMP2]]
;
; CHECK-LABEL: define i64 @foo1(
-; CHECK-SAME: i64 [[TMP0:%.*]]) {
-; CHECK-NEXT: [[TMP2:%.*]] = call i64 @bar1(i64 [[TMP0]])
-; CHECK-NEXT: ret i64 [[TMP2]]
+; CHECK-SAME: i64 [[A:%.*]]) {
+; CHECK-NEXT: [[B:%.*]] = call i64 @bar1(i64 [[A]])
+; CHECK-NEXT: ret i64 [[B]]
;
- %2 = call i64 @bar1(i64 %0)
- ret i64 %2
+ %b = call i64 @bar1(i64 %a)
+ ret i64 %b
}
-define i64 @foo2(i64 %0) {
+define i64 @foo2(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @foo2(
; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
@@ -103,8 +103,8 @@ define i64 @foo2(i64 %0) {
; SWITCH-NEXT: ret i64 [[TMP2]]
;
; CHECK-LABEL: define i64 @foo2(
-; CHECK-SAME: i64 [[TMP0:%.*]]) {
-; CHECK-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; CHECK-SAME: i64 [[A:%.*]]) {
+; CHECK-NEXT: switch i64 [[A]], 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:%.*]]
@@ -121,14 +121,14 @@ define i64 @foo2(i64 %0) {
; 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]]
+; CHECK-NEXT: [[B_I:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; CHECK-NEXT: ret i64 [[B_I]]
;
- %2 = call i64 @bar2(i64 %0)
- ret i64 %2
+ %b = call i64 @bar2(i64 %a)
+ ret i64 %b
}
-define i64 @bar1(i64 %0) {
+define i64 @bar1(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @bar1(
; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
@@ -174,8 +174,8 @@ define i64 @bar1(i64 %0) {
; SWITCH-NEXT: ret i64 [[TMP2]]
;
; CHECK-LABEL: define i64 @bar1(
-; CHECK-SAME: i64 [[TMP0:%.*]]) {
-; CHECK-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; CHECK-SAME: i64 [[A:%.*]]) {
+; CHECK-NEXT: switch i64 [[A]], label [[DEFAULT_BRANCH:%.*]] [
; CHECK-NEXT: i64 0, label [[BRANCH_0:%.*]]
; CHECK-NEXT: i64 2, label [[BRANCH_2:%.*]]
; CHECK-NEXT: i64 4, label [[BRANCH_4:%.*]]
@@ -192,10 +192,10 @@ define i64 @bar1(i64 %0) {
; 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]]
+; CHECK-NEXT: [[B:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; CHECK-NEXT: ret i64 [[B]]
;
- switch i64 %0, label %default_branch [
+ switch i64 %a, label %default_branch [
i64 0, label %branch_0
i64 2, label %branch_2
i64 4, label %branch_4
@@ -218,11 +218,11 @@ 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
+ %b = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ], [ 3, %default_branch ]
+ ret i64 %b
}
-define i64 @bar2(i64 %0) {
+define i64 @bar2(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @bar2(
; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
@@ -268,8 +268,8 @@ define i64 @bar2(i64 %0) {
; SWITCH-NEXT: ret i64 [[TMP2]]
;
; CHECK-LABEL: define i64 @bar2(
-; CHECK-SAME: i64 [[TMP0:%.*]]) {
-; CHECK-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; CHECK-SAME: i64 [[A:%.*]]) {
+; CHECK-NEXT: switch i64 [[A]], label [[UNREACHABLEDEFAULT:%.*]] [
; CHECK-NEXT: i64 0, label [[BRANCH_0:%.*]]
; CHECK-NEXT: i64 2, label [[BRANCH_2:%.*]]
; CHECK-NEXT: i64 4, label [[BRANCH_4:%.*]]
@@ -286,10 +286,10 @@ define i64 @bar2(i64 %0) {
; 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]]
+; CHECK-NEXT: [[B:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; CHECK-NEXT: ret i64 [[B]]
;
- switch i64 %0, label %unreachabledefault [
+ switch i64 %a, label %unreachabledefault [
i64 0, label %branch_0
i64 2, label %branch_2
i64 4, label %branch_4
@@ -312,6 +312,6 @@ unreachabledefault:
unreachable
exit:
- %2 = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ]
- ret i64 %2
+ %b = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ]
+ ret i64 %b
}
diff --git a/llvm/test/Transforms/Inline/inline-switch-default.ll b/llvm/test/Transforms/Inline/inline-switch-default.ll
index 6271d6c06dcc6..44f1304e82dff 100644
--- a/llvm/test/Transforms/Inline/inline-switch-default.ll
+++ b/llvm/test/Transforms/Inline/inline-switch-default.ll
@@ -7,25 +7,25 @@ target triple = "x86_64-unknown-linux-gnu"
; The `bar1` should not be inlined since there is a default branch.
-define i64 @foo1(i64 %0) {
+define i64 @foo1(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @foo1(
-; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
-; LOOKUPTABLE-NEXT: [[TMP2:%.*]] = call i64 @bar1(i64 [[TMP0]])
-; LOOKUPTABLE-NEXT: ret i64 [[TMP2]]
+; LOOKUPTABLE-SAME: i64 [[A:%.*]]) {
+; LOOKUPTABLE-NEXT: [[B:%.*]] = call i64 @bar1(i64 [[A]])
+; LOOKUPTABLE-NEXT: ret i64 [[B]]
;
; SWITCH-LABEL: define i64 @foo1(
-; SWITCH-SAME: i64 [[TMP0:%.*]]) {
-; SWITCH-NEXT: [[TMP2:%.*]] = call i64 @bar1(i64 [[TMP0]])
-; SWITCH-NEXT: ret i64 [[TMP2]]
+; SWITCH-SAME: i64 [[A:%.*]]) {
+; SWITCH-NEXT: [[B:%.*]] = call i64 @bar1(i64 [[A]])
+; SWITCH-NEXT: ret i64 [[B]]
;
- %2 = call i64 @bar1(i64 %0)
- ret i64 %2
+ %b = call i64 @bar1(i64 %a)
+ ret i64 %b
}
-define i64 @foo2(i64 %0) {
+define i64 @foo2(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @foo2(
-; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
-; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; LOOKUPTABLE-SAME: i64 [[A:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[A]], 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:%.*]]
@@ -42,12 +42,12 @@ define i64 @foo2(i64 %0) {
; 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]]
+; LOOKUPTABLE-NEXT: [[B_I:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[B_I]]
;
; SWITCH-LABEL: define i64 @foo2(
-; SWITCH-SAME: i64 [[TMP0:%.*]]) {
-; SWITCH-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT_I:%.*]] [
+; SWITCH-SAME: i64 [[A:%.*]]) {
+; SWITCH-NEXT: switch i64 [[A]], 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:%.*]]
@@ -64,17 +64,17 @@ define i64 @foo2(i64 %0) {
; 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]]
+; SWITCH-NEXT: [[B_I:%.*]] = phi i64 [ 5, [[BRANCH_0_I]] ], [ 9, [[BRANCH_2_I]] ], [ 2, [[BRANCH_4_I]] ], [ 7, [[BRANCH_6_I]] ]
+; SWITCH-NEXT: ret i64 [[B_I]]
;
- %2 = call i64 @bar2(i64 %0)
- ret i64 %2
+ %b = call i64 @bar2(i64 %a)
+ ret i64 %b
}
-define i64 @bar1(i64 %0) {
+define i64 @bar1(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @bar1(
-; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
-; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; LOOKUPTABLE-SAME: i64 [[A:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[A]], label [[DEFAULT_BRANCH:%.*]] [
; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0:%.*]]
; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2:%.*]]
; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4:%.*]]
@@ -91,12 +91,12 @@ define i64 @bar1(i64 %0) {
; 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]]
+; LOOKUPTABLE-NEXT: [[B:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[B]]
;
; SWITCH-LABEL: define i64 @bar1(
-; SWITCH-SAME: i64 [[TMP0:%.*]]) {
-; SWITCH-NEXT: switch i64 [[TMP0]], label [[DEFAULT_BRANCH:%.*]] [
+; SWITCH-SAME: i64 [[A:%.*]]) {
+; SWITCH-NEXT: switch i64 [[A]], label [[DEFAULT_BRANCH:%.*]] [
; SWITCH-NEXT: i64 0, label [[BRANCH_0:%.*]]
; SWITCH-NEXT: i64 2, label [[BRANCH_2:%.*]]
; SWITCH-NEXT: i64 4, label [[BRANCH_4:%.*]]
@@ -113,10 +113,10 @@ define i64 @bar1(i64 %0) {
; 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-NEXT: [[B:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ], [ 3, [[DEFAULT_BRANCH]] ]
+; SWITCH-NEXT: ret i64 [[B]]
;
- switch i64 %0, label %default_branch [
+ switch i64 %a, label %default_branch [
i64 0, label %branch_0
i64 2, label %branch_2
i64 4, label %branch_4
@@ -139,14 +139,14 @@ 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
+ %b = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ], [ 3, %default_branch ]
+ ret i64 %b
}
-define i64 @bar2(i64 %0) {
+define i64 @bar2(i64 %a) {
; LOOKUPTABLE-LABEL: define i64 @bar2(
-; LOOKUPTABLE-SAME: i64 [[TMP0:%.*]]) {
-; LOOKUPTABLE-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; LOOKUPTABLE-SAME: i64 [[A:%.*]]) {
+; LOOKUPTABLE-NEXT: switch i64 [[A]], label [[UNREACHABLEDEFAULT:%.*]] [
; LOOKUPTABLE-NEXT: i64 0, label [[BRANCH_0:%.*]]
; LOOKUPTABLE-NEXT: i64 2, label [[BRANCH_2:%.*]]
; LOOKUPTABLE-NEXT: i64 4, label [[BRANCH_4:%.*]]
@@ -163,12 +163,12 @@ define i64 @bar2(i64 %0) {
; 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]]
+; LOOKUPTABLE-NEXT: [[B:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; LOOKUPTABLE-NEXT: ret i64 [[B]]
;
; SWITCH-LABEL: define i64 @bar2(
-; SWITCH-SAME: i64 [[TMP0:%.*]]) {
-; SWITCH-NEXT: switch i64 [[TMP0]], label [[UNREACHABLEDEFAULT:%.*]] [
+; SWITCH-SAME: i64 [[A:%.*]]) {
+; SWITCH-NEXT: switch i64 [[A]], label [[UNREACHABLEDEFAULT:%.*]] [
; SWITCH-NEXT: i64 0, label [[BRANCH_0:%.*]]
; SWITCH-NEXT: i64 2, label [[BRANCH_2:%.*]]
; SWITCH-NEXT: i64 4, label [[BRANCH_4:%.*]]
@@ -185,10 +185,10 @@ define i64 @bar2(i64 %0) {
; 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-NEXT: [[B:%.*]] = phi i64 [ 5, [[BRANCH_0]] ], [ 9, [[BRANCH_2]] ], [ 2, [[BRANCH_4]] ], [ 7, [[BRANCH_6]] ]
+; SWITCH-NEXT: ret i64 [[B]]
;
- switch i64 %0, label %unreachabledefault [
+ switch i64 %a, label %unreachabledefault [
i64 0, label %branch_0
i64 2, label %branch_2
i64 4, label %branch_4
@@ -211,6 +211,6 @@ unreachabledefault:
unreachable
exit:
- %2 = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ]
- ret i64 %2
+ %b = phi i64 [ 5, %branch_0 ], [ 9, %branch_2 ], [ 2, %branch_4 ], [ 7, %branch_6 ]
+ ret i64 %b
}
>From 1d2170ca03b2a89e28dd26d1145c7b560f07ce8f Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Wed, 7 Feb 2024 08:40:39 +0800
Subject: [PATCH 6/6] fixup! [InlineCost] Consider the default branch when
calculating cost
---
.../llvm/Analysis/InlineModelFeatureMaps.h | 2 ++
.../llvm/Analysis/TargetTransformInfoImpl.h | 3 +--
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 11 ++++----
llvm/lib/Analysis/InlineCost.cpp | 25 ++++++++++---------
4 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/llvm/include/llvm/Analysis/InlineModelFeatureMaps.h b/llvm/include/llvm/Analysis/InlineModelFeatureMaps.h
index ca9bb7244d669..d62ec9ce49119 100644
--- a/llvm/include/llvm/Analysis/InlineModelFeatureMaps.h
+++ b/llvm/include/llvm/Analysis/InlineModelFeatureMaps.h
@@ -39,6 +39,8 @@ namespace llvm {
M(int64_t, {1}, jump_table_penalty, "Accumulation of costs for jump tables") \
M(int64_t, {1}, case_cluster_penalty, \
"Accumulation of costs for case clusters") \
+ M(int64_t, {1}, switch_default_dest_penalty, \
+ "Accumulation of costs for switch default destination") \
M(int64_t, {1}, switch_penalty, \
"Accumulation of costs for switch statements") \
M(int64_t, {1}, unsimplified_common_instructions, \
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 0349c144d9723..60eab53fa2f60 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 = !SI.defaultDestUndefined();
- return SI.getNumCases() + HasDefault;
+ return SI.getNumCases();
}
unsigned getInliningThresholdMultiplier() const { return 1; }
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 8152efdf60902..5e7bdcdf72a49 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -446,7 +446,6 @@ 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 = !SI.defaultDestUndefined();
const TargetLoweringBase *TLI = getTLI();
const DataLayout &DL = this->getDataLayout();
@@ -455,7 +454,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 + HasDefault;
+ return N;
APInt MaxCaseVal = SI.case_begin()->getCaseValue()->getValue();
APInt MinCaseVal = MaxCaseVal;
@@ -475,23 +474,23 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
if (TLI->isSuitableForBitTests(Dests.size(), N, MinCaseVal, MaxCaseVal,
DL))
- return 1 + HasDefault;
+ return 1;
}
// Check if suitable for a jump table.
if (IsJTAllowed) {
if (N < 2 || N < TLI->getMinimumJumpTableEntries())
- return N + HasDefault;
+ return N;
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 + HasDefault;
+ return 1;
}
}
- return N + HasDefault;
+ return N;
}
bool shouldBuildLookupTables() {
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index 7b9e6e02f7b09..dc6878039f6a6 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -336,8 +336,8 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
/// 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,8 +699,10 @@ 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.
@@ -708,8 +710,6 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
int64_t JTCost =
static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;
addCost(JTCost);
- if (NumCaseCluster > 1)
- addCost((NumCaseCluster - 1) * 2 * InstrCost);
return;
}
@@ -1154,6 +1154,7 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
// 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
@@ -1232,16 +1233,16 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
}
}
- 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 +
JTCostMultiplier * InstrCost;
increment(InlineCostFeatureIndex::jump_table_penalty, JTCost);
- if (NumCaseCluster > 1)
- increment(InlineCostFeatureIndex::case_cluster_penalty,
- (NumCaseCluster - 1) * CaseClusterCostMultiplier * InstrCost);
return;
}
@@ -2465,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;
}
More information about the llvm-commits
mailing list