diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -2137,6 +2137,12 @@ 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; } diff --git a/llvm/test/Analysis/ValueTracking/known-power-of-two.ll b/llvm/test/Analysis/ValueTracking/known-power-of-two.ll --- a/llvm/test/Analysis/ValueTracking/known-power-of-two.ll +++ b/llvm/test/Analysis/ValueTracking/known-power-of-two.ll @@ -214,7 +214,8 @@ ; 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 @@ -262,7 +263,7 @@ ; 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 @@ -295,7 +296,8 @@ ; 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 @@ -343,7 +345,7 @@ ; 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