Skip to content

Commit

Permalink
[ValueTracking] Add support for fshl/fshr in `isKnownToBeAPowerOf…
Browse files Browse the repository at this point in the history
…Two`

If the funnel shifts are rotates (op0 == op1) then the number of 1s/0s
don't change so we can just look through op0/op1.

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

Differential Revision: https://reviews.llvm.org/D157307
  • Loading branch information
goldsteinn committed Aug 9, 2023
1 parent 6f4d660 commit 4f818da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,12 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
case Intrinsic::bitreverse:
case Intrinsic::bswap:
return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
case Intrinsic::fshr:
case Intrinsic::fshl:
// If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
if (II->getArgOperand(0) == II->getArgOperand(1))
return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
break;
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 @@ -214,7 +214,8 @@ define i16 @fshl_is_pow2_or_zero(i16 %x, i16 %y, i16 %z) {
; CHECK-SAME: (i16 [[X:%.*]], i16 [[Y:%.*]], i16 [[Z:%.*]]) {
; CHECK-NEXT: [[XP2:%.*]] = shl i16 4, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.fshl.i16(i16 [[XP2]], i16 [[XP2]], i16 [[Z]])
; 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 @@ -262,7 +263,7 @@ define i1 @fshl_is_pow2(i16 %x, i16 %y, i16 %z) {
; CHECK-NEXT: [[XP2:%.*]] = shl nuw i16 1, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.fshl.i16(i16 [[XP2]], i16 [[XP2]], i16 [[Z]])
; 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 @@ -295,7 +296,8 @@ define i16 @fshr_is_pow2_or_zero(i16 %x, i16 %y, i16 %z) {
; CHECK-SAME: (i16 [[X:%.*]], i16 [[Y:%.*]], i16 [[Z:%.*]]) {
; CHECK-NEXT: [[XP2:%.*]] = shl i16 4, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.fshr.i16(i16 [[XP2]], i16 [[XP2]], i16 [[Z]])
; 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 @@ -343,7 +345,7 @@ define i1 @fshr_is_pow2(i16 %x, i16 %y, i16 %z) {
; CHECK-NEXT: [[XP2:%.*]] = shl nuw i16 1, [[X]]
; CHECK-NEXT: [[XX:%.*]] = call i16 @llvm.fshr.i16(i16 [[XP2]], i16 [[XP2]], i16 [[Z]])
; 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 4f818da

Please sign in to comment.