[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
Thu Oct 28 05:36:07 PDT 2021
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe8535fa78458: [InstCombine] allow Negator to fold multi-use select with constant arms (authored by spatel).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D112657/new/
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.382997.patch
Type: text/x-patch
Size: 2293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211028/80c8943d/attachment.bin>
More information about the llvm-commits
mailing list