[PATCH] D157306: [ValueTracking] Add support for `bswap` and `bitreverse` in `isKnownToBeAPowerOfTwo`
Noah Goldstein via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 7 11:47:17 PDT 2023
goldstein.w.n updated this revision to Diff 547889.
goldstein.w.n added a comment.
Rebase
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D157306/new/
https://reviews.llvm.org/D157306
Files:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Analysis/ValueTracking/known-power-of-two.ll
Index: llvm/test/Analysis/ValueTracking/known-power-of-two.ll
===================================================================
--- llvm/test/Analysis/ValueTracking/known-power-of-two.ll
+++ llvm/test/Analysis/ValueTracking/known-power-of-two.ll
@@ -84,7 +84,8 @@
; 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 @@
; 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 @@
; 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 @@
; 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
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2139,6 +2139,11 @@
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;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157306.547889.patch
Type: text/x-patch
Size: 2515 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230807/594c8d37/attachment.bin>
More information about the llvm-commits
mailing list