Skip to content

Commit 6a65e98

Browse files
authored
[InstCombine] Drop range attributes in foldIsPowerOf2 (#111946)
Fixes #111934.
1 parent f74f568 commit 6a65e98

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -955,9 +955,11 @@ static Value *foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd,
955955
}
956956

957957
/// Reduce a pair of compares that check if a value has exactly 1 bit set.
958-
/// Also used for logical and/or, must be poison safe.
958+
/// Also used for logical and/or, must be poison safe if range attributes are
959+
/// dropped.
959960
static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
960-
InstCombiner::BuilderTy &Builder) {
961+
InstCombiner::BuilderTy &Builder,
962+
InstCombinerImpl &IC) {
961963
// Handle 'and' / 'or' commutation: make the equality check the first operand.
962964
if (JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_NE)
963965
std::swap(Cmp0, Cmp1);
@@ -971,7 +973,10 @@ static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
971973
match(Cmp1, m_SpecificICmp(ICmpInst::ICMP_ULT,
972974
m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
973975
m_SpecificInt(2)))) {
974-
Value *CtPop = Cmp1->getOperand(0);
976+
auto *CtPop = cast<Instruction>(Cmp1->getOperand(0));
977+
// Drop range attributes and re-infer them in the next iteration.
978+
CtPop->dropPoisonGeneratingAnnotations();
979+
IC.addToWorklist(CtPop);
975980
return Builder.CreateICmpEQ(CtPop, ConstantInt::get(CtPop->getType(), 1));
976981
}
977982
// (X == 0) || (ctpop(X) u> 1) --> ctpop(X) != 1
@@ -980,7 +985,10 @@ static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
980985
match(Cmp1, m_SpecificICmp(ICmpInst::ICMP_UGT,
981986
m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
982987
m_SpecificInt(1)))) {
983-
Value *CtPop = Cmp1->getOperand(0);
988+
auto *CtPop = cast<Instruction>(Cmp1->getOperand(0));
989+
// Drop range attributes and re-infer them in the next iteration.
990+
CtPop->dropPoisonGeneratingAnnotations();
991+
IC.addToWorklist(CtPop);
984992
return Builder.CreateICmpNE(CtPop, ConstantInt::get(CtPop->getType(), 1));
985993
}
986994
return nullptr;
@@ -3375,7 +3383,7 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
33753383
if (Value *V = foldSignedTruncationCheck(LHS, RHS, I, Builder))
33763384
return V;
33773385

3378-
if (Value *V = foldIsPowerOf2(LHS, RHS, IsAnd, Builder))
3386+
if (Value *V = foldIsPowerOf2(LHS, RHS, IsAnd, Builder, *this))
33793387
return V;
33803388

33813389
if (Value *V = foldPowerOf2AndShiftedMask(LHS, RHS, IsAnd, Builder))

llvm/test/Transforms/InstCombine/ispow2.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,3 +1522,35 @@ define <2 x i1> @not_pow2_or_z_known_bits_fail_wrong_cmp(<2 x i32> %xin) {
15221522
%r = icmp ugt <2 x i32> %cnt, <i32 2, i32 2>
15231523
ret <2 x i1> %r
15241524
}
1525+
1526+
; Make sure that range attributes on return values are dropped after merging these two icmps
1527+
1528+
define i1 @has_single_bit(i32 %x) {
1529+
; CHECK-LABEL: @has_single_bit(
1530+
; CHECK-NEXT: entry:
1531+
; CHECK-NEXT: [[POPCNT:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[X:%.*]])
1532+
; CHECK-NEXT: [[SEL:%.*]] = icmp eq i32 [[POPCNT]], 1
1533+
; CHECK-NEXT: ret i1 [[SEL]]
1534+
;
1535+
entry:
1536+
%cmp1 = icmp ne i32 %x, 0
1537+
%popcnt = call range(i32 1, 33) i32 @llvm.ctpop.i32(i32 %x)
1538+
%cmp2 = icmp ult i32 %popcnt, 2
1539+
%sel = select i1 %cmp1, i1 %cmp2, i1 false
1540+
ret i1 %sel
1541+
}
1542+
1543+
define i1 @has_single_bit_inv(i32 %x) {
1544+
; CHECK-LABEL: @has_single_bit_inv(
1545+
; CHECK-NEXT: entry:
1546+
; CHECK-NEXT: [[POPCNT:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[X:%.*]])
1547+
; CHECK-NEXT: [[SEL:%.*]] = icmp ne i32 [[POPCNT]], 1
1548+
; CHECK-NEXT: ret i1 [[SEL]]
1549+
;
1550+
entry:
1551+
%cmp1 = icmp eq i32 %x, 0
1552+
%popcnt = call range(i32 1, 33) i32 @llvm.ctpop.i32(i32 %x)
1553+
%cmp2 = icmp ugt i32 %popcnt, 1
1554+
%sel = select i1 %cmp1, i1 true, i1 %cmp2
1555+
ret i1 %sel
1556+
}

0 commit comments

Comments
 (0)