Skip to content

Commit cf37ae5

Browse files
authored
[InstCombine] Add one-use check when folding fabs over selects (#122270)
Fixes multi-use issue introduced by #86390. It allows the folding of `fabs (select Cond, TrueC, FalseC)` to avoid performance regression in ocio
1 parent 058d183 commit cf37ae5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
27522752

27532753
if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
27542754
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
2755-
if (isa<Constant>(TVal) || isa<Constant>(FVal)) {
2755+
if (Arg->hasOneUse() ? (isa<Constant>(TVal) || isa<Constant>(FVal))
2756+
: (isa<Constant>(TVal) && isa<Constant>(FVal))) {
27562757
CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
27572758
CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
27582759
SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);

llvm/test/Transforms/InstCombine/intrinsic-select.ll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
33

44
declare void @use(i32)
5+
declare void @usef32(float)
56

67
declare i32 @llvm.ctlz.i32(i32, i1)
78
declare <3 x i17> @llvm.ctlz.v3i17(<3 x i17>, i1)
@@ -344,3 +345,29 @@ define double @test_fabs_select_fmf2(i1 %cond, double %a) {
344345
%fabs = call nnan ninf nsz double @llvm.fabs.f64(double %sel1)
345346
ret double %fabs
346347
}
348+
349+
define float @test_fabs_select_multiuse(i1 %cond, float %x) {
350+
; CHECK-LABEL: @test_fabs_select_multiuse(
351+
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], float [[X:%.*]], float 0x7FF0000000000000
352+
; CHECK-NEXT: call void @usef32(float [[SELECT]])
353+
; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
354+
; CHECK-NEXT: ret float [[FABS]]
355+
;
356+
%select = select i1 %cond, float %x, float 0x7FF0000000000000
357+
call void @usef32(float %select)
358+
%fabs = call float @llvm.fabs.f32(float %select)
359+
ret float %fabs
360+
}
361+
362+
define float @test_fabs_select_multiuse_both_constant(i1 %cond, float %x) {
363+
; CHECK-LABEL: @test_fabs_select_multiuse_both_constant(
364+
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND:%.*]], float -1.000000e+00, float -2.000000e+00
365+
; CHECK-NEXT: call void @usef32(float [[SELECT]])
366+
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[COND]], float 1.000000e+00, float 2.000000e+00
367+
; CHECK-NEXT: ret float [[FABS]]
368+
;
369+
%select = select i1 %cond, float -1.0, float -2.0
370+
call void @usef32(float %select)
371+
%fabs = call float @llvm.fabs.f32(float %select)
372+
ret float %fabs
373+
}

0 commit comments

Comments
 (0)