[PATCH] D145458: [InstSimplify] Add simplifications for `min/max(Abs(X), X)`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 6 18:07:37 PST 2023


goldstein.w.n created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

`smax(Abs(X, t/f), X) --> Abs(X)`

- https://alive2.llvm.org/ce/z/FJf3pM
- https://alive2.llvm.org/ce/z/Lem-WV

`umin(Abs(X, t/f), X) --> Abs(X)`

- https://alive2.llvm.org/ce/z/4LG-33
- https://alive2.llvm.org/ce/z/_LVNYi

`smin(Abs(X, t/f), X) --> X`

- https://alive2.llvm.org/ce/z/bNMkWz
- https://alive2.llvm.org/ce/z/VkMrcp

`umax(Abs(X, t/f), X) --> X`

- https://alive2.llvm.org/ce/z/ewXWfe
- https://alive2.llvm.org/ce/z/JHy-LC


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145458

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/minmax-intrin.ll


Index: llvm/test/Transforms/InstSimplify/minmax-intrin.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/minmax-intrin.ll
+++ llvm/test/Transforms/InstSimplify/minmax-intrin.ll
@@ -80,9 +80,7 @@
 
 define i8 @umax_absT(i8 %x) {
 ; CHECK-LABEL: @umax_absT(
-; CHECK-NEXT:    [[XX:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 true)
-; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.umax.i8(i8 [[XX]], i8 [[X]])
-; CHECK-NEXT:    ret i8 [[RET]]
+; CHECK-NEXT:    ret i8 [[X:%.*]]
 ;
   %xx = call i8 @llvm.abs.i8(i8 %x, i1 true)
   %ret = call i8 @llvm.umax.i8(i8 %xx, i8 %x)
@@ -103,8 +101,7 @@
 define <2 x i8> @umin_absF(<2 x i8> %x) {
 ; CHECK-LABEL: @umin_absF(
 ; CHECK-NEXT:    [[XX:%.*]] = call <2 x i8> @llvm.abs.v2i8(<2 x i8> [[X:%.*]], i1 false)
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[X]], <2 x i8> [[XX]])
-; CHECK-NEXT:    ret <2 x i8> [[RET]]
+; CHECK-NEXT:    ret <2 x i8> [[XX]]
 ;
   %xx = call <2 x i8> @llvm.abs.v2i8(<2 x i8> %x, i1 false)
   %ret = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %x, <2 x i8> %xx)
@@ -114,8 +111,7 @@
 define i8 @smax_absT(i8 %x) {
 ; CHECK-LABEL: @smax_absT(
 ; CHECK-NEXT:    [[XX:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 true)
-; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[XX]])
-; CHECK-NEXT:    ret i8 [[RET]]
+; CHECK-NEXT:    ret i8 [[XX]]
 ;
   %xx = call i8 @llvm.abs.i8(i8 %x, i1 true)
   %ret = call i8 @llvm.smax.i8(i8 %x, i8 %xx)
@@ -124,9 +120,7 @@
 
 define i8 @smin_absF(i8 %x) {
 ; CHECK-LABEL: @smin_absF(
-; CHECK-NEXT:    [[XX:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 false)
-; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.smin.i8(i8 [[XX]], i8 [[X]])
-; CHECK-NEXT:    ret i8 [[RET]]
+; CHECK-NEXT:    ret i8 [[X:%.*]]
 ;
   %xx = call i8 @llvm.abs.i8(i8 %x, i1 false)
   %ret = call i8 @llvm.smin.i8(i8 %xx, i8 %x)
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6122,6 +6122,12 @@
               m_Intrinsic<Intrinsic::usub_sat>(m_Value(), m_Specific(Op1))))
       return IID == Intrinsic::umin ? Op0 : Op1;
   }
+  // smax(Abs(X, t/f), X) --> Abs(X, t/f)
+  // umin(Abs(X, t/f), X) --> Abs(X, t/f)
+  // smin(Abs(X, t/f), X) --> X
+  // umax(Abs(X, t/f), X) --> X
+  if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Specific(Op1), m_Value())))
+    return (IID == Intrinsic::smax || IID == Intrinsic::umin) ? Op0 : Op1;
 
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145458.502884.patch
Type: text/x-patch
Size: 2593 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230307/b61d06fa/attachment.bin>


More information about the llvm-commits mailing list