Skip to content

Commit

Permalink
[InstCombine] Add transform (icmp eq/ne bitreverse(x), C) -> `(icmp…
Browse files Browse the repository at this point in the history
… eq/ne x, bitreverse(C))`

EQ: https://alive2.llvm.org/ce/z/TESofr
NE: https://alive2.llvm.org/ce/z/mwloaT

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D145339
  • Loading branch information
goldsteinn committed Mar 7, 2023
1 parent a4bb08e commit e2079e8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Expand Up @@ -3282,6 +3282,11 @@ Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant(
return new ICmpInst(Pred, II->getArgOperand(0),
ConstantInt::get(Ty, C.byteSwap()));

case Intrinsic::bitreverse:
// bitreverse(A) == C -> A == bitreverse(C)
return new ICmpInst(Pred, II->getArgOperand(0),
ConstantInt::get(Ty, C.reverseBits()));

case Intrinsic::ctlz:
case Intrinsic::cttz: {
// ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
Expand Up @@ -767,8 +767,7 @@ define i1 @trunc_negative_destbits_not_enough(i33 %x) {

define i1 @bitreverse_ne_22(i8 %x) {
; CHECK-LABEL: @bitreverse_ne_22(
; CHECK-NEXT: [[Y:%.*]] = call i8 @llvm.bitreverse.i8(i8 [[X:%.*]])
; CHECK-NEXT: [[Z:%.*]] = icmp ne i8 [[Y]], 22
; CHECK-NEXT: [[Z:%.*]] = icmp ne i8 [[X:%.*]], 104
; CHECK-NEXT: ret i1 [[Z]]
;
%y = call i8 @llvm.bitreverse.i8(i8 %x)
Expand All @@ -790,8 +789,7 @@ define i1 @bitreverse_ult_22_fail_not_equality_pred(i8 %x) {

define <2 x i1> @bitreverse_vec_eq_2_2(<2 x i8> %x) {
; CHECK-LABEL: @bitreverse_vec_eq_2_2(
; CHECK-NEXT: [[Y:%.*]] = call <2 x i8> @llvm.bitreverse.v2i8(<2 x i8> [[X:%.*]])
; CHECK-NEXT: [[Z:%.*]] = icmp eq <2 x i8> [[Y]], <i8 2, i8 2>
; CHECK-NEXT: [[Z:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 64, i8 64>
; CHECK-NEXT: ret <2 x i1> [[Z]]
;
%y = call <2 x i8> @llvm.bitreverse.v2i8(<2 x i8> %x)
Expand Down

0 comments on commit e2079e8

Please sign in to comment.