[llvm] [InstCombine] Canonicalize `extractvalue + select` (PR #84686)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 13 10:33:26 PDT 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/84686
>From fe4a86466c9f222f5c3251ff9e450524827bca8c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 14 Mar 2024 01:15:50 +0800
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests. NFC.
---
.../InstCombine/extract-select-agg.ll | 85 +++++++++++++++++++
1 file changed, 85 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/extract-select-agg.ll
diff --git a/llvm/test/Transforms/InstCombine/extract-select-agg.ll b/llvm/test/Transforms/InstCombine/extract-select-agg.ll
new file mode 100644
index 00000000000000..745bce1f23246d
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/extract-select-agg.ll
@@ -0,0 +1,85 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i64 @test_select_agg_constant_agg(i64 %val, i1 %cond) {
+; CHECK-LABEL: define i64 @test_select_agg_constant_agg(
+; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } { i64 1, i64 2 }, { i64, i64 } { i64 0, i64 3 }
+; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
+; CHECK-NEXT: ret i64 [[RET]]
+;
+entry:
+ %sel = select i1 %cond, { i64, i64 } {i64 1, i64 2}, { i64, i64 } {i64 0, i64 3}
+ %ret = extractvalue { i64, i64 } %sel, 0
+ ret i64 %ret
+}
+
+define void @test_select_agg_constant_agg_multiuse(i64 %val, i1 %cond) {
+; CHECK-LABEL: define void @test_select_agg_constant_agg_multiuse(
+; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } { i64 1, i64 2 }, { i64, i64 } { i64 0, i64 3 }
+; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
+; CHECK-NEXT: call void @use(i64 [[RET]])
+; CHECK-NEXT: [[V1:%.*]] = extractvalue { i64, i64 } [[SEL]], 1
+; CHECK-NEXT: call void @use(i64 [[V1]])
+; CHECK-NEXT: ret void
+;
+entry:
+ %sel = select i1 %cond, { i64, i64 } {i64 1, i64 2}, { i64, i64 } {i64 0, i64 3}
+ %v0 = extractvalue { i64, i64 } %sel, 0
+ call void @use(i64 %v0)
+ %v1 = extractvalue { i64, i64 } %sel, 1
+ call void @use(i64 %v1)
+ ret void
+}
+
+; TODO: it can be folded to zext i1 %cond to i64
+define i64 @test_select_agg_constant(i64 %val, i1 %cond) {
+; CHECK-LABEL: define i64 @test_select_agg_constant(
+; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[A:%.*]] = insertvalue { i64, i64 } { i64 1, i64 poison }, i64 [[VAL]], 1
+; CHECK-NEXT: [[B:%.*]] = insertvalue { i64, i64 } { i64 0, i64 poison }, i64 [[VAL]], 1
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } [[A]], { i64, i64 } [[B]]
+; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
+; CHECK-NEXT: ret i64 [[RET]]
+;
+entry:
+ %a = insertvalue { i64, i64 } { i64 1, i64 poison }, i64 %val, 1
+ %b = insertvalue { i64, i64 } { i64 0, i64 poison }, i64 %val, 1
+ %sel = select i1 %cond, { i64, i64 } %a, { i64, i64 } %b
+ %ret = extractvalue { i64, i64 } %sel, 0
+ ret i64 %ret
+}
+
+define void @test_select_agg_multiuse(i1 %cond, i64 %v1, i64 %v2, i64 %v3, i64 %v4) {
+; CHECK-LABEL: define void @test_select_agg_multiuse(
+; CHECK-SAME: i1 [[COND:%.*]], i64 [[V1:%.*]], i64 [[V2:%.*]], i64 [[V3:%.*]], i64 [[V4:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[A0:%.*]] = insertvalue { i64, i64 } poison, i64 [[V1]], 0
+; CHECK-NEXT: [[A1:%.*]] = insertvalue { i64, i64 } [[A0]], i64 [[V2]], 1
+; CHECK-NEXT: [[B0:%.*]] = insertvalue { i64, i64 } poison, i64 [[V3]], 0
+; CHECK-NEXT: [[B1:%.*]] = insertvalue { i64, i64 } [[B0]], i64 [[V4]], 1
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } [[A1]], { i64, i64 } [[B1]]
+; CHECK-NEXT: [[X:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
+; CHECK-NEXT: call void @use(i64 [[X]])
+; CHECK-NEXT: [[Y:%.*]] = extractvalue { i64, i64 } [[SEL]], 1
+; CHECK-NEXT: call void @use(i64 [[Y]])
+; CHECK-NEXT: ret void
+;
+entry:
+ %a0 = insertvalue { i64, i64 } poison, i64 %v1, 0
+ %a1 = insertvalue { i64, i64 } %a0, i64 %v2, 1
+ %b0 = insertvalue { i64, i64 } poison, i64 %v3, 0
+ %b1 = insertvalue { i64, i64 } %b0, i64 %v4, 1
+ %sel = select i1 %cond, { i64, i64 } %a1, { i64, i64 } %b1
+ %x = extractvalue { i64, i64 } %sel, 0
+ call void @use(i64 %x)
+ %y = extractvalue { i64, i64 } %sel, 1
+ call void @use(i64 %y)
+ ret void
+}
+
+declare void @use(i64)
>From 8936a8ed249552bbdd78555da53e5d60fc696e27 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 14 Mar 2024 01:17:44 +0800
Subject: [PATCH 2/2] [InstCombine] Canonicalize `extractvalue + select`
---
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 6 ++++++
llvm/test/Transforms/InstCombine/extract-select-agg.ll | 8 +++-----
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 1688005de2104d..1668989a2ebb33 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3834,6 +3834,12 @@ Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
if (Instruction *Res = foldOpIntoPhi(EV, PN))
return Res;
+ // Canonicalize extract (select Cond, TV, FV)
+ // -> select cond, (extract TV), (extract FV)
+ if (auto *SI = dyn_cast<SelectInst>(Agg))
+ if (Instruction *R = FoldOpIntoSelect(EV, SI, /*FoldWithMultiUse=*/true))
+ return R;
+
// We could simplify extracts from other values. Note that nested extracts may
// already be simplified implicitly by the above: extract (extract (insert) )
// will be translated into extract ( insert ( extract ) ) first and then just
diff --git a/llvm/test/Transforms/InstCombine/extract-select-agg.ll b/llvm/test/Transforms/InstCombine/extract-select-agg.ll
index 745bce1f23246d..6ba6b1a575601d 100644
--- a/llvm/test/Transforms/InstCombine/extract-select-agg.ll
+++ b/llvm/test/Transforms/InstCombine/extract-select-agg.ll
@@ -5,8 +5,7 @@ define i64 @test_select_agg_constant_agg(i64 %val, i1 %cond) {
; CHECK-LABEL: define i64 @test_select_agg_constant_agg(
; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } { i64 1, i64 2 }, { i64, i64 } { i64 0, i64 3 }
-; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
+; CHECK-NEXT: [[RET:%.*]] = zext i1 [[COND]] to i64
; CHECK-NEXT: ret i64 [[RET]]
;
entry:
@@ -19,10 +18,9 @@ define void @test_select_agg_constant_agg_multiuse(i64 %val, i1 %cond) {
; CHECK-LABEL: define void @test_select_agg_constant_agg_multiuse(
; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } { i64 1, i64 2 }, { i64, i64 } { i64 0, i64 3 }
-; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
+; CHECK-NEXT: [[RET:%.*]] = zext i1 [[COND]] to i64
; CHECK-NEXT: call void @use(i64 [[RET]])
-; CHECK-NEXT: [[V1:%.*]] = extractvalue { i64, i64 } [[SEL]], 1
+; CHECK-NEXT: [[V1:%.*]] = select i1 [[COND]], i64 2, i64 3
; CHECK-NEXT: call void @use(i64 [[V1]])
; CHECK-NEXT: ret void
;
More information about the llvm-commits
mailing list