[llvm] r290913 - InstCombine: Fold fabs on select of constants
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 3 14:40:34 PST 2017
Author: arsenm
Date: Tue Jan 3 16:40:34 2017
New Revision: 290913
URL: http://llvm.org/viewvc/llvm-project?rev=290913&view=rev
Log:
InstCombine: Fold fabs on select of constants
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/trunk/test/Transforms/InstCombine/fabs.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=290913&r1=290912&r2=290913&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Tue Jan 3 16:40:34 2017
@@ -1625,6 +1625,18 @@ Instruction *InstCombiner::visitCallInst
break;
}
+ case Intrinsic::fabs: {
+ Value *Cond;
+ Constant *LHS, *RHS;
+ if (match(II->getArgOperand(0),
+ m_Select(m_Value(Cond), m_Constant(LHS), m_Constant(RHS)))) {
+ CallInst *Call0 = Builder->CreateCall(II->getCalledFunction(), {LHS});
+ CallInst *Call1 = Builder->CreateCall(II->getCalledFunction(), {RHS});
+ return SelectInst::Create(Cond, Call0, Call1);
+ }
+
+ break;
+ }
case Intrinsic::ppc_altivec_lvx:
case Intrinsic::ppc_altivec_lvxl:
// Turn PPC lvx -> load if the pointer is known aligned.
Modified: llvm/trunk/test/Transforms/InstCombine/fabs.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fabs.ll?rev=290913&r1=290912&r2=290913&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fabs.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fabs.ll Tue Jan 3 16:40:34 2017
@@ -98,3 +98,51 @@ define float @square_fabs_shrink_call2(f
; CHECK-NEXT: ret float %sq
}
+; CHECK-LABEL: @fabs_select_constant_negative_positive(
+; CHECK: %fabs = select i1 %cmp, float 1.000000e+00, float 2.000000e+00
+; CHECK-NEXT: ret float %fabs
+define float @fabs_select_constant_negative_positive(i32 %c) {
+ %cmp = icmp eq i32 %c, 0
+ %select = select i1 %cmp, float -1.0, float 2.0
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
+
+; CHECK-LABEL: @fabs_select_constant_positive_negative(
+; CHECK: %fabs = select i1 %cmp, float 1.000000e+00, float 2.000000e+00
+; CHECK-NEXT: ret float %fabs
+define float @fabs_select_constant_positive_negative(i32 %c) {
+ %cmp = icmp eq i32 %c, 0
+ %select = select i1 %cmp, float 1.0, float -2.0
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
+
+; CHECK-LABEL: @fabs_select_constant_negative_negative(
+; CHECK: %fabs = select i1 %cmp, float 1.000000e+00, float 2.000000e+00
+; CHECK-NEXT: ret float %fabs
+define float @fabs_select_constant_negative_negative(i32 %c) {
+ %cmp = icmp eq i32 %c, 0
+ %select = select i1 %cmp, float -1.0, float -2.0
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
+
+; CHECK-LABEL: @fabs_select_constant_neg0(
+; CHECK-NEXT: ret float 0.0
+define float @fabs_select_constant_neg0(i32 %c) {
+ %cmp = icmp eq i32 %c, 0
+ %select = select i1 %cmp, float -0.0, float 0.0
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
+
+; CHECK-LABEL: @fabs_select_var_constant_negative(
+; CHECK: %select = select i1 %cmp, float %x, float -1.000000e+00
+; CHECK: %fabs = call float @llvm.fabs.f32(float %select)
+define float @fabs_select_var_constant_negative(i32 %c, float %x) {
+ %cmp = icmp eq i32 %c, 0
+ %select = select i1 %cmp, float %x, float -1.0
+ %fabs = call float @llvm.fabs.f32(float %select)
+ ret float %fabs
+}
More information about the llvm-commits
mailing list