[PATCH] D147299: [InstCombine] Add support for maximum(a,b) + minimum(a,b) => a + b

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 22:59:32 PDT 2023


skatkov created this revision.
skatkov added reviewers: spatel, nikic, lebedev.ri, RKSimon, dantrushin, mkazantsev, fhahn, efriedma.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
skatkov requested review of this revision.
Herald added a project: LLVM.

The same optimization for

  maximum(a,b) * minimum(a,b) => a * b

is added.


https://reviews.llvm.org/D147299

Files:
  llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/fadd-maximum-minimum.ll
  llvm/test/Transforms/InstCombine/fmul-maximum-minimum.ll


Index: llvm/test/Transforms/InstCombine/fmul-maximum-minimum.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/fmul-maximum-minimum.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare float    @llvm.minimum.f32(float %Val0, float %Val1)
+declare float    @llvm.maximum.f32(float %Val0, float %Val1)
+
+define float @test(float %a, float %b) {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[RES:%.*]] = fmul float [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    ret float [[RES]]
+;
+entry:
+  %min = call float @llvm.minimum.f32(float %b, float %a)
+  %max = call float @llvm.maximum.f32(float %a, float %b)
+  %res = fmul float %min, %max
+  ret float %res
+}
Index: llvm/test/Transforms/InstCombine/fadd-maximum-minimum.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/fadd-maximum-minimum.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare float    @llvm.minimum.f32(float %Val0, float %Val1)
+declare float    @llvm.maximum.f32(float %Val0, float %Val1)
+
+define float @test(float %a, float %b) {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[RES:%.*]] = fadd float [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    ret float [[RES]]
+;
+entry:
+  %min = call float @llvm.minimum.f32(float %b, float %a)
+  %max = call float @llvm.maximum.f32(float %a, float %b)
+  %res = fadd float %min, %max
+  ret float %res
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -765,6 +765,16 @@
       I.hasNoSignedZeros() && match(Start, m_Zero()))
     return replaceInstUsesWith(I, Start);
 
+  // minumin(X, Y) * maximum(X, Y) => X * Y.
+  if (match(&I,
+            m_c_FMul(m_Intrinsic<Intrinsic::maximum>(m_Value(X), m_Value(Y)),
+                     m_CombineOr(m_Intrinsic<Intrinsic::minimum>(m_Deferred(X),
+                                                                 m_Deferred(Y)),
+                                 m_Intrinsic<Intrinsic::minimum>(
+                                     m_Deferred(Y), m_Deferred(X)))))) {
+    return BinaryOperator::CreateFMulFMF(X, Y, &I);
+  }
+
   return nullptr;
 }
 
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1804,6 +1804,16 @@
       return replaceInstUsesWith(I, V);
   }
 
+  // minumin(X, Y) + maximum(X, Y) => X + Y.
+  if (match(&I,
+            m_c_FAdd(m_Intrinsic<Intrinsic::maximum>(m_Value(X), m_Value(Y)),
+                     m_CombineOr(m_Intrinsic<Intrinsic::minimum>(m_Deferred(X),
+                                                                 m_Deferred(Y)),
+                                 m_Intrinsic<Intrinsic::minimum>(
+                                     m_Deferred(Y), m_Deferred(X)))))) {
+    return BinaryOperator::CreateFAddFMF(X, Y, &I);
+  }
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147299.509908.patch
Type: text/x-patch
Size: 3455 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230331/808a81eb/attachment.bin>


More information about the llvm-commits mailing list