[llvm] [InstCombine] Add one-use check when folding fabs over selects (PR #122270)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 9 06:28:41 PST 2025
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/122270
>From 93fa070d9cbb63a54ecb40554172028765b66649 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 9 Jan 2025 20:51:40 +0800
Subject: [PATCH 1/4] [InstCombine] Add pre-commit tests. NFC.
---
.../Transforms/InstCombine/intrinsic-select.ll | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index 4ce2908a630785..e7b092e1f34d77 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -2,6 +2,7 @@
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
declare void @use(i32)
+declare void @usef32(float)
declare i32 @llvm.ctlz.i32(i32, i1)
declare <3 x i17> @llvm.ctlz.v3i17(<3 x i17>, i1)
@@ -344,3 +345,17 @@ define double @test_fabs_select_fmf2(i1 %cond, double %a) {
%fabs = call nnan ninf nsz double @llvm.fabs.f64(double %sel1)
ret double %fabs
}
+
+define float @test_fabs_select_multiuse(i1 %cond, float %x) {
+; CHECK-LABEL: @test_fabs_select_multiuse(
+; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], float [[X:%.*]], float 0x7FF0000000000000
+; CHECK-NEXT: call void @usef32(float [[SELECT]])
+; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT: [[FABS:%.*]] = select i1 [[COND]], float [[TMP1]], float 0x7FF0000000000000
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %select = select i1 %cond, float %x, float 0x7FF0000000000000
+ call void @usef32(float %select)
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
>From b3a56728885f5a09f3d062b586518336a34f35e4 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 9 Jan 2025 21:02:26 +0800
Subject: [PATCH 2/4] [InstCombine] Add one-use check when folding fabs over
selects
---
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 2 +-
llvm/test/Transforms/InstCombine/intrinsic-select.ll | 3 +--
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index c55c40c88bc845..99ff12fcb37967 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2709,7 +2709,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
- if (isa<Constant>(TVal) || isa<Constant>(FVal)) {
+ if (Arg->hasOneUse() && (isa<Constant>(TVal) || isa<Constant>(FVal))) {
CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);
diff --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index e7b092e1f34d77..fd047e43a852e7 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -350,8 +350,7 @@ define float @test_fabs_select_multiuse(i1 %cond, float %x) {
; CHECK-LABEL: @test_fabs_select_multiuse(
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], float [[X:%.*]], float 0x7FF0000000000000
; CHECK-NEXT: call void @usef32(float [[SELECT]])
-; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[X]])
-; CHECK-NEXT: [[FABS:%.*]] = select i1 [[COND]], float [[TMP1]], float 0x7FF0000000000000
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
; CHECK-NEXT: ret float [[FABS]]
;
%select = select i1 %cond, float %x, float 0x7FF0000000000000
>From 4b87c01b3d9ce9423fca916bb284dcd71346bec3 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 9 Jan 2025 21:07:07 +0800
Subject: [PATCH 3/4] [InstCombine] Add more tests. NFC.
---
.../test/Transforms/InstCombine/intrinsic-select.ll | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index fd047e43a852e7..b8fb5eccb5730b 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -358,3 +358,16 @@ define float @test_fabs_select_multiuse(i1 %cond, float %x) {
%fabs = call float @llvm.fabs.f32(float %select)
ret float %fabs
}
+
+define float @test_fabs_select_multiuse_both_constant(i1 %cond, float %x) {
+; CHECK-LABEL: @test_fabs_select_multiuse_both_constant(
+; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], float -1.000000e+00, float -2.000000e+00
+; CHECK-NEXT: call void @usef32(float [[SELECT]])
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %select = select i1 %cond, float -1.0, float -2.0
+ call void @usef32(float %select)
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
>From 5e22edee64429dfe281ffd6a92c21d0005b74349 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 9 Jan 2025 22:28:09 +0800
Subject: [PATCH 4/4] [InstCombine] Allow folding with two constant arms
---
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 3 ++-
llvm/test/Transforms/InstCombine/intrinsic-select.ll | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 99ff12fcb37967..a19eded6565997 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2709,7 +2709,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
- if (Arg->hasOneUse() && (isa<Constant>(TVal) || isa<Constant>(FVal))) {
+ if (Arg->hasOneUse() ? (isa<Constant>(TVal) || isa<Constant>(FVal))
+ : (isa<Constant>(TVal) && isa<Constant>(FVal))) {
CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);
diff --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index b8fb5eccb5730b..a8117ce663a6d0 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -363,7 +363,7 @@ define float @test_fabs_select_multiuse_both_constant(i1 %cond, float %x) {
; CHECK-LABEL: @test_fabs_select_multiuse_both_constant(
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], float -1.000000e+00, float -2.000000e+00
; CHECK-NEXT: call void @usef32(float [[SELECT]])
-; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
+; CHECK-NEXT: [[FABS:%.*]] = select i1 [[COND]], float 1.000000e+00, float 2.000000e+00
; CHECK-NEXT: ret float [[FABS]]
;
%select = select i1 %cond, float -1.0, float -2.0
More information about the llvm-commits
mailing list