[PATCH] D108814: InstCombine: avoid non-reducing xor combine

Tim Northover via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 27 03:38:24 PDT 2021


t.p.northover created this revision.
t.p.northover added a reviewer: spatel.
Herald added subscribers: hiraditya, mcrosier.
t.p.northover requested review of this revision.
Herald added a project: LLVM.

We should only fold `min(not X, C) -> not max(X, not C)` if `not C` is as simple as `C`, otherwise there's a competing `not max(X, not Y) -> min(not X, Y)` that will form an infinite loop. Checking for a non-expr constant is a good way to do this (the fold above already takes care of `C == not D` so we don't lose any power that way).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108814

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/minmax-intrinsics.ll


Index: llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
===================================================================
--- llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
+++ llvm/test/Transforms/InstCombine/minmax-intrinsics.ll
@@ -506,6 +506,17 @@
   ret i8 %m
 }
 
+define i8 @umin_of_not_and_nontrivial_const(i8 %x) {
+; CHECK-LABEL: @umin_of_not_and_nontrivial_const(
+; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 %x, -1
+; CHECK-NEXT:    [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[NOTX]], i8 ptrtoint ({{.*}}))
+; CHECK-NEXT:    ret i8 [[M]]
+;
+  %notx = xor i8 %x, -1
+  %m = call i8 @llvm.umin.i8(i8 ptrtoint (i8(i8)* @umin_of_not_and_nontrivial_const to i8), i8 %notx)
+  ret i8 %m
+}
+
 ; Negative test - too many uses
 
 define i8 @umin_of_not_and_const_uses(i8 %x) {
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1085,7 +1085,7 @@
         return BinaryOperator::CreateNot(InvMaxMin);
       }
       // max (not X), C --> not(min X, ~C)
-      if (match(I1, m_Constant(C)) && I0->hasOneUse()) {
+      if (match(I1, m_ImmConstant(C)) && I0->hasOneUse()) {
         Constant *NotC = ConstantExpr::getNot(C);
         Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotC);
         return BinaryOperator::CreateNot(InvMaxMin);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108814.369060.patch
Type: text/x-patch
Size: 1456 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210827/a3a42e24/attachment.bin>


More information about the llvm-commits mailing list