Skip to content

Commit

Permalink
[ValueTracking] Add support for mul in isKnownToBeAPowerOfTwo
Browse files Browse the repository at this point in the history
pow2 * pow2 is a power of 2 or zero.

Proof: https://alive2.llvm.org/ce/z/FNiiXd

Differential Revision: https://reviews.llvm.org/D157308
  • Loading branch information
goldsteinn committed Aug 9, 2023
1 parent 4f818da commit dff3d8a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,10 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
return false;
case Instruction::Mul:
return OrZero &&
isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
case Instruction::And:
if (OrZero) {
// A power of two and'd with anything is a power of two or zero.
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/Analysis/ValueTracking/known-power-of-two.ll
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ define i16 @mul_is_pow2_or_zero(i16 %x, i16 %y, i16 %z) {
; CHECK-NEXT: [[XP2:%.*]] = shl i16 4, [[X]]
; CHECK-NEXT: [[ZP2:%.*]] = shl i16 2, [[Z]]
; CHECK-NEXT: [[XX:%.*]] = mul i16 [[XP2]], [[ZP2]]
; 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

0 comments on commit dff3d8a

Please sign in to comment.