[llvm] 6a44edb - [InstCombine] fold abs of select with negated op (PR39474)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 04:44:03 PDT 2020


Author: Sanjay Patel
Date: 2020-08-24T07:37:55-04:00
New Revision: 6a44edb8da335bba355cd180a3f43564d14cd1be

URL: https://github.com/llvm/llvm-project/commit/6a44edb8da335bba355cd180a3f43564d14cd1be
DIFF: https://github.com/llvm/llvm-project/commit/6a44edb8da335bba355cd180a3f43564d14cd1be.diff

LOG: [InstCombine] fold abs of select with negated op (PR39474)

Similar to the existing transform - peek through a select
to match a value and its negation.

https://alive2.llvm.org/ce/z/MXi5KG

  define i8 @src(i1 %b, i8 %x) {
  %0:
    %neg = sub i8 0, %x
    %sel = select i1 %b, i8 %x, i8 %neg
    %abs = abs i8 %sel, 1
    ret i8 %abs
  }
  =>
  define i8 @tgt(i1 %b, i8 %x) {
  %0:
    %abs = abs i8 %x, 1
    ret i8 %abs
  }
  Transformation seems to be correct!

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/test/Transforms/InstCombine/abs-intrinsic.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index c6c4105b9b30..ef2563ce4cec 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -776,6 +776,10 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     Value *X;
     if (match(IIOperand, m_Neg(m_Value(X))))
       return replaceOperand(*II, 0, X);
+    if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
+      return replaceOperand(*II, 0, X);
+    if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
+      return replaceOperand(*II, 0, X);
 
     break;
   }

diff  --git a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
index 3bb9400fde74..20ba876c5344 100644
--- a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -107,9 +107,7 @@ define <4 x i32> @abs_of_neg_vec(<4 x i32> %x) {
 
 define i32 @abs_of_select_neg_true_val(i1 %b, i32 %x) {
 ; CHECK-LABEL: @abs_of_select_neg_true_val(
-; CHECK-NEXT:    [[NEG:%.*]] = sub i32 0, [[X:%.*]]
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[B:%.*]], i32 [[NEG]], i32 [[X]]
-; CHECK-NEXT:    [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[SEL]], i1 true)
+; CHECK-NEXT:    [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
 ; CHECK-NEXT:    ret i32 [[ABS]]
 ;
   %neg = sub i32 0, %x
@@ -120,9 +118,7 @@ define i32 @abs_of_select_neg_true_val(i1 %b, i32 %x) {
 
 define <4 x i32> @abs_of_select_neg_false_val(<4 x i1> %b, <4 x i32> %x) {
 ; CHECK-LABEL: @abs_of_select_neg_false_val(
-; CHECK-NEXT:    [[NEG:%.*]] = sub <4 x i32> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[SEL:%.*]] = select <4 x i1> [[B:%.*]], <4 x i32> [[X]], <4 x i32> [[NEG]]
-; CHECK-NEXT:    [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[SEL]], i1 false)
+; CHECK-NEXT:    [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[X:%.*]], i1 false)
 ; CHECK-NEXT:    ret <4 x i32> [[ABS]]
 ;
   %neg = sub <4 x i32> zeroinitializer, %x


        


More information about the llvm-commits mailing list