summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2021-10-28 08:11:59 -0400
committerSanjay Patel <spatel@rotateright.com>2021-10-28 08:35:58 -0400
commite8535fa78458abe37e54e5c01d52558c5e284092 (patch)
treeee6455fb2146dffc450f5904387f4171c7650bae
parent98f08752f76b80a509037986d620b402c48f5115 (diff)
downloadllvm-e8535fa78458abe37e54e5c01d52558c5e284092.tar.gz
[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
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp14
-rw-r--r--llvm/test/Transforms/InstCombine/sub-of-negatible.ll11
2 files changed, 18 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index 37c7e6135501..7dc516c6fdc3 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 6c6fdbf35ed1..67abc286298a 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