[llvm] e8535fa - [InstCombine] allow Negator to fold multi-use select with constant arms

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 28 05:36:04 PDT 2021


Author: Sanjay Patel
Date: 2021-10-28T08:35:58-04:00
New Revision: e8535fa78458abe37e54e5c01d52558c5e284092

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

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

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().

Differential Revision: https://reviews.llvm.org/D112657

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index 37c7e6135501c..7dc516c6fdc31 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -215,6 +215,20 @@ LLVM_NODISCARD Value *Negator::visitImpl(Value *V, unsigned Depth) {
                  : 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.
   }

diff  --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index 6c6fdbf35ed18..67abc286298ab 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -98,10 +98,10 @@ define i8 @t4(i8 %x, i1 %y) {
 
 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 i8 @select_of_constants_multi_use(i1 %b) {
 
 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


        


More information about the llvm-commits mailing list