[PATCH] D112657: [InstCombine] allow Negator to fold multi-use select with constant arms

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 27 13:24:03 PDT 2021


spatel created this revision.
spatel added a reviewer: lebedev.ri.
Herald added subscribers: hiraditya, kristof.beyls, mcrosier.
spatel requested review of this revision.
Herald added a project: LLVM.

The motivating test is reduced from:
https://llvm.org/PR52261

Note that the more general problem of folding any binop into a multi-use select of constants is still there. We need to ease the restriction in InstCombinerImpl::FoldOpIntoSelect() to catch those. But these examples never reach that code because Negator exclusively handles negation patterns within visitSub().


https://reviews.llvm.org/D112657

Files:
  llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
  llvm/test/Transforms/InstCombine/sub-of-negatible.ll


Index: llvm/test/Transforms/InstCombine/sub-of-negatible.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -98,10 +98,10 @@
 
 define i8 @select_of_constants_multi_use(i1 %b) {
 ; CHECK-LABEL: @select_of_constants_multi_use(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[B:%.*]], i8 42, i8 -2
+; CHECK-NEXT:    [[S_NEG:%.*]] = select i1 [[B:%.*]], i8 -42, i8 2
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[B]], i8 42, i8 -2
 ; CHECK-NEXT:    call void @use8(i8 [[S]])
-; CHECK-NEXT:    [[N:%.*]] = sub nsw i8 0, [[S]]
-; CHECK-NEXT:    ret i8 [[N]]
+; CHECK-NEXT:    ret i8 [[S_NEG]]
 ;
   %s = select i1 %b, i8 42, i8 -2
   call void @use8(i8 %s)
@@ -111,10 +111,7 @@
 
 define i32 @PR52261(i1 %b) {
 ; CHECK-LABEL: @PR52261(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[B:%.*]], i32 2, i32 -2
-; CHECK-NEXT:    [[N:%.*]] = sub nsw i32 0, [[S]]
-; CHECK-NEXT:    [[A:%.*]] = and i32 [[S]], [[N]]
-; CHECK-NEXT:    ret i32 [[A]]
+; CHECK-NEXT:    ret i32 2
 ;
   %s = select i1 %b, i32 2, i32 -2
   %n = sub nsw i32 0, %s
Index: llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -215,6 +215,20 @@
                  : Builder.CreateSExt(I->getOperand(0), I->getType(),
                                       I->getName() + ".neg");
     break;
+  case Instruction::Select: {
+    // If both arms of the select are constants, we don't need to recurse.
+    // Therefore, this transform is not limited by uses.
+    auto *Sel = cast<SelectInst>(I);
+    Constant *TrueC, *FalseC;
+    if (match(Sel->getTrueValue(), m_ImmConstant(TrueC)) &&
+        match(Sel->getFalseValue(), m_ImmConstant(FalseC))) {
+      Constant *NegTrueC = ConstantExpr::getNeg(TrueC);
+      Constant *NegFalseC = ConstantExpr::getNeg(FalseC);
+      return Builder.CreateSelect(Sel->getCondition(), NegTrueC, NegFalseC,
+                                  I->getName() + ".neg", /*MDFrom=*/I);
+    }
+    break;
+  }
   default:
     break; // Other instructions require recursive reasoning.
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112657.382764.patch
Type: text/x-patch
Size: 2293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211027/3f8d4158/attachment.bin>


More information about the llvm-commits mailing list