[llvm] 6f4d660 - [ValueTracking] Add support for `bswap` and `bitreverse` in `isKnownToBeAPowerOfTwo`
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 9 12:45:45 PDT 2023
Author: Noah Goldstein
Date: 2023-08-09T14:42:55-05:00
New Revision: 6f4d660d7faa8772bb6d6ad7329c6d6e305a6c4b
URL: https://github.com/llvm/llvm-project/commit/6f4d660d7faa8772bb6d6ad7329c6d6e305a6c4b
DIFF: https://github.com/llvm/llvm-project/commit/6f4d660d7faa8772bb6d6ad7329c6d6e305a6c4b.diff
LOG: [ValueTracking] Add support for `bswap` and `bitreverse` in `isKnownToBeAPowerOfTwo`
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
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Analysis/ValueTracking/known-power-of-two.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 2cdfbdfb825f53..495fb5344cc2f2 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -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;
}
diff --git a/llvm/test/Analysis/ValueTracking/known-power-of-two.ll b/llvm/test/Analysis/ValueTracking/known-power-of-two.ll
index 1936e009e44568..fe01bfdaf432b3 100644
--- a/llvm/test/Analysis/ValueTracking/known-power-of-two.ll
+++ b/llvm/test/Analysis/ValueTracking/known-power-of-two.ll
@@ -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
@@ -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
@@ -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
@@ -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
More information about the llvm-commits
mailing list