Skip to content

Commit

Permalink
[ValueTracking] Add support for bswap and bitreverse in `isKnownT…
Browse files Browse the repository at this point in the history
…oBeAPowerOfTwo`

Both of these intrinsics don't change the number of 1s/0s so we can
look directly through them.

Proofs: https://alive2.llvm.org/ce/z/gnZuwC

Differential Revision: https://reviews.llvm.org/D157306
  • Loading branch information
goldsteinn committed Aug 9, 2023
1 parent 03b529a commit 6f4d660
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,11 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
case Intrinsic::smin:
return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Depth, Q) &&
isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
// bswap/bitreverse just move around bits, but don't change any 1s/0s
// thus dont change pow2/non-pow2 status.
case Intrinsic::bitreverse:
case Intrinsic::bswap:
return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
default:
break;
}
Expand Down
10 changes: 6 additions & 4 deletions llvm/test/Analysis/ValueTracking/known-power-of-two.ll
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ define i16 @bswap_is_pow2_or_zero(i16 %x, i16 %y) {
; CHECK-SAME: (i16 [[X:%.*]], i16 [[Y:%.*]]) {
; CHECK-NEXT: [[XP2:%.*]] = shl i16 4, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.bswap.i16(i16 [[XP2]])
; CHECK-NEXT: [[R:%.*]] = urem i16 [[Y]], [[XX]]
; CHECK-NEXT: [[TMP1:%.*]] = add i16 [[XX]], -1
; CHECK-NEXT: [[R:%.*]] = and i16 [[TMP1]], [[Y]]
; CHECK-NEXT: ret i16 [[R]]
;
%xp2 = shl i16 4, %x
Expand Down Expand Up @@ -115,7 +116,7 @@ define i1 @bswap_is_pow2(i16 %x, i16 %y) {
; CHECK-NEXT: [[XP2:%.*]] = shl nuw i16 1, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.bswap.i16(i16 [[XP2]])
; CHECK-NEXT: [[AND:%.*]] = and i16 [[XX]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[AND]], [[XX]]
; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[AND]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%xp2 = shl i16 1, %x
Expand Down Expand Up @@ -148,7 +149,8 @@ define i16 @bitreverse_is_pow2_or_zero(i16 %x, i16 %y) {
; CHECK-SAME: (i16 [[X:%.*]], i16 [[Y:%.*]]) {
; CHECK-NEXT: [[XP2:%.*]] = shl i16 4, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.bitreverse.i16(i16 [[XP2]])
; CHECK-NEXT: [[R:%.*]] = urem i16 [[Y]], [[XX]]
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i16 [[XX]], -1
; CHECK-NEXT: [[R:%.*]] = and i16 [[TMP1]], [[Y]]
; CHECK-NEXT: ret i16 [[R]]
;
%xp2 = shl i16 4, %x
Expand Down Expand Up @@ -179,7 +181,7 @@ define i1 @bitreverse_is_pow2(i16 %x, i16 %y) {
; CHECK-NEXT: [[XP2:%.*]] = shl nuw i16 1, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.bitreverse.i16(i16 [[XP2]])
; CHECK-NEXT: [[AND:%.*]] = and i16 [[XX]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[AND]], [[XX]]
; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[AND]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%xp2 = shl i16 1, %x
Expand Down

0 comments on commit 6f4d660

Please sign in to comment.