[PATCH] D101257: [InstCombine] C - ctpop(a) - > ctpop(~a)) if C is bitwidth (PR50104)
Dávid Bolvanský via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 25 08:10:00 PDT 2021
xbolva00 created this revision.
xbolva00 added reviewers: spatel, nikic.
Herald added a subscriber: hiraditya.
xbolva00 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Proof: https://alive2.llvm.org/ce/z/mncA9K
Solves https://bugs.llvm.org/show_bug.cgi?id=50104
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D101257
Files:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/ctpop.ll
Index: llvm/test/Transforms/InstCombine/ctpop.ll
===================================================================
--- llvm/test/Transforms/InstCombine/ctpop.ll
+++ llvm/test/Transforms/InstCombine/ctpop.ll
@@ -292,9 +292,9 @@
define i8 @sub_ctpop(i8 %a) {
; CHECK-LABEL: @sub_ctpop(
-; CHECK-NEXT: [[CNT:%.*]] = tail call i8 @llvm.ctpop.i8(i8 [[A:%.*]]), !range [[RNG0]]
-; CHECK-NEXT: [[RES:%.*]] = sub nuw nsw i8 8, [[CNT]]
-; CHECK-NEXT: ret i8 [[RES]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[A:%.*]], -1
+; CHECK-NEXT: [[TMP2:%.*]] = call i8 @llvm.ctpop.i8(i8 [[TMP1]]), !range [[RNG0]]
+; CHECK-NEXT: ret i8 [[TMP2]]
;
%cnt = tail call i8 @llvm.ctpop.i8(i8 %a)
%res = sub i8 8, %cnt
@@ -325,9 +325,9 @@
define <2 x i32> @sub_ctpop_vec(<2 x i32> %a) {
; CHECK-LABEL: @sub_ctpop_vec(
-; CHECK-NEXT: [[CNT:%.*]] = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[A:%.*]])
-; CHECK-NEXT: [[RES:%.*]] = sub nuw nsw <2 x i32> <i32 32, i32 32>, [[CNT]]
-; CHECK-NEXT: ret <2 x i32> [[RES]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i32> [[A:%.*]], <i32 -1, i32 -1>
+; CHECK-NEXT: [[TMP2:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[TMP1]])
+; CHECK-NEXT: ret <2 x i32> [[TMP2]]
;
%cnt = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %a)
%res = sub <2 x i32> <i32 32, i32 32>, %cnt
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2112,6 +2112,13 @@
return replaceInstUsesWith(
I, Builder.CreateIntrinsic(Intrinsic::umin, {I.getType()}, {Op0, Y}));
+ // C - ctpop(X) => ctpop(~X) if C is bitwidth
+ if (match(Op0, m_SpecificInt(Ty->getScalarSizeInBits())) &&
+ match(Op1, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(m_Value(X)))))
+ return replaceInstUsesWith(
+ I, Builder.CreateIntrinsic(Intrinsic::ctpop, {I.getType()},
+ {Builder.CreateNot(X)}));
+
return TryToNarrowDeduceFlags();
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101257.340360.patch
Type: text/x-patch
Size: 2122 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210425/95fc3152/attachment.bin>
More information about the llvm-commits
mailing list